pool light esp, gas heater esp, HA Overview display
This commit is contained in:
+341
-34
@@ -6,6 +6,8 @@
|
||||
# dashboard_import:
|
||||
# package_import_url: github://athom-tech/esp32-configs/athom-smart-plug.yaml
|
||||
#
|
||||
# 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)
|
||||
# V2.3 2025-06-15 Changed to an Athom V3 (esp32)
|
||||
# V2.2 2025-06-14 Fixes to offline time when sntp/network unavailable
|
||||
@@ -62,7 +64,7 @@ substitutions:
|
||||
|
||||
# Project Naming
|
||||
project_name: "Athom Technology.Smart Plug V1" # Project Details
|
||||
project_version: "v2.4" # Project V denotes release of yaml file, allowing checking of deployed vs latest version
|
||||
project_version: "v3.0" # 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
|
||||
@@ -83,6 +85,18 @@ substitutions:
|
||||
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)
|
||||
##############################################################################
|
||||
max_modes: "16" # total number of modes in the cycle
|
||||
mode_pulse_off: "400ms" # short OFF between quick pulses
|
||||
mode_pulse_on: "400ms" # short ON after each quick pulse
|
||||
mode_reset_off: "3s" # long OFF to force hardware reset to mode 1
|
||||
mode_reset_window_ms: "3000" # treat OFF>=this ms as a reset-to-1 when turning back ON
|
||||
mode_prime_on: "800ms" # if relay is OFF when a color is selected, turn ON for this long before pulsing
|
||||
# Ignored modes bitmask: bit (mode_index-1). For 4 and 5 ignored -> (1<<3)|(1<<4) = 24.
|
||||
ignored_modes_mask: "0"
|
||||
|
||||
##########################################################################################
|
||||
# PACKAGES: Included Common Packages
|
||||
# https://esphome.io/components/packages.html
|
||||
@@ -249,6 +263,57 @@ globals:
|
||||
restore_value: true
|
||||
initial_value: "0"
|
||||
|
||||
##############################################################################
|
||||
# POOL LIGHT MODE CONTROL GLOBALS (new)
|
||||
##############################################################################
|
||||
# Current assumed pool-light mode (0..max_modes). 0 means relay OFF.
|
||||
- id: current_mode
|
||||
type: int
|
||||
restore_value: false
|
||||
initial_value: "1"
|
||||
|
||||
# Total modes in the cycle (edit in substitutions).
|
||||
- id: max_modes
|
||||
type: int
|
||||
restore_value: false
|
||||
initial_value: "${max_modes}"
|
||||
|
||||
# Flag while we are actively pulsing to change modes (prevents scheduler interference).
|
||||
- id: mode_changing
|
||||
type: bool
|
||||
restore_value: false
|
||||
initial_value: "false"
|
||||
|
||||
# Timestamp of last relay off (for delta measurement).
|
||||
- id: last_off_ms
|
||||
type: uint32_t
|
||||
restore_value: false
|
||||
initial_value: "0"
|
||||
|
||||
# Millisecond threshold that separates "quick pulse" vs "reset to mode 1".
|
||||
- id: reset_window_ms
|
||||
type: int
|
||||
restore_value: false
|
||||
initial_value: "${mode_reset_window_ms}"
|
||||
|
||||
# Desired target mode for the cycle script.
|
||||
- id: desired_mode
|
||||
type: int
|
||||
restore_value: false
|
||||
initial_value: "1"
|
||||
|
||||
# Bitmask of ignored modes (1..max_modes). Example: ignore 4 & 5 -> 24.
|
||||
- id: ignored_mask
|
||||
type: int
|
||||
restore_value: false
|
||||
initial_value: "${ignored_modes_mask}"
|
||||
|
||||
# Internal: whether a recheck is already scheduled for min OFF enforcement.
|
||||
- id: recheck_pending
|
||||
type: bool
|
||||
restore_value: false
|
||||
initial_value: "false"
|
||||
|
||||
##########################################################################################
|
||||
# Text Sensors
|
||||
# https://esphome.io/components/text_sensor/index.html
|
||||
@@ -260,9 +325,6 @@ text_sensor:
|
||||
############################
|
||||
####################################################
|
||||
# Subscribe to the Morning On time, format "HH:MM"
|
||||
# We check x.size() == 5 and x[2] == ':',
|
||||
# then parse x.substr(0,2) and x.substr(3,2)
|
||||
# std::string uses 'substr', not 'substring'.
|
||||
####################################################
|
||||
- platform: mqtt_subscribe
|
||||
name: "Morning On Time Setting"
|
||||
@@ -363,8 +425,6 @@ 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
|
||||
@@ -430,7 +490,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);
|
||||
@@ -445,7 +504,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);
|
||||
@@ -460,7 +518,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);
|
||||
@@ -487,7 +544,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 {
|
||||
@@ -564,6 +621,23 @@ switch:
|
||||
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
|
||||
@@ -635,46 +709,280 @@ 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.
|
||||
# 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.
|
||||
##############################################################################
|
||||
- platform: template
|
||||
name: "Pool Light Mode"
|
||||
id: pool_light_mode_select
|
||||
update_interval: 250ms
|
||||
# Options include all 1..16 (no ignored modes currently) plus 0=OFF
|
||||
options:
|
||||
- "0= OFF"
|
||||
- "1= Dark Blue"
|
||||
- "2= Red"
|
||||
- "3= Green"
|
||||
- "4= Purple"
|
||||
- "5= Aqua"
|
||||
- "6= Lime"
|
||||
- "7= Cool White"
|
||||
- "8= Speed Up"
|
||||
- "9= Transformer"
|
||||
- "10= Blurple"
|
||||
- "11= Redlime"
|
||||
- "12= Greenqua"
|
||||
- "13= Bluequa"
|
||||
- "14= Grelime"
|
||||
- "15= Redurple"
|
||||
- "16= RGB Mad"
|
||||
|
||||
# Show OFF only when not in a color-change sequence.
|
||||
# While changing, reflect the in-flight/current mode (never 0).
|
||||
lambda: |-
|
||||
auto label_for = [](int m) -> std::string {
|
||||
switch (m) {
|
||||
case 0: return "0= OFF";
|
||||
case 1: return "1= Dark Blue";
|
||||
case 2: return "2= Red";
|
||||
case 3: return "3= Green";
|
||||
case 4: return "4= Purple";
|
||||
case 5: return "5= Aqua";
|
||||
case 6: return "6= Lime";
|
||||
case 7: return "7= Cool White";
|
||||
case 8: return "8= Speed Up";
|
||||
case 9: return "9= Transformer";
|
||||
case 10: return "10= Blurple";
|
||||
case 11: return "11= Redlime";
|
||||
case 12: return "12= Greenqua";
|
||||
case 13: return "13= Bluequa";
|
||||
case 14: return "14= Grelime";
|
||||
case 15: return "15= Redurple";
|
||||
case 16: return "16= RGB Mad";
|
||||
default: return "1= Dark Blue";
|
||||
}
|
||||
};
|
||||
auto is_ignored = [&](int m) -> bool {
|
||||
if (m < 1 || m > id(max_modes)) return true;
|
||||
int bit = 1 << (m - 1);
|
||||
return (id(ignored_mask) & bit) != 0;
|
||||
};
|
||||
|
||||
// While stepping (mode_changing), don't show OFF even if relay is briefly OFF.
|
||||
if (id(mode_changing)) {
|
||||
int cm = id(current_mode);
|
||||
if (cm <= 0) cm = 1; // during long reset, assume Mode 1 is next
|
||||
// Skip ignored modes for display if ever configured later
|
||||
if (!is_ignored(cm)) return label_for(cm);
|
||||
// find next non-ignored just for display robustness
|
||||
for (int m = cm + 1; m <= id(max_modes); m++) if (!is_ignored(m)) return label_for(m);
|
||||
for (int m = 1; m <= id(max_modes); m++) if (!is_ignored(m)) return label_for(m);
|
||||
return label_for(1);
|
||||
}
|
||||
|
||||
// Normal (not changing): show OFF if relay is actually OFF, else show current mode
|
||||
if (!id(relay).state) return std::string("0= OFF");
|
||||
|
||||
int cm = id(current_mode);
|
||||
if (cm == 0) return std::string("0= OFF");
|
||||
if (!is_ignored(cm)) return label_for(cm);
|
||||
for (int m = cm + 1; m <= id(max_modes); m++) if (!is_ignored(m)) return label_for(m);
|
||||
for (int m = 1; m <= id(max_modes); m++) if (!is_ignored(m)) return label_for(m);
|
||||
return label_for(1);
|
||||
|
||||
set_action:
|
||||
# First, set flags based on selection
|
||||
- lambda: |-
|
||||
int target = atoi(x.c_str());
|
||||
if (target < 0) target = 0;
|
||||
if (target > id(max_modes)) target = id(max_modes);
|
||||
|
||||
if (target == 0) {
|
||||
// MODE 0: force everything OFF; UI goes back to TIMER control
|
||||
id(mode_changing) = false;
|
||||
id(recheck_pending) = false;
|
||||
id(operation_mode) = 2; // TIMER
|
||||
id(current_mode) = 0; // reflect OFF in dropdown
|
||||
} else {
|
||||
// defensive: ignore choosing an ignored mode (options already hide them)
|
||||
int bit = 1 << (target - 1);
|
||||
if ((id(ignored_mask) & bit) != 0) {
|
||||
ESP_LOGW("mode","Selected target %d is ignored; ignoring request", target);
|
||||
return;
|
||||
}
|
||||
// Color selection: force ON mode and start changer
|
||||
id(operation_mode) = 1;
|
||||
id(desired_mode) = target;
|
||||
id(mode_changing) = true;
|
||||
}
|
||||
|
||||
# If OFF was selected, hard-stop any running scripts and cut power
|
||||
- if:
|
||||
condition:
|
||||
lambda: 'return atoi(x.c_str()) == 0;'
|
||||
then:
|
||||
- script.stop: change_to_desired_mode
|
||||
- script.stop: reset_to_mode1
|
||||
- script.stop: enforce_off_guard
|
||||
- switch.turn_off: relay
|
||||
else:
|
||||
- script.execute: change_to_desired_mode
|
||||
|
||||
#################################################################################################
|
||||
# SCRIPT COMPONENT
|
||||
# https://esphome.io/components/script.html
|
||||
#################################################################################################
|
||||
# Script: evaluate and drive the relay
|
||||
script:
|
||||
# Core: evaluate and drive the relay (with enforced min OFF window)
|
||||
- id: evaluate_relay_state
|
||||
then:
|
||||
- lambda: |-
|
||||
if (id(mode_changing)) {
|
||||
// do not fight the pulser
|
||||
return;
|
||||
}
|
||||
// Determine target state
|
||||
bool target_on = false;
|
||||
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;
|
||||
}
|
||||
|
||||
// TIMER → follow schedule windows
|
||||
{
|
||||
if (mode == 3) { // BOOST
|
||||
target_on = true;
|
||||
} else if (mode == 0) { // OFF
|
||||
target_on = false;
|
||||
} else if (mode == 1) { // ON
|
||||
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;
|
||||
if (should_on) id(relay).turn_on();
|
||||
else id(relay).turn_off();
|
||||
target_on = should_on;
|
||||
}
|
||||
|
||||
// Enforce minimum OFF time when turning ON normally (not during color pulses)
|
||||
if (target_on) {
|
||||
uint32_t delta = millis() - id(last_off_ms);
|
||||
if (delta < (uint32_t) id(reset_window_ms)) {
|
||||
// Still inside OFF window: wait; schedule a recheck if not already
|
||||
if (!id(recheck_pending)) {
|
||||
id(enforce_off_guard).execute();
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
id(relay).turn_on();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
id(relay).turn_off();
|
||||
return;
|
||||
}
|
||||
|
||||
# Poll until OFF window elapsed, then re-evaluate (does not toggle the relay itself)
|
||||
- id: enforce_off_guard
|
||||
mode: restart
|
||||
then:
|
||||
- lambda: |-
|
||||
id(recheck_pending) = true;
|
||||
- while:
|
||||
condition:
|
||||
lambda: |-
|
||||
if (id(mode_changing)) return false; // abort if color change starts
|
||||
uint32_t delta = millis() - id(last_off_ms);
|
||||
return delta < (uint32_t) id(reset_window_ms);
|
||||
then:
|
||||
- delay: 200ms
|
||||
- lambda: |-
|
||||
id(recheck_pending) = false;
|
||||
- script.execute: evaluate_relay_state
|
||||
|
||||
# Long reset to Mode 1
|
||||
- id: reset_to_mode1
|
||||
mode: restart
|
||||
then:
|
||||
- switch.turn_off: relay
|
||||
- delay: ${mode_reset_off}
|
||||
- switch.turn_on: relay
|
||||
- delay: ${mode_pulse_on}
|
||||
- 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
|
||||
then:
|
||||
# If relay is currently OFF, establish a known starting point
|
||||
- if:
|
||||
condition:
|
||||
lambda: 'return !id(relay).state;'
|
||||
then:
|
||||
- lambda: |-
|
||||
uint32_t delta = millis() - id(last_off_ms);
|
||||
// If OFF < reset window, enforce a true reset first for predictable start
|
||||
if (delta < (uint32_t) id(reset_window_ms)) {
|
||||
id(reset_to_mode1).execute();
|
||||
}
|
||||
- if:
|
||||
condition:
|
||||
lambda: 'return !id(relay).state;'
|
||||
then:
|
||||
- switch.turn_on: relay
|
||||
- delay: ${mode_prime_on}
|
||||
- lambda: |-
|
||||
// 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;
|
||||
}
|
||||
|
||||
# If target is below current, do a long reset to Mode 1, then proceed
|
||||
- if:
|
||||
condition:
|
||||
lambda: |-
|
||||
return id(desired_mode) < id(current_mode);
|
||||
then:
|
||||
- script.execute: reset_to_mode1
|
||||
- script.wait: reset_to_mode1
|
||||
|
||||
# If already exactly at target, ensure ON and finish
|
||||
- if:
|
||||
condition:
|
||||
lambda: 'return id(desired_mode) == id(current_mode);'
|
||||
then:
|
||||
- switch.turn_on: relay
|
||||
- lambda: |-
|
||||
id(mode_changing) = false;
|
||||
|
||||
# Step forward until we reach desired (ignored modes still counted)
|
||||
- while:
|
||||
condition:
|
||||
lambda: |-
|
||||
// Allow immediate cancellation if OFF is selected mid-flight
|
||||
return id(mode_changing) && (id(current_mode) < id(desired_mode));
|
||||
then:
|
||||
- switch.turn_off: relay
|
||||
- delay: ${mode_pulse_off}
|
||||
- switch.turn_on: relay
|
||||
- delay: ${mode_pulse_on}
|
||||
- lambda: |-
|
||||
id(current_mode)++;
|
||||
|
||||
# End: make sure we leave the light ON and clear the guard
|
||||
- switch.turn_on: relay
|
||||
- lambda: |-
|
||||
id(mode_changing) = false;
|
||||
|
||||
|
||||
#################################################################################################
|
||||
# INTERVAL COMPONENT
|
||||
# https://esphome.io/components/interval.html
|
||||
@@ -702,4 +1010,3 @@ interval:
|
||||
}
|
||||
}
|
||||
- script.execute: evaluate_relay_state
|
||||
|
||||
|
||||
Reference in New Issue
Block a user