ESPHome if condition

How can I get a switch to perform just once?

Here is my section of code, it’s the line after ‘condition’ I’m struggling with. I’m wanting the condition to be true if the ESP’s led is on. I started with the line …

lambda: 'return id(light1).state =="off";'

but that didn’t work either.

on_press:
then:
- if:
condition:
# Should return either true or false
switch.is_off: light1
then:
- light.toggle: light1
- delay: 2s
- switch.turn_on: tvtoggle
- delay: 30s
- light.toggle: light1
- switch.turn_on: tvtoggle

light:

  • platform: binary
    name: “TV led”
    output: led1
    internal: True
    id: light1

output:

  • platform: gpio
    id: led1
    pin: D2

Getting lights current state is not that straightforward, it would certainly not work "off" as a string.

Try just lambda: 'return id(light1).state' if state is available as boolean, otherwise a more complicated (and not documented i.e. may not be supported) solution for sure is possible.
Also it might work for you id you do the id() on the output (led1) not in the light.

Thanks for the help, couldn’t get it to work with that, so … I’ve set an internal switch run the led via gpio and I’ll use the ‘switch.is_on’ condition.

Tested and working fine :slight_smile: