IF AND in lambda

Hi folks,

I have a CYD using some code I found online which works well.

I want to update thew code so that the screen backlight stays on if an input boolean helper in HA is set to on and reverts back to turning off automatically if the helper is off

The original code is:

  - interval: 45s
    then:
      - if:
          condition:
            - lambda: |-
                return !id(recent_touch);
          then: 
            light.turn_off:
              id: backlight
          else:
            - lambda: |-
                id(recent_touch) = false;

and I have the helper imported from HA with:

  - platform: homeassistant
    name: "Desk presence"
    id: deskState
    entity_id: input_boolean.desk_occupied
    internal: false

I want to update the code to something like this but code that actually works :slight_smile:

  - interval: 45s
    then:
      - if:
          condition:
            - lambda: |-
                return !id(recent_touch) && !id(deskState);
          then: 
            light.turn_off:
              id: backlight
          else:
            - lambda: |-
                id(recent_touch) = false;

Hopefully that makes sense.

Thanks!

You are clipping your code the way we can’t see what components you are using…

binary_sensor:
  - platform: homeassistant
    name: "Desk presence"
    id: deskState
    entity_id: input_boolean.desk_occupied
    internal: false

you can use if(id(deskState).state)

Take a look at on_press and on_release.

Add them to your imported binary sensor helper.

If you want a delayed off look at delayed_off:.