yaml tidyups for esphome

This commit is contained in:
root
2026-02-05 21:28:48 +13:00
parent c104187d62
commit d7884770fe
15 changed files with 633 additions and 539 deletions
+110 -76
View File
@@ -6,14 +6,16 @@
# dashboard_import:
# package_import_url: github://athom-tech/esp32-configs/athom-smart-plug.yaml
#
# V2.4 2026-01-31 Added Morning/Evening Timer Enable switches (gate timer windows)
# V2.3 2025-06-19 Minor changes to remember mode in reboot and to ensure offline functionality
# V2.2 2025-06-14 Fixes to offline time when sntp/network unavailable
# V2.1 2025-06-12 Added select and button to chose modes, added countdown & startup to boost
# V2.0 2025-06-05 YAML Tidyups
#
#
# INSTRUCTIONS
# - It allows the device to work in a standalone timer style operation
# - The timer has a morning and evening time (but no weekday/weekend settings)
# - Morning Timer Enable / Evening Timer Enable switches gate their timer windows (default ON and restored on reboot)
# - Default values are set, but changed values are remembered in flash
# - It uses SNTP for time setting (but obviously only if wifi & networking are working)
# - It will default to an internal timer if no wifi. To reset internal timer, reboot the device at 12pm (noon)
@@ -25,7 +27,7 @@
# - Home Assistant entities are set so that BOOST mode can be pressed with a button and other modes selectable with a dropdown
# - If you need it ON continuously with no MQTT, toggle power ON/OFF 4 times within 30 seconds (with ~2 secs in between to allow it to boot)
#
# MQTT Commands
# MQTT Commands
# Values will be set in place on the update_interval time, not immediately
# Use 00:00 in 24hr format for time setting. (Note there is no weekday/weekend setting)
# mqtt_timer_topic/morning-on/06:00 : Time device will go on
@@ -56,11 +58,11 @@ substitutions:
device_name: "esp-poolpumppower"
friendly_name: "Pool Pump Power"
description_comment: "Pool Pump Power :: Athom Smart Plug Power V3"
device_area: "Outside" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
device_area: "Outside" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Athom Technology.Smart Plug V3" # Project Details
project_version: "v2.2" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
project_version: "v2.4" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
api_key: !secret esp-poolpumppower_api_key # unfortunately you can't use substitutions inside secrets names
@@ -70,17 +72,17 @@ substitutions:
# Device Settings
log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
update_interval: "20s" # update time for for general sensors etc
# Device Settings
relay_icon: "mdi:pool"
current_limit : "10" # Current Limit in Amps. AU Plug = 10. IL, BR, EU, UK, US Plug = 16.
current_limit: "10" # Current Limit in Amps. AU Plug = 10. IL, BR, EU, UK, US Plug = 16.
mqtt_timer_topic: "viewroad-commands/poolpump-timer" # Topics you will use to change stuff
boost_duration_default: "60" # Minutes to stay ON in STARTUP mode before reverting to TIMER
morning_on_default: "450" # Default in minutes from midnight. Default 07:30 => 450
morning_off_default: "450" # Default in minutes from midnight. Default 07:30 => 450 (same as ON as no need for morning schedule)
evening_on_default: "1260" # Default in minutes from midnight. Default 21:00 => 1260
evening_off_default: "1350" # Default in minutes from midnight. Default 22:30 => 1350 => 1440 is midnight
##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
@@ -106,17 +108,17 @@ packages:
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
#### WEB PORTAL ####
#common_webportal: !include common/webportal_common.yaml
#### SNTP (Only use if you want/need accurate timeclocks) ####
common_sntp: !include common/sntp_common.yaml
common_sntp: !include common/sntp_common.yaml
#### DIAGNOSTICS Sensors ####
diag_basic: !include common/include_basic_diag_sensors.yaml
diag_more: !include common/include_more_diag_sensors.yaml
diag_debug: !include common/include_debug_diag_sensors.yaml
diag_resetcount: !include common/include_resetcount_diag_sensors.yaml
diag_basic: !include common/include_basic_diag_sensors.yaml
diag_more: !include common/include_more_diag_sensors.yaml
diag_debug: !include common/include_debug_diag_sensors.yaml
diag_resetcount: !include common/include_resetcount_diag_sensors.yaml
# Device Specific included packages
common_athompowermonV3: !include
@@ -132,8 +134,8 @@ esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" #Appears on the esphome page in HA
area: "${device_area}"
name_add_mac_suffix: False
area: "${device_area}"
name_add_mac_suffix: false
min_version: 2024.6.0
project:
name: "${project_name}"
@@ -144,7 +146,7 @@ esphome:
board_build.flash_mode: dio
on_boot:
then:
- script.execute: evaluate_relay_state
- script.execute: evaluate_relay_state
##########################################################################################
# ESP Platform and Framework
@@ -155,7 +157,7 @@ esp32:
flash_size: 4MB
variant: ESP32C3
framework:
type: esp-idf # "esp-idf" OR "arduino". Suggested ESP-IDF Framework, or Plug Out the UART Cable Might Cause ESP32 Hang.
type: esp-idf # "esp-idf" OR "arduino". Suggested ESP-IDF Framework, or Plug Out the UART Cable Might Cause ESP32 Hang.
version: recommended # recommended, latest or dev
preferences:
@@ -164,10 +166,10 @@ preferences:
esp32_improv:
authorizer: none
##########################################################################################
##########################################################################################
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
##########################################################################################
##########################################################################################
logger:
level: "${log_level}" #INFO Level suggested, or DEBUG for testing
baud_rate: 0 #set to 0 for no logging via UART, needed if you are using it for other serial things (eg PZEM)
@@ -234,16 +236,6 @@ globals:
restore_value: true
initial_value: "2"
####################################################
# current_mins is set if SNTP is invalid.
# We assume user powers on the device at 12:00 noon
# => 12 * 60 = 720 minutes from midnight.
####################################################
- id: current_mins
type: int
restore_value: true
initial_value: "720" # 720 is 12:00 Noon
####################################################
# boost_timer: counts minutes in BOOST mode
# After 'boost_duration' minutes, revert to TIMER.
@@ -272,7 +264,7 @@ text_sensor:
name: "Morning On Time Setting"
id: morning_on_topic
topic: "${mqtt_timer_topic}/morning-on" # Stored in the format HH:MM
internal: True
internal: true
on_value:
then:
- lambda: |-
@@ -285,6 +277,7 @@ text_sensor:
} else {
ESP_LOGW("timer","Invalid Morning On format: %s", x.c_str());
}
####################################################
# Morning Off time => "HH:MM"
####################################################
@@ -292,7 +285,7 @@ text_sensor:
name: "Morning Off Time Setting"
id: morning_off_topic
topic: "${mqtt_timer_topic}/morning-off" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
internal: true # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
@@ -304,6 +297,7 @@ text_sensor:
} else {
ESP_LOGW("timer","Invalid Morning Off format: %s", x.c_str());
}
####################################################
# Evening On time => "HH:MM"
####################################################
@@ -311,7 +305,7 @@ text_sensor:
name: "Evening On Time Setting"
id: evening_on_topic
topic: "${mqtt_timer_topic}/evening-on" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
internal: true # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
@@ -323,6 +317,7 @@ text_sensor:
} else {
ESP_LOGW("timer","Invalid Evening On format: %s", x.c_str());
}
####################################################
# Evening Off time => "HH:MM"
####################################################
@@ -330,7 +325,7 @@ text_sensor:
name: "Evening Off Time Setting"
id: evening_off_topic
topic: "${mqtt_timer_topic}/evening-off" # Stored in the format HH:MM
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
internal: true # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
@@ -342,6 +337,7 @@ text_sensor:
} else {
ESP_LOGW("timer","Invalid Evening Off format: %s", x.c_str());
}
####################################################
# Boost duration => 1 - 1439
####################################################
@@ -349,7 +345,7 @@ text_sensor:
name: "Boost Duration"
id: boost_time_topic
topic: "${mqtt_timer_topic}/boost-time" # Stored as an integer from 1-1439
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
internal: true # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
@@ -357,7 +353,7 @@ text_sensor:
char *endptr;
long v = strtol(x.c_str(), &endptr, 10);
// invalid if nothing parsed, trailing chars, or out of 01439
// invalid if nothing parsed, trailing chars, or out of 0-1439
if (endptr == x.c_str() || *endptr != '\0' || v < 0 || v > 1439) {
ESP_LOGE("boost_time", "Invalid boost_time '%s'", x.c_str());
} else {
@@ -368,13 +364,11 @@ text_sensor:
# Subscribe to operation mode:
# OFF, ON, TIMER, BOOST
# We do case-insensitive compare using strcasecmp
# (Requires <strings.h> typically included in ESPHome)
####################################################
# MQTT subscription: set mode, then immediately re-evaluate relay
- platform: mqtt_subscribe
id: timer_operation_mode_topic
topic: "${mqtt_timer_topic}/operation"
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
internal: true # No need to show this in Home Assistant as there is a sensor that shows the set value
on_value:
then:
- lambda: |-
@@ -420,7 +414,6 @@ text_sensor:
lambda: |-
int hour = id(morning_on) / 60;
int minute = id(morning_on) % 60;
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
@@ -434,8 +427,6 @@ text_sensor:
lambda: |-
int hour = id(morning_off) / 60;
int minute = id(morning_off) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
@@ -449,8 +440,6 @@ text_sensor:
lambda: |-
int hour = id(evening_on) / 60;
int minute = id(evening_on) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
@@ -464,8 +453,6 @@ text_sensor:
lambda: |-
int hour = id(evening_off) / 60;
int minute = id(evening_off) % 60;
// Increase buffer size to 8 just to be safe
// Increase to 16 for safety
char buff[16];
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
return { std::string(buff) };
@@ -491,7 +478,7 @@ binary_sensor:
then:
- lambda: |-
if (id(relay).state) {
// Relay is ON: turn it OFF and set mode to 0 (TIMER)
// Relay is ON: turn it OFF and set mode to 2 (TIMER)
id(relay).turn_off();
id(operation_mode) = 2;
} else {
@@ -499,7 +486,7 @@ binary_sensor:
id(relay).turn_on();
id(operation_mode) = 3;
}
- platform: template
name: "Relay Status"
lambda: |-
@@ -518,14 +505,14 @@ sensor:
update_interval: "${update_interval}"
lambda: |-
return id(boost_duration);
- platform: template
name: "Mins from Midnight"
id: mins_from_midnight
unit_of_measurement: "mins"
accuracy_decimals: "0"
update_interval: "${update_interval}"
internal: True # No need to show this in Home Assistant
internal: true # No need to show this in Home Assistant
lambda: |-
return id(current_mins);
@@ -541,21 +528,28 @@ sensor:
if (!id(relay).state) {
return 0;
}
int rem = 0;
// only calculate for mode 2 (scheduled) or mode 3 (BOOST)
if (id(operation_mode) == 2) {
int a = id(morning_off) - id(current_mins);
int b = id(evening_off) - id(current_mins);
// if a is negative, use b; otherwise pick the smaller of a or b
rem = (a < 0) ? b : (a < b ? a : b);
}
else if (id(operation_mode) == 3) {
// Only report remaining time for the currently-active enabled window
if (id(morning_timer_enable).state &&
id(current_mins) >= id(morning_on) && id(current_mins) < id(morning_off)) {
rem = id(morning_off) - id(current_mins);
} else if (id(evening_timer_enable).state &&
id(current_mins) >= id(evening_on) && id(current_mins) < id(evening_off)) {
rem = id(evening_off) - id(current_mins);
} else {
rem = 0;
}
} else if (id(operation_mode) == 3) {
rem = id(boost_duration) - id(boost_timer);
}
// never return negative
return rem > 0 ? rem : 0;
#################################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
@@ -567,8 +561,36 @@ switch:
id: relay
restore_mode: RESTORE_DEFAULT_OFF # Ensures the relay is restored (or off) at boot
#internal: true # Hides the switch from Home Assistant
icon: "${relay_icon}"
icon: "${relay_icon}"
#################################################################################################
# TIMER ENABLE SWITCHES (gate the schedule windows)
# These default ON, and restore as ON at reboot (unless changed).
#################################################################################################
- platform: template
name: "Morning Timer Enable"
id: morning_timer_enable
icon: "mdi:timer-outline"
optimistic: true
restore_mode: RESTORE_DEFAULT_ON
entity_category: config
on_turn_on:
- script.execute: evaluate_relay_state
on_turn_off:
- script.execute: evaluate_relay_state
- platform: template
name: "Evening Timer Enable"
id: evening_timer_enable
icon: "mdi:timer-outline"
optimistic: true
restore_mode: RESTORE_DEFAULT_ON
entity_category: config
on_turn_on:
- script.execute: evaluate_relay_state
on_turn_off:
- script.execute: evaluate_relay_state
#################################################################################################
# BUTTON COMPONENT
# https://esphome.io/components/button/index.html
@@ -585,6 +607,7 @@ button:
id(operation_mode) = 3;
# 2) immediately re-evaluate relay state
- script.execute: evaluate_relay_state
- platform: template
name: "Default timer settings"
id: default_timer_settings_button
@@ -592,14 +615,15 @@ button:
on_press:
- lambda: |-
// Restore all timing globals to their YAML defaults
id(morning_on) = ${morning_on_default};
id(morning_off) = ${morning_off_default};
id(evening_on) = ${evening_on_default};
id(evening_off) = ${evening_off_default};
id(boost_duration)= ${boost_duration_default};
id(morning_on) = ${morning_on_default};
id(morning_off) = ${morning_off_default};
id(evening_on) = ${evening_on_default};
id(evening_off) = ${evening_off_default};
id(boost_duration) = ${boost_duration_default};
// Reset mode to TIMER and clear any running boost
id(operation_mode)= 2;
id(boost_timer) = 0;
id(operation_mode) = 2;
id(boost_timer) = 0;
ESP_LOGI("timer","Default timer settings applied");
- script.execute: evaluate_relay_state
@@ -656,25 +680,36 @@ script:
return;
}
// OFF always off
// OFF -> always off
if (mode == 0) {
id(relay).turn_off();
return;
}
// ON always on
// ON -> always on
if (mode == 1) {
id(relay).turn_on();
return;
}
// TIMER follow schedule windows
// TIMER -> follow schedule windows (gated by enable switches)
{
bool should_on = false;
if (id(current_mins) >= id(morning_on) && id(current_mins) < id(morning_off))
should_on = true;
if (id(current_mins) >= id(evening_on) && id(current_mins) < id(evening_off))
should_on = true;
// Morning window only applies if Morning Timer Enable is ON
if (id(morning_timer_enable).state) {
if (id(current_mins) >= id(morning_on) && id(current_mins) < id(morning_off)) {
should_on = true;
}
}
// Evening window only applies if Evening Timer Enable is ON
if (id(evening_timer_enable).state) {
if (id(current_mins) >= id(evening_on) && id(current_mins) < id(evening_off)) {
should_on = true;
}
}
if (should_on) id(relay).turn_on();
else id(relay).turn_off();
}
@@ -688,7 +723,7 @@ interval:
- interval: "1min"
then:
- lambda: |-
// update current_mins via SNTP or fallback
// - update current_mins via SNTP or fallback
if (!id(sntp_time).now().is_valid()) {
id(current_mins)++;
if (id(current_mins) >= 1440) id(current_mins) = 0;
@@ -697,7 +732,7 @@ interval:
id(current_mins) = now.hour * 60 + now.minute;
}
// if in BOOST, advance boost_timer and expire when done
// - if in BOOST, advance boost_timer and expire when done
if (id(operation_mode) == 3) {
id(boost_timer)++;
if (id(boost_timer) >= id(boost_duration)) {
@@ -706,4 +741,3 @@ interval:
}
}
- script.execute: evaluate_relay_state