diff --git a/esphome/esp-downstloungemain.yaml b/esphome/esp-downstloungemain.yaml index 27c6da4..d3abfd6 100644 --- a/esphome/esp-downstloungemain.yaml +++ b/esphome/esp-downstloungemain.yaml @@ -201,6 +201,52 @@ status_led: number: GPIO2 inverted: true +#:########################################################################################:# +# SCRIPTS +# https://esphome.io/components/script.html +#:########################################################################################:# +script: + # Local/HA requested ON for Main Lights. Change the local relay immediately, then + # publish a non-retained command so any linked switch follows. + - id: mainlights_on + mode: restart + then: + - if: + condition: + lambda: "return !id(relay_1).state;" + then: + - switch.turn_on: relay_1 + - mqtt.publish: + topic: "${mqtt_local_command_topic}" + payload: "On" + retain: false + + # Local/HA requested OFF for Main Lights. + - id: mainlights_off + mode: restart + then: + - if: + condition: + lambda: "return id(relay_1).state;" + then: + - switch.turn_off: relay_1 + - mqtt.publish: + topic: "${mqtt_local_command_topic}" + payload: "Off" + retain: false + + # Physical Button 1 toggle logic. + - id: mainlights_toggle + mode: restart + then: + - if: + condition: + lambda: "return id(relay_1).state;" + then: + - script.execute: mainlights_off + else: + - script.execute: mainlights_on + #:########################################################################################:# # BINARY SENSORS # https://esphome.io/components/binary_sensor/ @@ -214,8 +260,11 @@ binary_sensor: inverted: true use_interrupt: false name: "Button 1: ${switch_1_name}" + filters: + - delayed_on: 10ms + - delayed_off: 10ms on_press: - - switch.toggle: relay_1 + - script.execute: mainlights_toggle # Button 2 (GPIO05) - platform: gpio @@ -224,16 +273,22 @@ binary_sensor: mode: INPUT inverted: true name: "Button 2: ${switch_2_name}" + filters: + - delayed_on: 10ms + - delayed_off: 10ms on_press: - switch.toggle: relay_2 # Button 3 (GPIO04) - platform: gpio pin: - number: GPIO4 + number: GPIO04 mode: INPUT inverted: true name: "Button 3: ${switch_3_name}" + filters: + - delayed_on: 10ms + - delayed_off: 10ms on_press: - switch.toggle: relay_3 @@ -242,19 +297,36 @@ binary_sensor: # https://esphome.io/components/switch/ #:########################################################################################:# switch: - # Relay 1 (GPIO13) + publish MQTT status - - platform: gpio + # Exposed Home Assistant control for Main Lights. This controls the local relay and + # publishes the matching command so linked switches follow. + - platform: template name: "Relay 1: ${switch_1_name}" + id: relay_1_ha + lambda: |- + return id(relay_1).state; + turn_on_action: + - script.execute: mainlights_on + turn_off_action: + - script.execute: mainlights_off + + # Physical Relay 1. Kept internal so HA uses the template switch above. + # Relay callbacks publish retained status only, not commands. + - platform: gpio + name: "Internal Relay 1: ${switch_1_name}" + internal: true pin: GPIO13 id: relay_1 + restore_mode: ${relay_restore_mode} on_turn_on: - mqtt.publish: topic: "${mqtt_local_status_topic}" payload: "On" + retain: true on_turn_off: - mqtt.publish: topic: "${mqtt_local_status_topic}" payload: "Off" + retain: true # Relay 2 (GPIO12) - platform: gpio