various esphome changes
This commit is contained in:
@@ -3,21 +3,22 @@
|
||||
# MASTER BATHROOM HEATED TOWEL RAIL
|
||||
# Controlled by a Sonoff Basic
|
||||
#
|
||||
# V1.1 2025-04-12 Fixes to timers and offline modes
|
||||
# V1.0 2025-02-14 Initial Version
|
||||
#
|
||||
# INSTRUCTIONS
|
||||
# - It allows a heated towel rail device to work in a standalone operation
|
||||
# - On startup, it will turn on for 2 hours then go into timer mode (this allows you to just turn it on to get some heat immediately)
|
||||
# - On startup, it will turn on for (startup_duration) hours then go into timer mode (this allows you to just turn it on to get some heat immediately)
|
||||
# - The timer has a morning and evening time (but no weekday/weekend setting)
|
||||
# - Default values are 5am-7am and 9pm-Midnight (as this suits our use case)
|
||||
# - 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 4 on/off times via MQTT (See below commands)
|
||||
# - You can set 4 modes ON/OFF/TIMER/STARTUP via MQTT
|
||||
# - You can set 4 modes ON/OFF/TIMER/STARTUP via MQTT. That way, you can set to STARTUP for a short boost
|
||||
# - Any new timer times set via MQTT will be remembered though a reboot
|
||||
# - On a reboot, the device will always turn on for the Startup Duration (STARTUP mode, default 2 hours)
|
||||
# - TIMER mode will always be switched on after startup mode is complete
|
||||
# - If you need it ON continuously with no MQTT, toggle power ON/OFF 4 times within 20 seconds (with ~2 secs in between to allow it to boot)
|
||||
# - 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
|
||||
@@ -29,49 +30,61 @@
|
||||
# mqtt_timer_topic/operation/ON : Towel rail permanently on
|
||||
# mqtt_timer_topic/operation/OFF : Towel rail permanently off
|
||||
# mqtt_timer_topic/operation/TIMER : Towel rail will obey timer settings
|
||||
# mqtt_timer_topic/operation/STARTUP : Turn on for 2 hours then TIMER (also on startup)
|
||||
# mqtt_timer_topic/operation/STARTUP : Turn on for (startup_duration) hours then TIMER (also on startup)
|
||||
#
|
||||
#############################################
|
||||
#############################################
|
||||
|
||||
#############################################
|
||||
# VARIABLE SUBSTITUTIONS
|
||||
# Give the device a useful name & description here
|
||||
# and change values accordingly.
|
||||
#############################################
|
||||
substitutions:
|
||||
|
||||
#############################################
|
||||
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
|
||||
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
|
||||
#############################################
|
||||
|
||||
mqtt_timer_topic: "viewroad-commands/masterbath-towelrail" # Topics you will use to change stuff
|
||||
startup_duration: "120" # Minutes to stay ON in STARTUP mode before reverting to TIMER
|
||||
timezone: "Pacific/Auckland" # For setting clock with snmp
|
||||
|
||||
devicename: "esp-masterbathtowelrail"
|
||||
friendly_name: "Master Bathroom Towelrail"
|
||||
description_comment: "Sonoff Basic controlling ON/OFF/Timer for the Heated Towel Rail in the Master Bathroom"
|
||||
|
||||
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
|
||||
|
||||
api_key: !secret esp-masterbathtowelrail_api_key # unfortunately you can't use substitutions inside secrets names
|
||||
ota_pass: !secret esp-masterbathtowelrail_ota_pass # unfortunately you can't use substitutions inside secrets names
|
||||
wifi_ssid: !secret wifi_ssid
|
||||
wifi_password: !secret wifi_password
|
||||
fallback_ap_password: !secret fallback_ap_password
|
||||
|
||||
# Add these if we are giving it a static ip, or remove them in the Wifi section
|
||||
#static_ip_address: !secret esp-occupancyoffice_static_ip
|
||||
#static_ip_gateway: !secret esp-occupancyoffice_gateway
|
||||
#static_ip_subnet: !secret esp-occupancyoffice_subnet
|
||||
static_ip_address: !secret esp-masterbathtowelrail_ip
|
||||
|
||||
mqtt_server: !secret mqtt_server
|
||||
mqtt_username: !secret mqtt_username
|
||||
mqtt_password: !secret mqtt_password
|
||||
mqtt_topic: "esphome" #main topic for the mqtt server, call it what you like
|
||||
update_interval: "60s" # Update time for for general sensors etc.
|
||||
|
||||
# Add these if we are using the internal web server (this is pretty processor intensive)
|
||||
#web_server_username: !secret web_server_username
|
||||
#web_server_password: !secret web_server_password
|
||||
#############################################
|
||||
# SYSTEM SPECIFIC VARIABLE SUBSTITUTIONS
|
||||
#############################################
|
||||
|
||||
update_interval: 60s # update time for for general sensors etc
|
||||
timezone: "Pacific/Auckland"
|
||||
sntp_update_interval: 6h # Set the duration between the sntp service polling
|
||||
# Network time servers https://www.ntppool.org/zone/@
|
||||
# Make sure you have some DNS, or use IP addresses only here.
|
||||
sntp_server_1: !secret ntp_server_1
|
||||
sntp_server_2: !secret ntp_server_2
|
||||
sntp_server_3: !secret ntp_server_3
|
||||
|
||||
#############################################
|
||||
# Included Common Packages
|
||||
# https://esphome.io/components/esphome.html
|
||||
#############################################
|
||||
packages:
|
||||
common_wifi: !include
|
||||
file: common/network_common.yaml
|
||||
vars:
|
||||
local_static_ip_address: ${static_ip_address}
|
||||
local_api_key: ${api_key}
|
||||
local_ota_pass: ${ota_pass}
|
||||
common_mqtt: !include
|
||||
file: common/mqtt_common.yaml
|
||||
common_general_sensors: !include
|
||||
file: common/sensors_common.yaml
|
||||
vars:
|
||||
local_friendly_name: ${friendly_name}
|
||||
local_update_interval: ${update_interval}
|
||||
|
||||
#############################################
|
||||
# ESPHome
|
||||
@@ -80,13 +93,13 @@ substitutions:
|
||||
esphome:
|
||||
name: ${devicename}
|
||||
friendly_name: ${friendly_name}
|
||||
comment: ${description_comment} #a ppears on the esphome page in HA
|
||||
comment: ${description_comment} # Appears on the esphome page in HA
|
||||
on_boot:
|
||||
priority: 900 # high priority to run after globals are initialized
|
||||
priority: 900 # High priority to run after globals are initialized
|
||||
then:
|
||||
- lambda: |-
|
||||
// 1) Figure out the current time in "seconds from midnight"
|
||||
// using SNTP if available, otherwise fallback_time * 60.
|
||||
// using SNTP if available, otherwise current_mins * 60.
|
||||
bool have_sntp = id(sntp_time).now().is_valid();
|
||||
int current_time_s = 0;
|
||||
|
||||
@@ -94,15 +107,15 @@ esphome:
|
||||
auto now = id(sntp_time).now();
|
||||
current_time_s = now.hour * 3600 + now.minute * 60 + now.second;
|
||||
} else {
|
||||
// fallback_time is in minutes; convert to seconds
|
||||
current_time_s = id(fallback_time) * 60;
|
||||
// current_mins is in minutes; convert to seconds
|
||||
current_time_s = id(current_mins) * 60;
|
||||
}
|
||||
|
||||
// 2) Compare with the last boot time
|
||||
int diff = current_time_s - id(last_boot_time_s);
|
||||
|
||||
// If within 20 seconds, increment boot_count; otherwise reset to 1
|
||||
if (diff >= 0 && diff <= 20) {
|
||||
// If within 30 seconds, increment boot_count; otherwise reset to 1
|
||||
if (diff >= 0 && diff <= 30) {
|
||||
id(boot_count)++;
|
||||
} else {
|
||||
id(boot_count) = 1;
|
||||
@@ -134,58 +147,11 @@ esp8266:
|
||||
# https://esphome.io/components/logger.html
|
||||
#############################################
|
||||
logger:
|
||||
level: INFO #INFO Level suggested, or DEBUG for testing
|
||||
level: DEBUG #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
|
||||
|
||||
#############################################
|
||||
# Enable the Home Assistant API
|
||||
# https://esphome.io/components/api.html
|
||||
#############################################
|
||||
api:
|
||||
encryption:
|
||||
key: ${api_key}
|
||||
|
||||
#############################################
|
||||
# Enable Over the Air Update Capability
|
||||
# https://esphome.io/components/ota.html?highlight=ota
|
||||
#############################################
|
||||
ota:
|
||||
- platform: esphome
|
||||
password: ${ota_pass}
|
||||
|
||||
#############################################
|
||||
# Safe Mode
|
||||
# Safe mode will detect boot loops
|
||||
# https://esphome.io/components/safe_mode
|
||||
#############################################
|
||||
safe_mode:
|
||||
|
||||
#############################################
|
||||
# Wifi Settings
|
||||
# https://esphome.io/components/wifi.html
|
||||
#
|
||||
# Power Save mode (can reduce wifi reliability)
|
||||
# NONE (least power saving, Default for ESP8266)
|
||||
# LIGHT (Default for ESP32)
|
||||
# HIGH (most power saving)
|
||||
#############################################
|
||||
wifi:
|
||||
ssid: ${wifi_ssid}
|
||||
password: ${wifi_password}
|
||||
#power_save_mode: LIGHT # https://esphome.io/components/wifi.html#wifi-power-save-mode
|
||||
#manual_ip: # optional static IP address
|
||||
#static_ip: ${static_ip_address}
|
||||
#gateway: ${static_ip_gateway}
|
||||
#subnet: ${static_ip_subnet}
|
||||
ap: # Details for fallback hotspot in case wifi connection fails https://esphome.io/components/wifi.html#access-point-mode
|
||||
ssid: ${devicename} AP
|
||||
password: ${fallback_ap_password}
|
||||
ap_timeout: 30min # Time until it brings up fallback AP. default is 1min
|
||||
|
||||
captive_portal: # extra fallback mechanism for when connecting if the configured WiFi fails
|
||||
|
||||
#############################################
|
||||
# Real time clock time source for ESPHome
|
||||
# If it's invalid, we fall back to an internal clock
|
||||
@@ -195,19 +161,30 @@ captive_portal: # extra fallback mechanism for when connecting if the configure
|
||||
time:
|
||||
- platform: sntp
|
||||
id: sntp_time
|
||||
|
||||
#############################################
|
||||
# MQTT Monitoring
|
||||
# https://esphome.io/components/mqtt.html?highlight=mqtt
|
||||
# MUST also have api enabled if you enable MQTT
|
||||
#############################################
|
||||
mqtt:
|
||||
broker: ${mqtt_server}
|
||||
topic_prefix: ${mqtt_topic}/${devicename}
|
||||
username: ${mqtt_username}
|
||||
password: ${mqtt_password}
|
||||
#discovery: True # enable entity discovery (true is default)
|
||||
#discover_ip: True # enable device discovery (true is default)
|
||||
# Define the timezone of the device
|
||||
timezone: "${timezone}"
|
||||
# Change sync interval from default 5min to 6 hours (or as set in substitutions)
|
||||
update_interval: "${sntp_update_interval}"
|
||||
# Set specific sntp servers to use
|
||||
servers:
|
||||
- "${sntp_server_1}"
|
||||
- "${sntp_server_2}"
|
||||
- "${sntp_server_3}"
|
||||
# Publish the time the device was last restarted
|
||||
on_time_sync:
|
||||
then:
|
||||
- logger.log: "Synchronised sntp clock"
|
||||
- text_sensor.template.publish:
|
||||
id: time_sync
|
||||
state: "SNTP clock Syncd"
|
||||
# Update last restart time, but only once.
|
||||
- if:
|
||||
condition:
|
||||
lambda: 'return id(device_last_restart).state == "";'
|
||||
then:
|
||||
- text_sensor.template.publish:
|
||||
id: device_last_restart
|
||||
state: !lambda 'return id(sntp_time).now().strftime("%a %d %b %Y - %I:%M:%S %p");'
|
||||
|
||||
#############################################
|
||||
# Global Variables for use in automations etc
|
||||
@@ -221,7 +198,7 @@ globals:
|
||||
restore_value: true
|
||||
initial_value: "0"
|
||||
|
||||
# Counts how many consecutive boots have occurred within 10 seconds
|
||||
# Counts how many consecutive boots have occurred (within X seconds)
|
||||
- id: boot_count
|
||||
type: int
|
||||
restore_value: true
|
||||
@@ -249,11 +226,11 @@ globals:
|
||||
initial_value: "1260"
|
||||
|
||||
# Evening Off time (minutes from midnight),
|
||||
# default 00:00 => 0 => treat as midnight
|
||||
# default 24:00 => 1440 => treat as midnight
|
||||
- id: evening_off
|
||||
type: int
|
||||
restore_value: true
|
||||
initial_value: "0"
|
||||
initial_value: "1440"
|
||||
|
||||
####################################################
|
||||
# operation_mode:
|
||||
@@ -268,15 +245,15 @@ globals:
|
||||
initial_value: "3"
|
||||
|
||||
####################################################
|
||||
# fallback_time is used if SNTP is invalid.
|
||||
# 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.
|
||||
# Not restored, so it resets each boot.
|
||||
####################################################
|
||||
- id: fallback_time
|
||||
- id: current_mins
|
||||
type: int
|
||||
restore_value: false
|
||||
initial_value: "720"
|
||||
initial_value: "720" # 720 is 12:00 Noon
|
||||
|
||||
####################################################
|
||||
# startup_timer: counts minutes in STARTUP mode
|
||||
@@ -288,6 +265,7 @@ globals:
|
||||
restore_value: false
|
||||
initial_value: "0"
|
||||
|
||||
|
||||
#############################################
|
||||
# Text Sensors
|
||||
# https://esphome.io/components/text_sensor/index.html
|
||||
@@ -297,7 +275,6 @@ text_sensor:
|
||||
############################
|
||||
# MQTT Subscriptions
|
||||
############################
|
||||
|
||||
####################################################
|
||||
# Subscribe to the Morning On time, format "HH:MM"
|
||||
# We check x.size() == 5 and x[2] == ':',
|
||||
@@ -305,9 +282,9 @@ text_sensor:
|
||||
# std::string uses 'substr', not 'substring'.
|
||||
####################################################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Morning On Time"
|
||||
name: "Morning On Time Setting"
|
||||
id: morning_on_topic
|
||||
topic: "${mqtt_timer_topic}/morning-on"
|
||||
topic: "${mqtt_timer_topic}/morning-on" # Stored in the format HH:MM
|
||||
internal: True
|
||||
on_value:
|
||||
then:
|
||||
@@ -321,14 +298,13 @@ text_sensor:
|
||||
} else {
|
||||
ESP_LOGW("timer","Invalid Morning On format: %s", x.c_str());
|
||||
}
|
||||
|
||||
####################################################
|
||||
# Morning Off time => "HH:MM"
|
||||
####################################################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Morning Off Time"
|
||||
name: "Morning Off Time Setting"
|
||||
id: morning_off_topic
|
||||
topic: "${mqtt_timer_topic}/morning-off"
|
||||
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
|
||||
on_value:
|
||||
then:
|
||||
@@ -341,14 +317,13 @@ text_sensor:
|
||||
} else {
|
||||
ESP_LOGW("timer","Invalid Morning Off format: %s", x.c_str());
|
||||
}
|
||||
|
||||
####################################################
|
||||
# Evening On time => "HH:MM"
|
||||
####################################################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Evening On Time"
|
||||
name: "Evening On Time Setting"
|
||||
id: evening_on_topic
|
||||
topic: "${mqtt_timer_topic}/evening-on"
|
||||
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
|
||||
on_value:
|
||||
then:
|
||||
@@ -361,14 +336,13 @@ text_sensor:
|
||||
} else {
|
||||
ESP_LOGW("timer","Invalid Evening On format: %s", x.c_str());
|
||||
}
|
||||
|
||||
####################################################
|
||||
# Evening Off time => "HH:MM"
|
||||
####################################################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Evening Off Time"
|
||||
name: "Evening Off Time Setting"
|
||||
id: evening_off_topic
|
||||
topic: "${mqtt_timer_topic}/evening-off"
|
||||
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
|
||||
on_value:
|
||||
then:
|
||||
@@ -381,7 +355,6 @@ text_sensor:
|
||||
} else {
|
||||
ESP_LOGW("timer","Invalid Evening Off format: %s", x.c_str());
|
||||
}
|
||||
|
||||
####################################################
|
||||
# Subscribe to operation mode:
|
||||
# OFF, ON, TIMER, STARTUP
|
||||
@@ -389,9 +362,9 @@ text_sensor:
|
||||
# (Requires <strings.h> typically included in ESPHome)
|
||||
####################################################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Timer Operation Mode"
|
||||
name: "Operation Mode Setting"
|
||||
id: timer_operation_mode_topic
|
||||
topic: "${mqtt_timer_topic}/operation"
|
||||
topic: "${mqtt_timer_topic}/operation" # STARTUP,ON,OFF,TIMER
|
||||
internal: True # No need to show this in Home Assistant as there is a sensor that shows the set value
|
||||
on_value:
|
||||
then:
|
||||
@@ -412,7 +385,7 @@ text_sensor:
|
||||
ESP_LOGI("timer","Operation mode set to OFF");
|
||||
} else if (strcasecmp(x.c_str(), "STARTUP") == 0) {
|
||||
id(operation_mode) = 3;
|
||||
id(startup_timer) = 0;
|
||||
id(startup_timer) = 0; // Reset the startup timer to zero
|
||||
ESP_LOGI("timer","Operation mode set to STARTUP");
|
||||
} else {
|
||||
ESP_LOGW("timer","Invalid operation mode: %s", x.c_str());
|
||||
@@ -438,7 +411,7 @@ text_sensor:
|
||||
# Expose the "Morning On" time as a text (HH:MM)
|
||||
######################################################
|
||||
- platform: template
|
||||
name: "Morning On Time State"
|
||||
name: "Timeclock: Morning On Time"
|
||||
lambda: |-
|
||||
int hour = id(morning_on) / 60;
|
||||
int minute = id(morning_on) % 60;
|
||||
@@ -452,7 +425,7 @@ text_sensor:
|
||||
# Expose the "Morning Off" time as a text (HH:MM)
|
||||
######################################################
|
||||
- platform: template
|
||||
name: "Morning Off Time State"
|
||||
name: "Timeclock: Morning Off Time"
|
||||
lambda: |-
|
||||
int hour = id(morning_off) / 60;
|
||||
int minute = id(morning_off) % 60;
|
||||
@@ -467,7 +440,7 @@ text_sensor:
|
||||
# Expose the "Evening On" time as a text (HH:MM)
|
||||
######################################################
|
||||
- platform: template
|
||||
name: "Evening On Time State"
|
||||
name: "Timeclock: Evening On Time"
|
||||
lambda: |-
|
||||
int hour = id(evening_on) / 60;
|
||||
int minute = id(evening_on) % 60;
|
||||
@@ -482,7 +455,7 @@ text_sensor:
|
||||
# Expose the "Evening Off" time as a text (HH:MM)
|
||||
######################################################
|
||||
- platform: template
|
||||
name: "Evening Off Time State"
|
||||
name: "Timeclock: Evening Off Time"
|
||||
lambda: |-
|
||||
int hour = id(evening_off) / 60;
|
||||
int minute = id(evening_off) % 60;
|
||||
@@ -494,25 +467,45 @@ text_sensor:
|
||||
update_interval: ${update_interval}
|
||||
|
||||
######################################################
|
||||
# ESPHome Info
|
||||
# Creates a sensor showing when the device was last restarted
|
||||
# Uptime template sensor, and SNTP are needed
|
||||
######################################################
|
||||
- platform: version
|
||||
name: ${friendly_name} Version
|
||||
- platform: wifi_info
|
||||
ip_address:
|
||||
name: ${friendly_name} IP Address
|
||||
- platform: template
|
||||
name: "Last Restart: ${friendly_name}"
|
||||
id: device_last_restart
|
||||
update_interval: ${update_interval}
|
||||
icon: mdi:clock
|
||||
entity_category: diagnostic
|
||||
|
||||
- platform: template
|
||||
name: "Time Sync Status"
|
||||
id: time_sync
|
||||
update_interval: ${update_interval}
|
||||
entity_category: diagnostic
|
||||
|
||||
- platform: template
|
||||
name: "Internal Time"
|
||||
id: time_text
|
||||
update_interval: ${update_interval}
|
||||
entity_category: diagnostic
|
||||
lambda: |-
|
||||
auto time_text = id(sntp_time).now().strftime("%H:%M:%S - %d-%m-%Y");
|
||||
return { time_text };
|
||||
|
||||
#############################################
|
||||
# General Sensors
|
||||
# https://esphome.io/components/sensor/index.html
|
||||
# Sensors
|
||||
# https://esphome.io/components/text_sensor/index.html
|
||||
#############################################
|
||||
sensor:
|
||||
- platform: uptime # Uptime for this device
|
||||
name: ${friendly_name} Uptime
|
||||
update_interval: ${update_interval}
|
||||
- platform: wifi_signal # Wifi Strength
|
||||
name: ${friendly_name} Wifi Signal
|
||||
update_interval: ${update_interval}
|
||||
- platform: template
|
||||
name: "Mins from Midnight"
|
||||
unit_of_measurement: "mins"
|
||||
accuracy_decimals: 0
|
||||
update_interval: ${update_interval}
|
||||
internal: True
|
||||
lambda: |-
|
||||
return id(current_mins);
|
||||
|
||||
|
||||
####################################################
|
||||
# Relay Switch (Sonoff Basic Relay on GPIO12)
|
||||
@@ -528,9 +521,27 @@ switch:
|
||||
# Check every minute to decide relay state
|
||||
####################################################
|
||||
interval:
|
||||
- interval: ${update_interval}
|
||||
- interval: "1min" # Must be 1min as this is used to calculate times
|
||||
then:
|
||||
- lambda: |-
|
||||
|
||||
// Do we have correct time from SNTP? If not...
|
||||
if (!id(time_sync).has_state()) {
|
||||
// Set minutes since midnight
|
||||
id(current_mins) = id(current_mins) +1 ;
|
||||
|
||||
// wrap around at 1440 => next day
|
||||
if (id(current_mins) >= 1440) {
|
||||
id(current_mins) = 0;
|
||||
}
|
||||
|
||||
// If we do have proper SNMP time...
|
||||
} else {
|
||||
// Use real time from SNTP
|
||||
auto now = id(sntp_time).now();
|
||||
id(current_mins) = now.hour * 60 + now.minute;
|
||||
}
|
||||
|
||||
// operation_mode:
|
||||
// 0 = OFF
|
||||
// 1 = ON
|
||||
@@ -543,7 +554,7 @@ interval:
|
||||
// minutes, then automatically revert to TIMER.
|
||||
//////////////////////////////////////////////////
|
||||
if (mode == 3) {
|
||||
id(startup_timer)++;
|
||||
id(startup_timer) = id(startup_timer) + 1 ; // works as long as update_interval in seconds
|
||||
// Compare with the substitution startup_duration
|
||||
if (id(startup_timer) < (int) ${startup_duration}) {
|
||||
// Still within the STARTUP period => turn relay on
|
||||
@@ -551,8 +562,10 @@ interval:
|
||||
} else {
|
||||
// After 'startup_duration' minutes => switch to TIMER
|
||||
id(operation_mode) = 2;
|
||||
id(mqtt_client).publish("${mqtt_timer_topic}/operation", "TIMER");
|
||||
}
|
||||
// Skip the rest of the logic
|
||||
ESP_LOGI("startup_timer", "startup_timer=%d", id(startup_timer));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -574,50 +587,26 @@ interval:
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// TIMER MODE => follow morning/evening schedule
|
||||
// using SNTP if valid, else fallback_time
|
||||
// using SNTP if valid, else current_mins
|
||||
//////////////////////////////////////////////////
|
||||
if (mode == 2) {
|
||||
auto now = id(sntp_time).now();
|
||||
bool have_sntp = now.is_valid();
|
||||
|
||||
int current_mins;
|
||||
if (!have_sntp) {
|
||||
// SNTP not available => fallback clock
|
||||
current_mins = id(fallback_time);
|
||||
// increment the fallback clock by 1 minute
|
||||
id(fallback_time) += 1;
|
||||
// wrap around at 1440 => next day
|
||||
if (id(fallback_time) >= 1440) {
|
||||
id(fallback_time) = 0;
|
||||
}
|
||||
} else {
|
||||
// Use real time from SNTP
|
||||
current_mins = now.hour * 60 + now.minute;
|
||||
}
|
||||
if (mode == 2)
|
||||
{
|
||||
|
||||
bool should_on = false;
|
||||
|
||||
// If evening_off == 0 => treat as midnight => 1440
|
||||
int evening_off_local = id(evening_off);
|
||||
if (evening_off_local == 0) {
|
||||
evening_off_local = 1440;
|
||||
}
|
||||
|
||||
// Check morning window
|
||||
// Example: morning_on=360 => 06:00, morning_off=480 => 08:00
|
||||
// If current_mins in [360..480), should_on = true
|
||||
if (id(morning_on) < id(morning_off)) {
|
||||
if (current_mins >= id(morning_on) && current_mins < id(morning_off)) {
|
||||
should_on = true;
|
||||
}
|
||||
if (id(current_mins) >= id(morning_on) && id(current_mins) < id(morning_off) )
|
||||
{
|
||||
should_on = true;
|
||||
}
|
||||
|
||||
// Check evening window
|
||||
// Example: evening_on=540 => 09:00, evening_off=1440 => midnight
|
||||
if (id(evening_on) < evening_off_local) {
|
||||
if (current_mins >= id(evening_on) && current_mins < evening_off_local) {
|
||||
should_on = true;
|
||||
}
|
||||
// Example: evening_on=1260 => 21:00, evening_off=1440 => midnight
|
||||
if (id(current_mins) >= id(evening_on) && id(current_mins) < id(evening_off) )
|
||||
{
|
||||
should_on = true;
|
||||
}
|
||||
|
||||
// Final relay state based on schedule
|
||||
@@ -626,4 +615,8 @@ interval:
|
||||
} else {
|
||||
id(relay).turn_off();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user