Or condition in lambda "If"

Hi i would like to test for 2 entities states in an if statemente in a lambda call:

esphome:
  name: $devicename
  on_boot:
    priority: -200
    then:
      - switch.turn_on: "${upper_devicename}_led"
      - delay: 10s
      - lambda: |-
          if(id(wake_esp).state)  {
            id(dsleep1).prevent_deep_sleep();
          }
          if(id(wakelocal).state) {
            id(dsleep1).prevent_deep_sleep();
          }

Instead of 2 separated if statements, is it possible to combine both in same if with an “or” condition?

Since you don’t know any C++ why not just stick to using YAML?

if:
  any:
    - binary_sensor.is_on: wake_esp
    - binary_sensor.is_on: wakelocal
then:
  deep_sleep.prevent: dsleep1

Why?

if(i == 2 || i == 4) {

Because all examples i saw are using lamdas. This is why i was thinking that IF statement could not be used in board “on_boot:” section.
So i will change it to yaml.
Thanks

So the correct C++ sintax should be:

esphome:
  name: $devicename
  on_boot:
    priority: -200
    then:
      - switch.turn_on: "${upper_devicename}_led"
      - delay: 10s
      - lambda: |-
          if(id(wake_esp).state || id(wakelocal).state)) {
            id(dsleep1).prevent_deep_sleep();
          }

I have coded it in Yaml:

    if:
      any:
        binary_sensor.is_on: wake_esp
        binary_sensor.is_on: wakelocal
    then:
      deep_sleep.prevent: dsleep1

But i get this error:

Duplicate key "binary_sensor.is_on"
  in "/config/esphome/espcorella.yaml", line 55, column 9
NOTE: Previous declaration here:
  in "/config/esphome/espcorella.yaml", line 54, column 9

It compiles nicely

  on_boot:
    priority: -200
    then:
      - switch.turn_on: "${upper_devicename}_led"
      - delay: 10s
      - lambda: |-
          if(id(wake_esp).state || id(wakelocal).state)  {
            id(dsleep1).prevent_deep_sleep();
          }

Thanks

You’re welcome.
I think the correct form for any would be

      any:
        - binary_sensor.is_on: wake_esp
        - binary_sensor.is_on: wakelocal
1 Like