Can not figure out how to toggle light with ESPHome

Hi,

I am struggling. I believe I have not understood the basic concept of using ESPHome. I am trying to toggle a light (an Ikea traadfri, which is available in HA) by pushing a button connected to a nodemcu. But I really have no idea what I am doing, seem unable to get my head around it.

The light is called sengelys.

I would have thought it would be something like this:

ota:

binary_sensor:
 - platform: gpio
   id: sengelystoggle
   pin: 
     number: D7
     mode: INPUT_PULLUP
     inverted: true
   filters: 
    - delayed_on: 10ms
    - delayed_off: 10ms
   on_press:
      then: 

And after the ‘then:’ I would have thought I could have something like light.toggle: sengelys, or light.toggle: light.sengelys. But no matter what I get errors when editing the file.

The above, with nothing after the then gives me this when I press the button:
[01:15:19][D][binary_sensor:036]: ‘sengelystoggle’: Sending state ON
[01:15:19][D][binary_sensor:036]: ‘sengelystoggle’: Sending state OFF

So it seems it is switching it on and then off immediately.

I have tried inserting some delays, have tried on_click, on_release, on_state, etc. It must be something very simple, but I can not get my head around it.

Anybody?

1 Like

I think you are confusing the entities available in home assistant and those available in esphome. I’ll post again in a while.

OK I think this will work - leave off the

on_press:
  then:

But give it a name: so your block will look like this:

binary_sensor:
 - platform: gpio
   id: sengelystoggle
   pin: 
     number: D7
     mode: INPUT_PULLUP
     inverted: true
  name: button
   filters: 
    - delayed_on: 10ms
    - delayed_off: 10ms

Then you need a home assistant (not esphome) automation like this:

  trigger:
  - device_id: ea2cdee5f3f146a8a0ea20364eda3f5f
    domain: switch
    entity_id: switch.espname_button
    platform: device
    type: turned_on
  condition: []
  action:
  - device_id: f494d7b33208428588eedf26a2ee60af
    domain: switch
    entity_id: light.traadfri
    type: toggle
2 Likes

Fantastic, thank you so much, this works. As I said, I had not understood the basics. And the solution was, as expected, so simple. Thank you.

Or if you want to keep it all in esphome (assuming you’ve enabled api: in esphome device):

binary_sensor:
 - platform: gpio
   id: sengelystoggle
   pin: 
     number: D7
     mode: INPUT_PULLUP
     inverted: true
  name: button
   filters: 
    - delayed_on: 10ms
    - delayed_off: 10ms
  on_press:
    then:
      - homeassistant.service:
          service: light.toggle
          data:
            entity_id: light.traadfri
7 Likes

Ah, also a good one. While searching I had seen the homeasstistant.service call and tried it, briefly, did bit search enough to figure out the api: requirement. Thanks, might try it next time, perhaps a tiny bit faster.