Configuring a physical button to update a virtual switch

I have the following device configuration using the esphome integration

esphome:
  name: are-you-home
  platform: ESP8266
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: !secret ota_password

wifi:
  ssid: "MyHotspot"
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Are-You-Home-Fallback-Hotspot"
    password: !secret fallback_password

captive_portal:

switch:
- platform: gpio
  pin: 
      number: 0
      inverted: true
  id: are_you_home_siren

- platform: template
  name: "Siren"
  optimistic: yes
  id: sirentemp
  turn_on_action:
  - while:
      condition:
        lambda: 'return true;'
      then:
      - switch.turn_on: are_you_home_siren
      - delay: 500ms 
      - switch.turn_off: are_you_home_siren
      - delay: 500ms
  turn_off_action:
  - switch.turn_off: are_you_home_siren

binary_sensor:
- platform: gpio
  pin: 
    number: 3
    mode: INPUT_PULLUP
  id: "silence_alarm_button"
  name: "Silence alarm"
  device_class: sound

That provides the following device entities

image

The device it controls has a piezo buzzer and a momentary switch. The beeping alarm triggers just fine when the switch is toggled in the HASS devices dashboard. When the physical momentary button is pushed the HASS devices dashboard reflects this by putting a line through the note and changing “detected” to “clear”.

What I am wanting to do is to link the two together i.e. when the physical button is pressed, if the buzzer is beeping, then Siren toggle on the dashboard changes to off and the sound stops.

I am not sure where to begin with this.

You need to give your button instructions on what to do by adding an automation to your gpio binary sensor, such as “on-press” or “on-release”…

esphome:
  name: are-you-home
  platform: ESP8266
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: !secret ota_password

wifi:
  ssid: "MyHotspot"
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Are-You-Home-Fallback-Hotspot"
    password: !secret fallback_password

captive_portal:

switch:
- platform: gpio
  pin: 
      number: 0
      inverted: true
  id: are_you_home_siren

- platform: template
  name: "Siren"
  optimistic: yes
  id: sirentemp
  turn_on_action:
  - while:
      condition:
        lambda: 'return true;'
      then:
      - switch.turn_on: are_you_home_siren
      - delay: 500ms 
      - switch.turn_off: are_you_home_siren
      - delay: 500ms
  turn_off_action:
  - switch.turn_off: are_you_home_siren

binary_sensor:
- platform: gpio
  pin: 
    number: 3
    mode: INPUT_PULLUP
  id: "silence_alarm_button"
  name: "Silence alarm"
  device_class: sound
  on_press:
      then:
        - switch.turn_off: sirentemp

You could use switch.toggle instead if you wanted to be able to trigger the siren by pressing the button.

Since the button itself is not detecting sound I would leave its device class as default, not sound. But it won’t hurt anything to have the device class that way, it will just make your state appear as “detected/clear” in the frontend instead of “on/off”.

If you don’t actually need to see the button’s entity in Home Assistant, you can also add “internal: true”.

1 Like

Thanks, that was perfect. For completeness sake, here is the final code:

esphome:
  name: are-you-home
  platform: ESP8266
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: !secret ota_password

wifi:
  ssid: "MyHotspot"
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Are-You-Home-Fallback-Hotspot"
    password: !secret fallback_password

captive_portal:

switch:
- platform: gpio
  pin: 
      number: 0
      inverted: true
  id: are_you_home_siren

- platform: template
  name: "Siren"
  optimistic: yes
  id: sirentemp
  turn_on_action:
  - while:
      condition:
        lambda: 'return true;'
      then:
      - switch.turn_on: are_you_home_siren
      - delay: 500ms 
      - switch.turn_off: are_you_home_siren
      - delay: 500ms
  turn_off_action:
  - switch.turn_off: are_you_home_siren

binary_sensor:
- platform: gpio
  pin: 
    number: 3
    mode: INPUT_PULLUP
  id: "silence_alarm_button"
  name: "Silence alarm"
  internal: true
  #device_class: sound
  on_press:
    then:
      - switch.turn_off: sirentemp

For my final project please see the following: https://www.instructables.com/Anyone-Home/ (see step 4 at end for HASS integration steps).