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
+71 -87
View File
@@ -3,7 +3,7 @@
# POOL LIGHT POWER AND TIMER 2
# Controlled by a Athom Smart Plug V2
#
# v4.0 2025-11-04 Changed to Athom V2
# v4.0 2026-01-31 Added Morning/Evening Timer Enable switches (gate timer windows)
# V3.0 2025-09-02 Pool Light mode controller fixes; Mode select forces Operation=ON;
# added Mode 0=OFF; select shows OFF when relay off; enforce 7s min OFF window
# V2.4 2025-06-15 Changed back to an Athom V1 (esp8266)
@@ -15,6 +15,8 @@
# 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)
# - Pool Light Mode can be set via the "Pool Light Mode" dropdown; selecting a mode forces Operation Mode = ON, and selecting "0=OFF" turns the relay OFF and returns Operation Mode to TIMER
# - 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)
@@ -49,6 +51,7 @@
##########################################################################################
##########################################################################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
@@ -58,10 +61,10 @@ substitutions:
device_name: "esp-poollightpower2"
friendly_name: "Pool Light Power"
description_comment: "Pool Light Power :: Athom Smart Plug Power V2"
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 V2" # Project Details
project_name: "Athom Technology.Smart Plug V2" # Project Details
project_version: "v3.0" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
@@ -74,7 +77,7 @@ substitutions:
update_interval: "10s" # update time for for general sensors etc
# Device Settings
relay_icon: "mdi:power-socket-au"
relay_icon: "mdi:power-socket-au"
current_limit : "10" # Current Limit in Amps. AU Plug = 10. IL, BR, EU, UK, US Plug = 16.
mqtt_timer_topic: "viewroad-commands/poollight-timer" # Topics you will use to change stuff
boost_duration_default: "180" # Minutes to stay ON in BOOST mode before reverting to TIMER
@@ -82,7 +85,7 @@ substitutions:
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: "1140" # Default in minutes from midnight. Default 19:00 => 1140
evening_off_default: "1350" # Default in minutes from midnight. Default 22:30 => 1350 => 1440 is midnight
##############################################################################
# POOL LIGHT MODE CONTROL (new)
##############################################################################
@@ -114,7 +117,7 @@ packages:
##################################################
# OPTIONAL packages (comment if you don't want them)
##################################################
##################################################
#### HOME ASSISTANT API (choose encryption or no encryption options) ####
common_api: !include
@@ -122,7 +125,7 @@ packages:
#file: common/api_common_noencryption.yaml
vars:
local_api_key: "${api_key}"
#### WEB PORTAL ####
#common_webportal: !include
# file: common/webportal_common.yaml
@@ -132,7 +135,7 @@ packages:
file: common/mqtt_common.yaml
vars:
local_device_name: "${device_name}"
#### SNTP (Only use if you want/need accurate timeclocks) ####
common_sntp: !include
file: common/sntp_common.yaml
@@ -169,7 +172,7 @@ esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" #Appears on the esphome page in HA
area: "${device_area}"
area: "${device_area}"
name_add_mac_suffix: False
min_version: 2024.6.0
project:
@@ -202,16 +205,48 @@ preferences:
mdns:
disabled: true # binary size saving
##########################################################################################
##########################################################################################
# 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)
#esp8266_store_log_strings_in_flash: false
#tx_buffer_size: 64
##########################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
##########################################################################################
switch:
####################################################
# Timer Enable Switches
# These gate the schedule windows in TIMER mode only.
# Default and restored state is ON.
####################################################
- platform: template
name: "Morning Timer Enable"
id: morning_timer_enable
restore_mode: RESTORE_DEFAULT_ON
icon: "mdi:timer-outline"
optimistic: true
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
restore_mode: RESTORE_DEFAULT_ON
icon: "mdi:timer-outline"
optimistic: true
on_turn_on:
- script.execute: evaluate_relay_state
on_turn_off:
- script.execute: evaluate_relay_state
##########################################################################################
# Global Variables for use in automations etc
# https://esphome.io/guides/automations.html?highlight=globals#global-variables
@@ -433,7 +468,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 {
@@ -546,31 +581,6 @@ text_sensor:
# BINARY SENSORS
# https://esphome.io/components/binary_sensor/
##########################################################################################
#binary_sensor:
# - platform: gpio
# pin:
# number: GPIO03
# mode: INPUT_PULLUP
# inverted: true
# name: "Power Button"
# id: power_button
# filters:
# - delayed_on: 20ms
# on_click:
# - min_length: 20ms
# max_length: 500ms
# then:
# - lambda: |-
# if (id(relay).state) {
# // Relay is ON: turn it OFF and set mode to 2 (TIMER)
# id(relay).turn_off();
# id(operation_mode) = 2;
# } else {
# // Relay is OFF: turn it ON and set mode to 3 (BOOST)
# id(relay).turn_on();
# id(operation_mode) = 3;
# }
binary_sensor:
- platform: template
id: relay_status
@@ -603,7 +613,6 @@ binary_sensor:
id(last_off_ms) = millis();
if (!id(mode_changing)) id(current_mode) = 0;
##########################################################################################
# Sensors
# https://esphome.io/components/text_sensor/index.html
@@ -617,7 +626,7 @@ sensor:
update_interval: "${update_interval}"
lambda: |-
return id(boost_duration);
- platform: template
name: "Mins from Midnight"
id: mins_from_midnight
@@ -654,37 +663,6 @@ sensor:
// never return negative
return rem > 0 ? rem : 0;
#################################################################################################
# SWITCH COMPONENT
# https://esphome.io/components/switch/
#################################################################################################
#switch:
# - platform: gpio
# name: "Power Output"
# pin: GPIO14
# 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}"
# # POOL LIGHT MODE CONTROL: detect quick vs long off periods and keep current_mode in sync
# on_turn_off:
# - lambda: |-
# id(last_off_ms) = millis();
# if (!id(mode_changing)) id(current_mode) = 0; // reflect OFF in select when off normally
# on_turn_on:
# - lambda: |-
# if (id(mode_changing)) {
# return;
# }
# uint32_t delta = millis() - id(last_off_ms);
# if (delta >= (uint32_t)id(reset_window_ms)) {
# id(current_mode) = 1;
# } else {
# id(current_mode)++;
# if (id(current_mode) > id(max_modes)) id(current_mode) = 1; // keep internal index sane
# }
#################################################################################################
# BUTTON COMPONENT
# https://esphome.io/components/button/index.html
@@ -701,6 +679,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
@@ -717,6 +696,9 @@ button:
id(operation_mode)= 2;
id(boost_timer) = 0;
ESP_LOGI("timer","Default timer settings applied");
# Restore timer enable switches to ON (defaults)
- switch.turn_on: morning_timer_enable
- switch.turn_on: evening_timer_enable
- script.execute: evaluate_relay_state
#################################################################################################
@@ -755,13 +737,6 @@ select:
}
- script.execute: evaluate_relay_state
##############################################################################
# POOL LIGHT MODE SELECT (new)
# 0=OFF -> immediately turns relay OFF and sets Operation Mode = TIMER.
# Selecting a color (>=1) forces Operation Mode = ON.
# If target < current, do a long reset to mode 1, then pulse forward.
# Ignored modes are hidden here but still counted by pulses.
##############################################################################
##############################################################################
# POOL LIGHT MODE SELECT (updated display logic)
# 0=OFF -> immediately turns relay OFF and sets Operation Mode = TIMER.
@@ -909,10 +884,21 @@ script:
target_on = true;
} else if (mode == 2) { // TIMER
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 when Morning Timer Enable is enabled
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 when Evening Timer Enable is enabled
if (id(evening_timer_enable).state) {
if (id(current_mins) >= id(evening_on) && id(current_mins) < id(evening_off)) {
should_on = true;
}
}
target_on = should_on;
}
@@ -963,7 +949,6 @@ script:
- lambda: |-
id(current_mode) = 1;
# Change to desired_mode, robust from OFF and without wrap on lower targets
# Change to desired_mode, robust from OFF and without wrap on lower targets
- id: change_to_desired_mode
mode: restart
@@ -986,7 +971,7 @@ script:
- switch.turn_on: relay
- delay: ${mode_prime_on}
- lambda: |-
// If we turned ON after reset window, hardware is at Mode 1
// If we turned ON after >= reset window, hardware is at Mode 1
if ((millis() - id(last_off_ms)) >= (uint32_t) id(reset_window_ms)) {
id(current_mode) = 1;
}
@@ -1028,7 +1013,6 @@ script:
- lambda: |-
id(mode_changing) = false;
#################################################################################################
# INTERVAL COMPONENT
# https://esphome.io/components/interval.html
@@ -1038,7 +1022,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;
@@ -1047,7 +1031,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)) {