yaml layout updates to v1.x
This commit is contained in:
@@ -1,15 +1,22 @@
|
||||
##########################################################################################
|
||||
##########################################################################################
|
||||
# DOWNSTAIRS BATHROOM HEATED TOWEL RAIL
|
||||
# Controlled by a Sonoff Basic
|
||||
#
|
||||
#:########################################################################################:#
|
||||
# TITLE: DOWNSTAIRS BATHROOM HEATED TOWEL RAIL
|
||||
# zorruno.com layout v1.1 2026
|
||||
#:########################################################################################:#
|
||||
# REPO:
|
||||
# https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-downstbathtowelrail.yaml
|
||||
#:########################################################################################:#
|
||||
# VERSIONS:
|
||||
# V2.3 2026-02-25 Updated yaml to zorruno layout V1.1
|
||||
# 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
|
||||
#:########################################################################################:#
|
||||
# HARDWARE:
|
||||
# Controlled by a Sonoff Basic
|
||||
#:########################################################################################:#
|
||||
# OPERATION NOTES:
|
||||
# - 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)
|
||||
@@ -17,55 +24,68 @@
|
||||
# - 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)
|
||||
# - If on a network and there is a MQTT server, you can set the on/off times via MQTT (See below commands)
|
||||
# - You can set 4 modes ON/OFF/TIMER/BOOST via MQTT. Setting BOOST gives you a oneshot operation
|
||||
# - You can set 4 modes ON/OFF/TIMER/BOOST via MQTT. Setting BOOST gives you a oneshot operation
|
||||
# - Any new timer times set via MQTT will be remembered though a reboot
|
||||
# - On startup, or a reboot, the device will always turn on for the BOOST Duration (BOOST mode, default 2 hours)
|
||||
# - TIMER mode will always be switched on after BOOST mode is complete
|
||||
# - 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
|
||||
# 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
|
||||
# mqtt_timer_topic/morning-off/08:00 : Time device will go off
|
||||
# mqtt_timer_topic/evening-on/09:00 : Time device will go on
|
||||
# mqtt_timer_topic/evening-off/00:00 : Time device will go off
|
||||
# mqtt_timer_topic/boost-time/0000 : Time in minutes device will temporarily go on for (1-1439)
|
||||
# mqtt_timer_topic/operation/ON : Device permanently on
|
||||
# mqtt_timer_topic/operation/OFF : Device permanently off
|
||||
# mqtt_timer_topic/operation/TIMER : Device will obey timer settings
|
||||
# mqtt_timer_topic/operation/BOOST : Turn on for (boost_duration) minutes then BOOST (also on startup)
|
||||
#:########################################################################################:#
|
||||
# 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
|
||||
# mqtt_timer_topic/morning-off/08:00 : Time device will go off
|
||||
# mqtt_timer_topic/evening-on/09:00 : Time device will go on
|
||||
# mqtt_timer_topic/evening-off/00:00 : Time device will go off
|
||||
# mqtt_timer_topic/boost-time/0000 : Time in minutes device will temporarily go on for (1-1439)
|
||||
# mqtt_timer_topic/operation/ON : Device permanently on
|
||||
# mqtt_timer_topic/operation/OFF : Device permanently off
|
||||
# mqtt_timer_topic/operation/TIMER : Device will obey timer settings
|
||||
# mqtt_timer_topic/operation/BOOST : Turn on for (boost_duration) minutes then BOOST (also on startup)
|
||||
#
|
||||
# operation_mode:
|
||||
# 0 = OFF
|
||||
# 1 = ON
|
||||
# 2 = TIMER
|
||||
# 3 = BOOST
|
||||
#
|
||||
##########################################################################################
|
||||
##########################################################################################
|
||||
#:########################################################################################:#
|
||||
# OFFLINE NOTES:
|
||||
# a) Home Assistant OFFLINE, but Network and MQTT ONLINE
|
||||
# - The towel rail will stick to its assigned schedule (and should remember on reboot)
|
||||
# - Schedule times and on/off switching can still occur via MQTT
|
||||
# b) MQTT OFFLINE (but WiFi/Network and HA API ONLINE)
|
||||
# - With no MQTT automation controllers, the towel rail will still run its timer logic locally
|
||||
# - If internet/wifi is still available, timing will be accurate (SNTP)
|
||||
# c) Entire WiFi/Network OFFLINE
|
||||
# - The towel rail will still run its timer logic locally, however timing will likely shift without SNTP
|
||||
# - Timeclocks will drift without network (SNTP). Time of day can be reset by powering off then on again at 12pm (midday)
|
||||
#:########################################################################################:#
|
||||
|
||||
##########################################################################################
|
||||
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
|
||||
#:########################################################################################:#
|
||||
# SUBSTITUTIONS: Specific device variable substitutions
|
||||
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
|
||||
##########################################################################################
|
||||
#:########################################################################################:#
|
||||
substitutions:
|
||||
# Device Naming
|
||||
device_name: "esp-downstbathtowelrail"
|
||||
friendly_name: "Downstairs Bathroom Towelrail"
|
||||
description_comment: "Heated Towel Rail, Downstairs Bathroom :: Sonoff Basic"
|
||||
description_comment: "Heated Towel Rail, Downstairs Bathroom :: Sonoff Basic (Layout V1.1)"
|
||||
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_version: "v2.2" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
|
||||
project_version: "v2.3" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
|
||||
|
||||
# Passwords
|
||||
api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names
|
||||
ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names
|
||||
# Passwords (unfortunately you can't use substitutions inside secrets names)
|
||||
api_key: !secret esp-api_key
|
||||
ota_pass: !secret esp-ota_pass
|
||||
static_ip_address: !secret esp-downstbathtowelrail_ip
|
||||
|
||||
# If we are changing IP addresses, you must update the current IP address here, otherwise it remains
|
||||
# Don't forget to switch it back when changed.
|
||||
current_ip_address: ${static_ip_address}
|
||||
|
||||
# Device Settings
|
||||
relay_icon: "mdi:heating-coil"
|
||||
log_level: "ERROR" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
|
||||
@@ -73,18 +93,16 @@ substitutions:
|
||||
|
||||
# Timer Settings
|
||||
mqtt_timer_topic: "viewroad-commands/downstbath-towelrail" # Topics you will use to change stuff
|
||||
boost_duration_default: "120" # Minutes to stay ON in BOOST mode before reverting to TIMER
|
||||
morning_on_default: "420" # Default in minutes from midnight. Default 07:00 => 420
|
||||
morning_off_default: "450" # Default in minutes from midnight. Default 07:30 => 450
|
||||
evening_on_default: "1260" # Default in minutes from midnight. Default 21:00 => 1260
|
||||
evening_off_default: "1320" # Default in minutes from midnight. Default 22:00 => 1320 => 1440 is midnight
|
||||
boost_duration_default: "120" # Minutes to stay ON in BOOST mode before reverting to TIMER
|
||||
morning_on_default: "420" # Default in minutes from midnight. Default 07:00 => 420
|
||||
morning_off_default: "450" # Default in minutes from midnight. Default 07:30 => 450
|
||||
evening_on_default: "1260" # Default in minutes from midnight. Default 21:00 => 1260
|
||||
evening_off_default: "1320" # Default in minutes from midnight. Default 22:00 => 1320
|
||||
|
||||
##########################################################################################
|
||||
#:########################################################################################:#
|
||||
# PACKAGES: Included Common Packages
|
||||
# https://esphome.io/components/packages.html
|
||||
##########################################################################################
|
||||
# Comment some of these out where not needed, eg MQTT, Web portal, SNTP, General Sensors
|
||||
##########################################################################################
|
||||
#:########################################################################################:#
|
||||
packages:
|
||||
#### WIFI, Network (Static/DHCP/IPV6 etc), Fallback AP, Safemode ####
|
||||
common_wifi: !include
|
||||
@@ -93,6 +111,7 @@ packages:
|
||||
local_device_name: "${device_name}"
|
||||
local_static_ip_address: "${static_ip_address}"
|
||||
local_ota_pass: "${ota_pass}"
|
||||
local_current_ip_address: "${current_ip_address}"
|
||||
|
||||
#### HOME ASSISTANT API (choose encryption or no encryption options) ####
|
||||
common_api: !include
|
||||
@@ -111,31 +130,34 @@ packages:
|
||||
#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_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
|
||||
|
||||
#############################################
|
||||
# ESPHome
|
||||
#:########################################################################################:#
|
||||
# ESPHOME:
|
||||
# https://esphome.io/components/esphome.html
|
||||
#############################################
|
||||
#:########################################################################################:#
|
||||
esphome:
|
||||
name: "${device_name}"
|
||||
friendly_name: "${friendly_name}"
|
||||
comment: "${description_comment}" # Appears on the esphome page in HA
|
||||
area: "${device_area}"
|
||||
project:
|
||||
name: "${project_name}"
|
||||
version: "${project_version}"
|
||||
platformio_options:
|
||||
build_flags:
|
||||
- "-Os" # optimize for size
|
||||
- "-Wl,--gc-sections" # drop unused code/data
|
||||
#- "-fno-exceptions" # strip C++ exceptions
|
||||
#- "-fno-rtti" # strip C++ RTTI
|
||||
- "-Os" # optimize for size
|
||||
- "-Wl,--gc-sections" # drop unused code/data
|
||||
#- "-fno-exceptions" # strip C++ exceptions
|
||||
#- "-fno-rtti" # strip C++ RTTI
|
||||
on_boot:
|
||||
priority: 900 # High priority to run after globals are initialized
|
||||
priority: 900 # High priority to run after globals are initialized
|
||||
then:
|
||||
# This evaluates if we switch on 4+ times in 20 seconds
|
||||
# If so, just change to on mode permanently.
|
||||
@@ -176,15 +198,16 @@ esphome:
|
||||
id(boost_timer) = 0; // and reset boost_timer = 0 (for time sync if no sntp)
|
||||
ESP_LOGI("power_cycle", "Boot count=%d => BOOST mode", id(boot_count));
|
||||
}
|
||||
- script.execute: evaluate_relay_state # Check what the relay should be doing straight away
|
||||
- script.execute: evaluate_relay_state # Check what the relay should be doing straight away
|
||||
|
||||
#############################################
|
||||
# ESP Platform and Framework
|
||||
# https://esphome.io/components/esp32.html
|
||||
#############################################
|
||||
#:########################################################################################:#
|
||||
# ESP PLATFORM AND FRAMEWORK:
|
||||
# https://esphome.io/components/esp8266/
|
||||
# https://esphome.io/components/esp32/
|
||||
#:########################################################################################:#
|
||||
esp8266:
|
||||
board: esp01_1m # The original sonoff basic
|
||||
restore_from_flash: True # restore some values on reboot
|
||||
board: esp01_1m # The original sonoff basic
|
||||
restore_from_flash: true # restore some values on reboot
|
||||
|
||||
preferences:
|
||||
flash_write_interval: 5min
|
||||
@@ -192,21 +215,21 @@ preferences:
|
||||
mdns:
|
||||
disabled: true
|
||||
|
||||
##########################################################################################
|
||||
# ESPHome Logging Enable
|
||||
#:########################################################################################:#
|
||||
# LOGGING: 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)
|
||||
level: "${log_level}"
|
||||
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
|
||||
#:########################################################################################:#
|
||||
# SWITCH COMPONENT:
|
||||
# https://esphome.io/components/switch/
|
||||
# Relay Switch (Sonoff Basic Relay on GPIO12)
|
||||
####################################################
|
||||
#:########################################################################################:#
|
||||
switch:
|
||||
- platform: gpio
|
||||
name: "Towel Rail Power"
|
||||
@@ -242,12 +265,11 @@ switch:
|
||||
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
|
||||
##########################################################################################
|
||||
#:########################################################################################:#
|
||||
# GLOBAL VARIABLES:
|
||||
# https://esphome.io/guides/automations.html#global-variables
|
||||
#:########################################################################################:#
|
||||
globals:
|
||||
|
||||
# Tracks the time (in seconds from midnight) at the previous boot
|
||||
- id: last_boot_time_s
|
||||
type: int
|
||||
@@ -260,31 +282,31 @@ globals:
|
||||
restore_value: true
|
||||
initial_value: "0"
|
||||
|
||||
# Morning On time (minutes from midnight),
|
||||
# Morning On time (minutes from midnight)
|
||||
- id: morning_on
|
||||
type: int
|
||||
restore_value: true
|
||||
initial_value: "${morning_on_default}"
|
||||
|
||||
# Morning Off time (minutes from midnight),
|
||||
# Morning Off time (minutes from midnight)
|
||||
- id: morning_off
|
||||
type: int
|
||||
restore_value: true
|
||||
initial_value: "${morning_off_default}"
|
||||
|
||||
# Evening On time (minutes from midnight),
|
||||
# Evening On time (minutes from midnight)
|
||||
- id: evening_on
|
||||
type: int
|
||||
restore_value: true
|
||||
initial_value: "${evening_on_default}"
|
||||
|
||||
# Evening Off time (minutes from midnight),
|
||||
# Evening Off time (minutes from midnight)
|
||||
- id: evening_off
|
||||
type: int
|
||||
restore_value: true
|
||||
initial_value: "${evening_off_default}"
|
||||
|
||||
# Boost Duration (minutes),
|
||||
# Boost Duration (minutes)
|
||||
- id: boost_duration
|
||||
type: int
|
||||
restore_value: true
|
||||
@@ -312,18 +334,16 @@ globals:
|
||||
restore_value: false
|
||||
initial_value: "0"
|
||||
|
||||
##########################################################################################
|
||||
# Text Sensors
|
||||
# https://esphome.io/components/text_sensor/index.html
|
||||
##########################################################################################
|
||||
#:########################################################################################:#
|
||||
# TEXT SENSORS:
|
||||
# https://esphome.io/components/text_sensor/
|
||||
#:########################################################################################:#
|
||||
text_sensor:
|
||||
############################
|
||||
# MQTT Subscriptions
|
||||
############################
|
||||
|
||||
############################
|
||||
# MQTT Subscriptions
|
||||
############################
|
||||
############################
|
||||
# Morning On time => "HH:MM"
|
||||
############################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Morning On Time Setting"
|
||||
id: morning_on_topic
|
||||
@@ -340,9 +360,7 @@ text_sensor:
|
||||
ESP_LOGW("timer","Invalid Morning On format: %s", x.c_str());
|
||||
}
|
||||
|
||||
############################
|
||||
# Morning Off time => "HH:MM"
|
||||
############################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Morning Off Time Setting"
|
||||
id: morning_off_topic
|
||||
@@ -359,9 +377,7 @@ text_sensor:
|
||||
ESP_LOGW("timer","Invalid Morning Off format: %s", x.c_str());
|
||||
}
|
||||
|
||||
############################
|
||||
# Evening On time => "HH:MM"
|
||||
############################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Evening On Time Setting"
|
||||
id: evening_on_topic
|
||||
@@ -378,9 +394,7 @@ text_sensor:
|
||||
ESP_LOGW("timer","Invalid Evening On format: %s", x.c_str());
|
||||
}
|
||||
|
||||
############################
|
||||
# Evening Off time => "HH:MM"
|
||||
############################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Evening Off Time Setting"
|
||||
id: evening_off_topic
|
||||
@@ -397,9 +411,7 @@ text_sensor:
|
||||
ESP_LOGW("timer","Invalid Evening Off format: %s", x.c_str());
|
||||
}
|
||||
|
||||
############################
|
||||
# Boost duration => integer minutes (1-1439)
|
||||
############################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Boost Duration"
|
||||
id: boost_time_topic
|
||||
@@ -409,7 +421,6 @@ text_sensor:
|
||||
then:
|
||||
- lambda: |-
|
||||
int v = 0;
|
||||
// Parse as integer
|
||||
if (sscanf(x.c_str(), "%d", &v) == 1 && v >= 1 && v <= 1439) {
|
||||
id(boost_duration) = v;
|
||||
ESP_LOGI("boost_time","Received new Boost Duration: %d mins", v);
|
||||
@@ -417,9 +428,7 @@ text_sensor:
|
||||
ESP_LOGW("boost_time","Invalid boost_time '%s'", x.c_str());
|
||||
}
|
||||
|
||||
####################################################
|
||||
# Subscribe to operation mode: OFF, ON, TIMER, BOOST
|
||||
####################################################
|
||||
- platform: mqtt_subscribe
|
||||
id: timer_operation_mode_topic
|
||||
topic: "${mqtt_timer_topic}/operation"
|
||||
@@ -427,14 +436,12 @@ text_sensor:
|
||||
on_value:
|
||||
then:
|
||||
- lambda: |-
|
||||
// Check only the first character for mode
|
||||
char c = x.c_str()[0];
|
||||
if (c == 'T') { // "TIMER"
|
||||
if (c == 'T') {
|
||||
id(operation_mode) = 2;
|
||||
} else if (c == 'O') { // "ON" or "OFF"
|
||||
// second letter N->ON, F->OFF
|
||||
} else if (c == 'O') {
|
||||
id(operation_mode) = (x.size() > 1 && x[1] == 'N') ? 1 : 0;
|
||||
} else if (c == 'B') { // "BOOST"
|
||||
} else if (c == 'B') {
|
||||
id(operation_mode) = 3;
|
||||
id(boost_timer) = 0;
|
||||
} else {
|
||||
@@ -442,13 +449,10 @@ text_sensor:
|
||||
}
|
||||
- script.execute: evaluate_relay_state
|
||||
|
||||
######################################################
|
||||
# Expose the current operation mode (OFF, ON, TIMER, BOOST)
|
||||
######################################################
|
||||
- platform: template
|
||||
name: "Operation Mode State"
|
||||
lambda: |-
|
||||
// 0=OFF, 1=ON, 2=TIMER, 3=BOOST
|
||||
switch (id(operation_mode)) {
|
||||
case 0: return {"OFF"};
|
||||
case 1: return {"ON"};
|
||||
@@ -458,66 +462,54 @@ text_sensor:
|
||||
}
|
||||
update_interval: 5s
|
||||
|
||||
######################################################
|
||||
# Expose the "Morning On" time as a text (HH:MM)
|
||||
######################################################
|
||||
- platform: template
|
||||
name: "Timeclock: Morning On Time"
|
||||
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) };
|
||||
update_interval: "${update_interval}"
|
||||
|
||||
######################################################
|
||||
# Expose the "Morning Off" time as a text (HH:MM)
|
||||
######################################################
|
||||
- platform: template
|
||||
name: "Timeclock: Morning Off Time"
|
||||
lambda: |-
|
||||
int hour = id(morning_off) / 60;
|
||||
int minute = id(morning_off) % 60;
|
||||
// Increase to 16 for safety
|
||||
char buff[16];
|
||||
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
|
||||
return { std::string(buff) };
|
||||
update_interval: "${update_interval}"
|
||||
|
||||
######################################################
|
||||
# Expose the "Evening On" time as a text (HH:MM)
|
||||
######################################################
|
||||
- platform: template
|
||||
name: "Timeclock: Evening On Time"
|
||||
lambda: |-
|
||||
int hour = id(evening_on) / 60;
|
||||
int minute = id(evening_on) % 60;
|
||||
// Increase to 16 for safety
|
||||
char buff[16];
|
||||
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
|
||||
return { std::string(buff) };
|
||||
update_interval: "${update_interval}"
|
||||
|
||||
######################################################
|
||||
# Expose the "Evening Off" time as a text (HH:MM)
|
||||
######################################################
|
||||
- platform: template
|
||||
name: "Timeclock: Evening Off Time"
|
||||
lambda: |-
|
||||
int hour = id(evening_off) / 60;
|
||||
int minute = id(evening_off) % 60;
|
||||
// Increase to 16 for safety
|
||||
char buff[16];
|
||||
snprintf(buff, sizeof(buff), "%02d:%02d", hour, minute);
|
||||
return { std::string(buff) };
|
||||
update_interval: "${update_interval}"
|
||||
|
||||
##########################################################################################
|
||||
# BINARY SENSORS
|
||||
#:########################################################################################:#
|
||||
# BINARY SENSORS:
|
||||
# https://esphome.io/components/binary_sensor/
|
||||
##########################################################################################
|
||||
#:########################################################################################:#
|
||||
binary_sensor:
|
||||
- platform: gpio
|
||||
pin:
|
||||
@@ -534,11 +526,9 @@ binary_sensor:
|
||||
then:
|
||||
- lambda: |-
|
||||
if (id(relay).state) {
|
||||
// Relay is ON: turn it OFF and set mode to 0 (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;
|
||||
}
|
||||
@@ -548,16 +538,16 @@ binary_sensor:
|
||||
lambda: |-
|
||||
return id(relay).state;
|
||||
|
||||
##########################################################################################
|
||||
# Sensors
|
||||
# https://esphome.io/components/text_sensor/index.html
|
||||
##########################################################################################
|
||||
#:########################################################################################:#
|
||||
# SENSORS:
|
||||
# https://esphome.io/components/sensor/
|
||||
#:########################################################################################:#
|
||||
sensor:
|
||||
- platform: template
|
||||
name: "Timeclock: Boost Duration"
|
||||
id: boost_duration_time
|
||||
unit_of_measurement: "mins"
|
||||
accuracy_decimals: "0"
|
||||
accuracy_decimals: 0
|
||||
update_interval: "${update_interval}"
|
||||
lambda: |-
|
||||
return id(boost_duration);
|
||||
@@ -566,9 +556,9 @@ sensor:
|
||||
name: "Mins from Midnight"
|
||||
id: mins_from_midnight
|
||||
unit_of_measurement: "mins"
|
||||
accuracy_decimals: "0"
|
||||
accuracy_decimals: 0
|
||||
update_interval: "${update_interval}"
|
||||
internal: True # No need to show this in Home Assistant
|
||||
internal: true
|
||||
lambda: |-
|
||||
return id(current_mins);
|
||||
|
||||
@@ -578,41 +568,35 @@ sensor:
|
||||
id: timer_minutes_remaining
|
||||
unit_of_measurement: "Mins"
|
||||
update_interval: 5s
|
||||
accuracy_decimals: "0"
|
||||
accuracy_decimals: 0
|
||||
lambda: |-
|
||||
// always zero if relay is off
|
||||
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) {
|
||||
rem = id(boost_duration) - id(boost_timer);
|
||||
}
|
||||
// never return negative
|
||||
return rem > 0 ? rem : 0;
|
||||
|
||||
#################################################################################################
|
||||
# BUTTON COMPONENT
|
||||
# https://esphome.io/components/button/index.html
|
||||
#################################################################################################
|
||||
#:########################################################################################:#
|
||||
# BUTTON COMPONENT:
|
||||
# https://esphome.io/components/button/
|
||||
#:########################################################################################:#
|
||||
button:
|
||||
- platform: template
|
||||
name: "Boost now"
|
||||
id: boost_button
|
||||
icon: "mdi:play-circle-outline"
|
||||
on_press:
|
||||
# 1) reset BOOST timer and set mode
|
||||
- lambda: |-
|
||||
id(boost_timer) = 0;
|
||||
id(operation_mode) = 3;
|
||||
# 2) immediately re-evaluate relay state
|
||||
- script.execute: evaluate_relay_state
|
||||
|
||||
- platform: template
|
||||
@@ -621,25 +605,22 @@ button:
|
||||
icon: "mdi:restore"
|
||||
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};
|
||||
// Reset mode to TIMER and clear any running boost
|
||||
id(operation_mode)= 2;
|
||||
id(boost_timer) = 0;
|
||||
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(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
|
||||
|
||||
#################################################################################################
|
||||
# SELECT COMPONENT
|
||||
# https://esphome.io/components/select/index.html
|
||||
#################################################################################################
|
||||
#:########################################################################################:#
|
||||
# SELECT COMPONENT:
|
||||
# https://esphome.io/components/select/
|
||||
#:########################################################################################:#
|
||||
select:
|
||||
- platform: template
|
||||
name: "Operation Mode"
|
||||
@@ -650,8 +631,6 @@ select:
|
||||
- "ON"
|
||||
- "TIMER"
|
||||
- "BOOST"
|
||||
|
||||
# show the current mode
|
||||
lambda: |-
|
||||
switch (id(operation_mode)) {
|
||||
case 1: return std::string("ON");
|
||||
@@ -659,43 +638,37 @@ select:
|
||||
case 3: return std::string("BOOST");
|
||||
default: return std::string("OFF");
|
||||
}
|
||||
|
||||
# when changed in HA, set mode & re-evaluate
|
||||
set_action:
|
||||
- lambda: |-
|
||||
if (x == "OFF") { id(operation_mode) = 0; }
|
||||
else if (x == "ON") { id(operation_mode) = 1; }
|
||||
else if (x == "TIMER") { id(operation_mode) = 2; }
|
||||
else { // BOOST
|
||||
else {
|
||||
id(boost_timer) = 0;
|
||||
id(operation_mode) = 3;
|
||||
}
|
||||
- script.execute: evaluate_relay_state
|
||||
|
||||
#################################################################################################
|
||||
# SCRIPT COMPONENT
|
||||
#:########################################################################################:#
|
||||
# SCRIPT COMPONENT:
|
||||
# https://esphome.io/components/script.html
|
||||
#################################################################################################
|
||||
# Script: evaluate and drive the relay
|
||||
#:########################################################################################:#
|
||||
script:
|
||||
- id: evaluate_relay_state
|
||||
then:
|
||||
- lambda: |-
|
||||
int mode = id(operation_mode);
|
||||
|
||||
// BOOST just forces the relay on
|
||||
if (mode == 3) {
|
||||
id(relay).turn_on();
|
||||
return;
|
||||
}
|
||||
|
||||
// OFF -> always off
|
||||
if (mode == 0) {
|
||||
id(relay).turn_off();
|
||||
return;
|
||||
}
|
||||
|
||||
// ON -> always on
|
||||
if (mode == 1) {
|
||||
id(relay).turn_on();
|
||||
return;
|
||||
@@ -705,14 +678,12 @@ script:
|
||||
{
|
||||
bool should_on = false;
|
||||
|
||||
// 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;
|
||||
@@ -723,16 +694,15 @@ script:
|
||||
else id(relay).turn_off();
|
||||
}
|
||||
|
||||
#################################################################################################
|
||||
# INTERVAL COMPONENT
|
||||
#:########################################################################################:#
|
||||
# INTERVAL COMPONENT:
|
||||
# https://esphome.io/components/interval.html
|
||||
#################################################################################################
|
||||
# Interval: bumps time (even if no SNTP), then calls the script to evaluate relay state
|
||||
#:########################################################################################:#
|
||||
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;
|
||||
@@ -741,12 +711,11 @@ 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)) {
|
||||
id(operation_mode) = 2;
|
||||
//id(mqtt_client).publish("${mqtt_timer_topic}/operation", "TIMER");
|
||||
}
|
||||
}
|
||||
- script.execute: evaluate_relay_state
|
||||
- script.execute: evaluate_relay_state
|
||||
Reference in New Issue
Block a user