Push button in ESPHome to toggle a light

I have five push buttons connected to one NodeMCU (with ESPHome). I am trying to make these push buttons toggle switches for various lights (mainly connected to other ESPHome nodes with traditional relays. Cannot get the toggle function to work properly. Any tip with the code?

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode:
        input: true
        pullup: true
      inverted: true
    name: Button Black
    filters:
      - delayed_off: 20ms
    on_press:
      then:
      - homeassistant.service:
          service: light.toggle
          data:
            entity_id: switch.relay_light_over_sea_map 

Logs? Where are they?

2 Likes

Challenge is when I push the button, the switch changes to On, but goes back to Off when I release it.

Linke Nick said, look at your logs. It could be your button is sending multiple on states and needs some debounce code.

You may want on_click

1 Like

You realize that this is the “Boot”/“Flash” button of your nodemcu, right?

3 Likes

So wisely choose your GPIO’s :warning:

Spoiler: GPIO0 isn’t a correct one :put_litter_in_its_place:

1 Like