From 03e0004e8ca370dd15c1af2e219aabe573e1067d Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Sun, 10 Aug 2025 23:29:25 +0200 Subject: [PATCH] Update lights when mode changes --- ...imate-Easy-ESPHome_standard_hw_relays.yaml | 274 ++++++++++++++++++ 1 file changed, 274 insertions(+) diff --git a/ESPHome/TX-Ultimate-Easy-ESPHome_standard_hw_relays.yaml b/ESPHome/TX-Ultimate-Easy-ESPHome_standard_hw_relays.yaml index d7f67a0..1693501 100644 --- a/ESPHome/TX-Ultimate-Easy-ESPHome_standard_hw_relays.yaml +++ b/ESPHome/TX-Ultimate-Easy-ESPHome_standard_hw_relays.yaml @@ -1353,9 +1353,44 @@ select: icon: mdi:dots-square on_value: - lambda: |- + // Update packed value (existing code) id(relay_light_modes_packed) = (id(relay_light_modes_packed) & ~(0x0F << 0)) | ((id(sl_relay_1_light_mode_eu).active_index().value_or(1) & 0x0F) << 0); + // Handle mode transitions (only after boot is complete) + if (!id(bs_boot_completed)->state) return; // Skip during boot + + static std::string previous_mode = ""; // Static variable local to THIS lambda only + std::string current_mode = x; + + if (previous_mode.empty()) { + previous_mode = current_mode; // Initialize on first run after boot + return; + } + + if (previous_mode != "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode == "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning TO Disabled - turn off lights + ESP_LOGI("${TAG_STD_HW_RELAYS}", + "Relay 1 light mode changed to ${LIGHT_MODE_DISABLED_TEXT} - turning off lights"); + id(light_set_state)->execute(1, 0, false); // Group 1, relay index 0 + id(light_set_state)->execute(2, 0, false); // Group 2, relay index 0 + } else if (previous_mode == "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode != "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning FROM Disabled - update lights based on relay state + ESP_LOGI("${TAG_STD_HW_RELAYS}", + "Relay 1 light mode changed from ${LIGHT_MODE_DISABLED_TEXT} - updating lights"); + bool relay_state = id(sw_relay_1).state; + if (current_mode == "${LIGHT_MODE_BOTTOM_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(1, 0, relay_state); + } + if (current_mode == "${LIGHT_MODE_TOP_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(2, 0, relay_state); + } + } + + previous_mode = current_mode; + # US relay light mode template - &relay_light_mode_us id: sl_relay_1_light_mode_us @@ -1375,9 +1410,44 @@ select: icon: mdi:dots-square on_value: - lambda: |- + // Update packed value id(relay_light_modes_packed) = (id(relay_light_modes_packed) & ~(0x0F << 0)) | ((id(sl_relay_1_light_mode_us).active_index().value_or(1) & 0x0F) << 0); + // Handle mode transitions (only after boot is complete) + if (!id(bs_boot_completed)->state) return; // Skip during boot + + static std::string previous_mode = ""; + std::string current_mode = x; + + if (previous_mode.empty()) { + previous_mode = current_mode; // Initialize on first run after boot + return; + } + + if (previous_mode != "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode == "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning TO Disabled - turn off lights + ESP_LOGI("${TAG_STD_HW_RELAYS}", + "Relay 1 light mode changed to ${LIGHT_MODE_DISABLED_TEXT} - turning off lights"); + id(light_set_state)->execute(1, 0, false); // Group 1, relay index 0 + id(light_set_state)->execute(2, 0, false); // Group 2, relay index 0 + } else if (previous_mode == "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode != "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning FROM Disabled - update lights based on relay state + ESP_LOGI("${TAG_STD_HW_RELAYS}", + "Relay 1 light mode changed from ${LIGHT_MODE_DISABLED_TEXT} - updating lights"); + bool relay_state = id(sw_relay_1).state; + if (current_mode == "${LIGHT_MODE_LEFT_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(1, 0, relay_state); + } + if (current_mode == "${LIGHT_MODE_RIGHT_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(2, 0, relay_state); + } + } + + previous_mode = current_mode; + - id: sl_relay_1_mode name: Relay 1 display mode platform: template @@ -1406,17 +1476,87 @@ select: <<: *relay_light_mode_eu on_value: - lambda: |- + // Update packed value id(relay_light_modes_packed) = (id(relay_light_modes_packed) & ~(0x0F << 4)) | ((id(sl_relay_2_light_mode_eu).active_index().value_or(1) & 0x0F) << 4); + // Handle mode transitions (only after boot is complete) + if (!id(bs_boot_completed)->state) return; // Skip during boot + + static std::string previous_mode = ""; + std::string current_mode = x; + + if (previous_mode.empty()) { + previous_mode = current_mode; // Initialize on first run after boot + return; + } + + if (previous_mode != "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode == "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning TO Disabled - turn off lights + ESP_LOGI("${TAG_STD_HW_RELAYS}", + "Relay 2 light mode changed to ${LIGHT_MODE_DISABLED_TEXT} - turning off lights"); + id(light_set_state)->execute(1, 1, false); // Group 1, relay index 1 + id(light_set_state)->execute(2, 1, false); // Group 2, relay index 1 + } else if (previous_mode == "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode != "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning FROM Disabled - update lights based on relay state + ESP_LOGI("${TAG_STD_HW_RELAYS}", + "Relay 2 light mode changed from ${LIGHT_MODE_DISABLED_TEXT} - updating lights"); + bool relay_state = id(sw_relay_2).state; + if (current_mode == "${LIGHT_MODE_BOTTOM_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(1, 1, relay_state); + } + if (current_mode == "${LIGHT_MODE_TOP_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(2, 1, relay_state); + } + } + + previous_mode = current_mode; + - id: sl_relay_2_light_mode_us name: Relay 2 light mode US <<: *relay_light_mode_us on_value: - lambda: |- + // Update packed value id(relay_light_modes_packed) = (id(relay_light_modes_packed) & ~(0x0F << 4)) | ((id(sl_relay_2_light_mode_us).active_index().value_or(1) & 0x0F) << 4); + // Handle mode transitions (only after boot is complete) + if (!id(bs_boot_completed)->state) return; // Skip during boot + + static std::string previous_mode = ""; + std::string current_mode = x; + + if (previous_mode.empty()) { + previous_mode = current_mode; // Initialize on first run after boot + return; + } + + if (previous_mode != "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode == "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning TO Disabled - turn off lights + ESP_LOGI("${TAG_STD_HW_RELAYS}", + "Relay 2 light mode changed to ${LIGHT_MODE_DISABLED_TEXT} - turning off lights"); + id(light_set_state)->execute(1, 1, false); // Group 1, relay index 1 + id(light_set_state)->execute(2, 1, false); // Group 2, relay index 1 + } else if (previous_mode == "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode != "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning FROM Disabled - update lights based on relay state + ESP_LOGI("${TAG_STD_HW_RELAYS}", + "Relay 2 light mode changed from ${LIGHT_MODE_DISABLED_TEXT} - updating lights"); + bool relay_state = id(sw_relay_2).state; + if (current_mode == "${LIGHT_MODE_LEFT_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(1, 1, relay_state); + } + if (current_mode == "${LIGHT_MODE_RIGHT_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(2, 1, relay_state); + } + } + + previous_mode = current_mode; + - id: sl_relay_2_mode name: Relay 2 display mode platform: template @@ -1445,17 +1585,85 @@ select: <<: *relay_light_mode_eu on_value: - lambda: |- + // Update packed value id(relay_light_modes_packed) = (id(relay_light_modes_packed) & ~(0x0F << 8)) | ((id(sl_relay_3_light_mode_eu).active_index().value_or(1) & 0x0F) << 8); + // Handle mode transitions (only after boot is complete) + if (!id(bs_boot_completed)->state) return; // Skip during boot + + static std::string previous_mode = ""; + std::string current_mode = x; + + if (previous_mode.empty()) { + previous_mode = current_mode; // Initialize on first run after boot + return; + } + + if (previous_mode != "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode == "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning TO Disabled - turn off lights + ESP_LOGI("${TAG_STD_HW_RELAYS}", + "Relay 3 light mode changed to ${LIGHT_MODE_DISABLED_TEXT} - turning off lights"); + id(light_set_state)->execute(1, 2, false); // Group 1, relay index 2 + id(light_set_state)->execute(2, 2, false); // Group 2, relay index 2 + } else if (previous_mode == "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode != "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning FROM Disabled - update lights based on relay state + ESP_LOGI("${TAG_STD_HW_RELAYS}", + "Relay 3 light mode changed from ${LIGHT_MODE_DISABLED_TEXT} - updating lights"); + bool relay_state = id(sw_relay_3).state; + if (current_mode == "${LIGHT_MODE_BOTTOM_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(1, 2, relay_state); + } + if (current_mode == "${LIGHT_MODE_TOP_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(2, 2, relay_state); + } + } + + previous_mode = current_mode; + - id: sl_relay_3_light_mode_us name: Relay 3 light mode US <<: *relay_light_mode_us on_value: - lambda: |- + // Update packed value id(relay_light_modes_packed) = (id(relay_light_modes_packed) & ~(0x0F << 8)) | ((id(sl_relay_3_light_mode_us).active_index().value_or(1) & 0x0F) << 8); + // Handle mode transitions (only after boot is complete) + if (!id(bs_boot_completed)->state) return; // Skip during boot + + static std::string previous_mode = ""; + std::string current_mode = x; + + if (previous_mode.empty()) { + previous_mode = current_mode; // Initialize on first run after boot + return; + } + + if (previous_mode != "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode == "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning TO Disabled - turn off lights + ESP_LOGI("${TAG_STD_HW_RELAYS}", "Relay 3 light mode changed to ${LIGHT_MODE_DISABLED_TEXT} - turning off lights"); + id(light_set_state)->execute(1, 2, false); // Group 1, relay index 2 + id(light_set_state)->execute(2, 2, false); // Group 2, relay index 2 + } else if (previous_mode == "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode != "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning FROM Disabled - update lights based on relay state + ESP_LOGI("${TAG_STD_HW_RELAYS}", "Relay 3 light mode changed from ${LIGHT_MODE_DISABLED_TEXT} - updating lights"); + bool relay_state = id(sw_relay_3).state; + if (current_mode == "${LIGHT_MODE_LEFT_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(1, 2, relay_state); + } + if (current_mode == "${LIGHT_MODE_RIGHT_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(2, 2, relay_state); + } + } + + previous_mode = current_mode; + - id: sl_relay_3_mode name: Relay 3 display mode platform: template @@ -1484,17 +1692,83 @@ select: <<: *relay_light_mode_eu on_value: - lambda: |- + // Update packed value id(relay_light_modes_packed) = (id(relay_light_modes_packed) & ~(0x0F << 12)) | ((id(sl_relay_4_light_mode_eu).active_index().value_or(1) & 0x0F) << 12); + // Handle mode transitions (only after boot is complete) + if (!id(bs_boot_completed)->state) return; // Skip during boot + + static std::string previous_mode = ""; + std::string current_mode = x; + + if (previous_mode.empty()) { + previous_mode = current_mode; // Initialize on first run after boot + return; + } + + if (previous_mode != "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode == "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning TO Disabled - turn off lights + ESP_LOGI("${TAG_STD_HW_RELAYS}", "Relay 4 light mode changed to ${LIGHT_MODE_DISABLED_TEXT} - turning off lights"); + id(light_set_state)->execute(1, 3, false); // Group 1, relay index 3 + id(light_set_state)->execute(2, 3, false); // Group 2, relay index 3 + } else if (previous_mode == "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode != "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning FROM Disabled - update lights based on relay state + ESP_LOGI("${TAG_STD_HW_RELAYS}", "Relay 4 light mode changed from ${LIGHT_MODE_DISABLED_TEXT} - updating lights"); + bool relay_state = id(sw_relay_4).state; + if (current_mode == "${LIGHT_MODE_BOTTOM_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(1, 3, relay_state); + } + if (current_mode == "${LIGHT_MODE_TOP_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(2, 3, relay_state); + } + } + + previous_mode = current_mode; + - id: sl_relay_4_light_mode_us name: Relay 4 light mode US <<: *relay_light_mode_us on_value: - lambda: |- + // Update packed value id(relay_light_modes_packed) = (id(relay_light_modes_packed) & ~(0x0F << 12)) | ((id(sl_relay_4_light_mode_us).active_index().value_or(1) & 0x0F) << 12); + // Handle mode transitions (only after boot is complete) + if (!id(bs_boot_completed)->state) return; // Skip during boot + + static std::string previous_mode = ""; + std::string current_mode = x; + + if (previous_mode.empty()) { + previous_mode = current_mode; // Initialize on first run after boot + return; + } + + if (previous_mode != "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode == "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning TO Disabled - turn off lights + ESP_LOGI("${TAG_STD_HW_RELAYS}", "Relay 4 light mode changed to ${LIGHT_MODE_DISABLED_TEXT} - turning off lights"); + id(light_set_state)->execute(1, 3, false); // Group 1, relay index 3 + id(light_set_state)->execute(2, 3, false); // Group 2, relay index 3 + } else if (previous_mode == "${LIGHT_MODE_DISABLED_TEXT}" && + current_mode != "${LIGHT_MODE_DISABLED_TEXT}") { + // Transitioning FROM Disabled - update lights based on relay state + ESP_LOGI("${TAG_STD_HW_RELAYS}", "Relay 4 light mode changed from ${LIGHT_MODE_DISABLED_TEXT} - updating lights"); + bool relay_state = id(sw_relay_4).state; + if (current_mode == "${LIGHT_MODE_LEFT_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(1, 3, relay_state); + } + if (current_mode == "${LIGHT_MODE_RIGHT_TEXT}" || current_mode == "${LIGHT_MODE_BOTH_TEXT}") { + id(light_set_state)->execute(2, 3, relay_state); + } + } + + previous_mode = current_mode; + - id: sl_relay_4_mode name: Relay 4 display mode platform: template