From 5ae45fff63b5c38eca0b5bd94790268164a39ebf Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Mon, 23 Dec 2024 00:41:51 +0100 Subject: [PATCH 1/5] Fix relay on display as light mode Solves #28 --- ...-Ultimate-Easy-ESPHome_core_hw_relays.yaml | 245 +++++++++++++----- 1 file changed, 179 insertions(+), 66 deletions(-) diff --git a/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml b/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml index 79a92ce..80b6bd5 100644 --- a/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml +++ b/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml @@ -11,54 +11,10 @@ ##### - For normal system use, modifications to this file are NOT required. ##### #################################################################################################### --- -binary_sensor: - - id: !extend bs_button_1 - on_click: - then: - - if: - condition: - - lambda: return sl_button_1_action->active_index().has_value(); - - lambda: return sl_button_1_action->active_index().value() == 1; - - and: &button_click_no_other_click - - lambda: return not bs_multi_touch->state; - - lambda: return not bs_swipe_left->state; - - lambda: return not bs_swipe_down->state; - - lambda: return not bs_swipe_right->state; - then: - - switch.toggle: sw_relay_1 - - - id: !extend bs_button_2 - on_click: - then: - - if: - condition: - - lambda: return sl_button_2_action->active_index().has_value(); - - lambda: return sl_button_2_action->active_index().value() == 1; - - and: *button_click_no_other_click - then: - - switch.toggle: sw_relay_2 - - - id: !extend bs_button_3 - on_click: - then: - - if: - condition: - - lambda: return sl_button_3_action->active_index().has_value(); - - lambda: return sl_button_3_action->active_index().value() == 1; - - and: *button_click_no_other_click - then: - - switch.toggle: sw_relay_3 - - - id: !extend bs_button_4 - on_click: - then: - - if: - condition: - - lambda: return sl_button_4_action->active_index().has_value(); - - lambda: return sl_button_4_action->active_index().value() == 1; - - and: *button_click_no_other_click - then: - - switch.toggle: sw_relay_4 +substitutions: + RELAY_MODE_TEXT_SWITCH: "Switch" + RELAY_MODE_TEXT_LIGHT: "Light" + RELAY_MODE_TEXT_NOT_USED: "Not in use" globals: - id: boot_initialization_relays @@ -68,24 +24,82 @@ globals: light: # These lights are used for the physical relays to be shown as lights - - &light_binary_base - id: light_output_1 + - id: light_output_1 name: Light output 1 output: output_relay_1 platform: binary internal: true + on_turn_on: + then: + - if: + condition: + - switch.is_off: sw_relay_1 + then: + - switch.turn_on: sw_relay_1 + on_turn_off: + then: + - if: + condition: + - switch.is_on: sw_relay_1 + then: + - switch.turn_off: sw_relay_1 - id: light_output_2 name: Light output 2 output: output_relay_2 - <<: *light_binary_base + platform: binary + internal: true + on_turn_on: + then: + - if: + condition: + - switch.is_off: sw_relay_2 + then: + - switch.turn_on: sw_relay_2 + on_turn_off: + then: + - if: + condition: + - switch.is_on: sw_relay_2 + then: + - switch.turn_off: sw_relay_2 - id: light_output_3 name: Light output 3 output: output_relay_3 - <<: *light_binary_base + platform: binary + internal: true + on_turn_on: + then: + - if: + condition: + - switch.is_off: sw_relay_3 + then: + - switch.turn_on: sw_relay_3 + on_turn_off: + then: + - if: + condition: + - switch.is_on: sw_relay_3 + then: + - switch.turn_off: sw_relay_3 - id: light_output_4 name: Light output 4 output: output_relay_4 - <<: *light_binary_base + platform: binary + internal: true + on_turn_on: + then: + - if: + condition: + - switch.is_off: sw_relay_4 + then: + - switch.turn_on: sw_relay_4 + on_turn_off: + then: + - if: + condition: + - switch.is_on: sw_relay_4 + then: + - switch.turn_off: sw_relay_4 output: - id: output_relay_1 @@ -159,6 +173,41 @@ script: } id(boot_initialization_relays) = true; + - id: !extend button_click_event + then: + - lambda: |- + // Handle only single clicks + if (click_count != 1) + return; + + // Ignore if other touch events are active + if (bs_multi_touch->state || + bs_swipe_left->state || + bs_swipe_down->state || + bs_swipe_right->state) { + return; + } + + // Toggle relay if corresponding button action is enabled + switch (button_id) { + case ${BUTTON_1_ID}: + if (sl_button_1_action->state != "${RELAY_MODE_TEXT_NOT_USED}") + sw_relay_1->toggle(); + break; + case ${BUTTON_2_ID}: + if (sl_button_2_action->state != "${RELAY_MODE_TEXT_NOT_USED}") + sw_relay_2->toggle(); + break; + case ${BUTTON_3_ID}: + if (sl_button_3_action->state != "${RELAY_MODE_TEXT_NOT_USED}") + sw_relay_3->toggle(); + break; + case ${BUTTON_4_ID}: + if (sl_button_4_action->state != "${RELAY_MODE_TEXT_NOT_USED}") + sw_relay_4->toggle(); + break; + } + - id: show_relay_status mode: restart then: @@ -236,12 +285,13 @@ script: select: - &relay_select_mode_base id: sl_relay_1_mode - name: Relay 1 mode + name: Relay 1 display mode platform: template options: - - "Relay" - - "Light" - - "Not in use" + - "${RELAY_MODE_TEXT_SWITCH}" + - "${RELAY_MODE_TEXT_LIGHT}" + - "${RELAY_MODE_TEXT_NOT_USED}" + initial_option: "${RELAY_MODE_TEXT_SWITCH}" optimistic: true restore_value: true internal: true @@ -250,44 +300,107 @@ select: icon: mdi:dip-switch - id: sl_relay_2_mode - name: Relay 2 mode + name: Relay 2 display mode <<: *relay_select_mode_base - id: sl_relay_3_mode - name: Relay 3 mode + name: Relay 3 display mode <<: *relay_select_mode_base - id: sl_relay_4_mode - name: Relay 4 mode + name: Relay 4 display mode <<: *relay_select_mode_base switch: - - &relay_switch_base - id: sw_relay_1 + - id: sw_relay_1 name: Relay 1 - platform: output output: output_relay_1 + platform: output restore_mode: RESTORE_DEFAULT_OFF internal: true on_turn_on: then: - script.execute: show_relay_status + - if: + condition: + - light.is_off: light_output_1 + then: + light.turn_on: light_output_1 on_turn_off: then: - script.execute: show_relay_status + - if: + condition: + - light.is_on: light_output_1 + then: + - light.turn_off: light_output_1 - id: sw_relay_2 name: Relay 2 output: output_relay_2 - <<: *relay_switch_base + platform: output + restore_mode: RESTORE_DEFAULT_OFF + internal: true + on_turn_on: + then: + - script.execute: show_relay_status + - if: + condition: + - light.is_off: light_output_2 + then: + light.turn_on: light_output_2 + on_turn_off: + then: + - script.execute: show_relay_status + - if: + condition: + - light.is_on: light_output_2 + then: + - light.turn_off: light_output_2 - id: sw_relay_3 name: Relay 3 output: output_relay_3 - <<: *relay_switch_base + platform: output + restore_mode: RESTORE_DEFAULT_OFF + internal: true + on_turn_on: + then: + - script.execute: show_relay_status + - if: + condition: + - light.is_off: light_output_3 + then: + - light.turn_on: light_output_3 + on_turn_off: + then: + - script.execute: show_relay_status + - if: + condition: + - light.is_on: light_output_3 + then: + - light.turn_off: light_output_3 - id: sw_relay_4 name: Relay 4 output: output_relay_4 - <<: *relay_switch_base + platform: output + restore_mode: RESTORE_DEFAULT_OFF + internal: true + on_turn_on: + then: + - script.execute: show_relay_status + - if: + condition: + - light.is_off: light_output_4 + then: + - light.turn_on: light_output_4 + on_turn_off: + then: + - script.execute: show_relay_status + - if: + condition: + - light.is_on: light_output_4 + then: + - light.turn_off: light_output_4 ... From dc9931939792a5868f732b04d64fd79e9f97c0fc Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Mon, 23 Dec 2024 00:46:06 +0100 Subject: [PATCH 2/5] Fix relay id on log --- ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_leds.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_leds.yaml b/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_leds.yaml index 0cf8b30..a044bb5 100644 --- a/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_leds.yaml +++ b/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_leds.yaml @@ -795,12 +795,12 @@ script: case 1: if (light_index >= id(gb_lights_1).size()) { ESP_LOGE("core_hw_leds", "Invalid set with %s light for Relay %" PRIu8, - light_mode_group_1.c_str(), light_index); + light_mode_group_1.c_str(), light_index+1); return; } if (state and !id(gb_lights_1)[light_index]->current_values.is_on()) { ESP_LOGI("core_hw_leds", "Turn-on %s light for Relay %" PRIu8, - light_mode_group_1.c_str(), light_index); + light_mode_group_1.c_str(), light_index+1); auto call = id(gb_lights_1)[light_index]->turn_on(); if (LIGHT_TRANSITION_TURN_ON > 0) call.set_transition_length(LIGHT_TRANSITION_TURN_ON); // in ms @@ -809,7 +809,7 @@ script: call.perform(); } else if (!state and id(gb_lights_1)[light_index]->current_values.is_on()) { ESP_LOGI("core_hw_leds", "Turn-off %s light for Relay %" PRIu8, - light_mode_group_1.c_str(), light_index); + light_mode_group_1.c_str(), light_index+1); auto call = id(gb_lights_1)[light_index]->turn_off(); if (LIGHT_TRANSITION_TURN_OFF > 0) call.set_transition_length(LIGHT_TRANSITION_TURN_OFF); // in ms @@ -819,12 +819,12 @@ script: case 2: if (light_index >= id(gb_lights_2).size()) { ESP_LOGE("core_hw_leds", "Invalid set with %s light for Relay %" PRIu8, - light_mode_group_2.c_str(), light_index); + light_mode_group_2.c_str(), light_index+1); return; } if (state and !id(gb_lights_2)[light_index]->current_values.is_on()) { ESP_LOGI("core_hw_leds", "Turn-on %s light for Relay %" PRIu8, - light_mode_group_2.c_str(), light_index); + light_mode_group_2.c_str(), light_index+1); auto call = id(gb_lights_2)[light_index]->turn_on(); if (LIGHT_TRANSITION_TURN_ON > 0) call.set_transition_length(LIGHT_TRANSITION_TURN_ON); // in ms @@ -833,7 +833,7 @@ script: call.perform(); } else if (!state and id(gb_lights_2)[light_index]->current_values.is_on()) { ESP_LOGI("core_hw_leds", "Turn-off %s light for Relay %" PRIu8, - light_mode_group_2.c_str(), light_index); + light_mode_group_2.c_str(), light_index+1); auto call = id(gb_lights_2)[light_index]->turn_off(); if (LIGHT_TRANSITION_TURN_OFF > 0) call.set_transition_length(LIGHT_TRANSITION_TURN_OFF); // in ms From 8a509cedc8136cb8d864453ba529bf30d4aa8d3e Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Mon, 23 Dec 2024 00:52:59 +0100 Subject: [PATCH 3/5] Point to `latest` --- TX-Ultimate-Easy-ESPHome.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TX-Ultimate-Easy-ESPHome.yaml b/TX-Ultimate-Easy-ESPHome.yaml index 3b95ea8..3f181d8 100644 --- a/TX-Ultimate-Easy-ESPHome.yaml +++ b/TX-Ultimate-Easy-ESPHome.yaml @@ -16,7 +16,7 @@ wifi: packages: remote_package: url: https://github.com/edwardtfn/TX-Ultimate-Easy - ref: dev + ref: latest # Or you can specify a version, like `ref: v2024.12.6` refresh: 30s files: - ESPHome/TX-Ultimate-Easy-ESPHome_core.yaml From ee6f5654473b8686ecf1aa17483dc206f67edf2f Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Mon, 23 Dec 2024 00:59:17 +0100 Subject: [PATCH 4/5] Change refresh to 5min --- TX-Ultimate-Easy-ESPHome.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TX-Ultimate-Easy-ESPHome.yaml b/TX-Ultimate-Easy-ESPHome.yaml index f3b95c3..9823def 100644 --- a/TX-Ultimate-Easy-ESPHome.yaml +++ b/TX-Ultimate-Easy-ESPHome.yaml @@ -16,8 +16,8 @@ wifi: packages: remote_package: url: https://github.com/edwardtfn/TX-Ultimate-Easy - ref: main - refresh: 30s + ref: latest # Or you can specify a version, like `ref: v2024.12.6` + refresh: 5min files: - ESPHome/TX-Ultimate-Easy-ESPHome_core.yaml # - ESPHome/TX-Ultimate-Easy-ESPHome_addon_ble_proxy.yaml # Adds experimental BLE proxy support (limited) From 552b83f0657874cae4796dc080eed96aaeee4ffc Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Mon, 23 Dec 2024 01:03:51 +0100 Subject: [PATCH 5/5] Re-add button ids on substitutions --- ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml b/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml index 20eb5a7..7573678 100644 --- a/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml +++ b/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml @@ -26,6 +26,12 @@ substitutions: BUTTON_PRESS_TIMEOUT: '10000' # Ignore if button is pressed for longer than this time, in msec BUTTON_LONG_PRESS_DELAY: '800' # The time to wait to consider a long press, in msec + # Button numbers constants + BUTTON_1_ID: '1' + BUTTON_2_ID: '2' + BUTTON_3_ID: '3' + BUTTON_4_ID: '4' + binary_sensor: - &binary_sensor_button_base id: bs_button_2