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
+69 -28
View File
@@ -3,15 +3,16 @@
# DOWNSTAIRS BATHROOM HEATED TOWEL RAIL
# Controlled by a Sonoff Basic
#
# V2.2 2025-06-14 Fixes to offline time when sntp/network unavailable
# V2.2 2026-01-30 Added Morning/Evening Timer enable switches (gate timer windows)
# V2.1 2025-06-12 Added select and button to chose modes, added countdown & startup to boost
# V2.0 2025-06-05 YAML Tidyups
# V1.1 2025-04-12 Fixes to timers and offline modes
# V1.0 2025-02-14 Initial Version
#
#
# 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)
@@ -23,7 +24,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
@@ -54,10 +55,10 @@ substitutions:
device_name: "esp-downstbathtowelrail"
friendly_name: "Downstairs Bathroom Towelrail"
description_comment: "Heated Towel Rail, Downstairs Bathroom :: Sonoff Basic"
device_area: "Downstairs Flat" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
device_area: "Downstairs Flat" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.
# Project Naming
project_name: "Sonoff Technologies.Sonoff Basic V1" # Project Details
project_name: "Sonoff Technologies.Sonoff Basic V1" # Project Details
project_version: "v2.2" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
# Passwords
@@ -105,10 +106,10 @@ 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
@@ -191,10 +192,10 @@ preferences:
mdns:
disabled: true
##########################################################################################
##########################################################################################
# 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)
@@ -212,7 +213,34 @@ switch:
pin: GPIO12
id: relay
restore_mode: RESTORE_DEFAULT_OFF
icon: "${relay_icon}"
icon: "${relay_icon}"
####################################################
# 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
@@ -370,7 +398,7 @@ text_sensor:
}
############################
# Boost duration => integer minutes (11439)
# Boost duration => integer minutes (1-1439)
############################
- platform: mqtt_subscribe
name: "Boost Duration"
@@ -401,12 +429,12 @@ text_sensor:
- lambda: |-
// Check only the first character for mode
char c = x.c_str()[0];
if (c == 'T') { // TIMER
if (c == 'T') { // "TIMER"
id(operation_mode) = 2;
} else if (c == 'O') { // ON or OFF
// second letter NON, FOFF
} else if (c == 'O') { // "ON" or "OFF"
// second letter N->ON, F->OFF
id(operation_mode) = (x.size() > 1 && x[1] == 'N') ? 1 : 0;
} else if (c == 'B') { // BOOST
} else if (c == 'B') { // "BOOST"
id(operation_mode) = 3;
id(boost_timer) = 0;
} else {
@@ -514,7 +542,7 @@ binary_sensor:
id(relay).turn_on();
id(operation_mode) = 3;
}
- platform: template
name: "Relay Status"
lambda: |-
@@ -533,7 +561,7 @@ sensor:
update_interval: "${update_interval}"
lambda: |-
return id(boost_duration);
- platform: template
name: "Mins from Midnight"
id: mins_from_midnight
@@ -570,7 +598,6 @@ sensor:
// never return negative
return rem > 0 ? rem : 0;
#################################################################################################
# BUTTON COMPONENT
# https://esphome.io/components/button/index.html
@@ -587,6 +614,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
@@ -603,6 +631,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
#################################################################################################
@@ -658,25 +689,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 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;
}
}
if (should_on) id(relay).turn_on();
else id(relay).turn_off();
}
@@ -690,7 +732,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;
@@ -699,7 +741,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)) {
@@ -708,4 +750,3 @@ interval:
}
}
- script.execute: evaluate_relay_state