Need help with Condition For

Hi, I was wondering if someone could help me with this. Reading through the forums the for condition where I have it won’t work but I don’t know how to format to get it to work.

What I’m after is for a condition that if the state of the sensor is off for 30 seconds then the light turns off. Is this doable or should I just use 2 automations, one for on and the other for off with a trigger?

  - alias: 'Front Porch Auto Light On/Off'
    trigger: 
      - platform: state
        entity_id: binary_sensor.doorbell_person_detected
    action:
      - choose:
        - conditions:
          - condition: state
            entity_id: binary_sensor.doorbell_person_detected
            state: "on"
          - condition: sun
            after: sunset
            after_offset: "-01:00:00"
          sequence:
            - service: light.turn_on
              entity_id: light.entry_light
        - conditions:
          - condition: state
            entity_id: binary_sensor.doorbell_person_detected
            state: "off"
            for:
              seconds: 30
          sequence:
            - service: light.turn_off
              entity_id: light.entry_light

Hello and welcome to the forum. Please have a read of point 11 here and edit your post accordingly.

The condition requiring the binary_sensor to be off for 30 seconds will never be true. Why not? The condition is looking for 30 seconds of “off” but that will never occur because the trigger occurs the instant the binary_sensor changes to off.

1 Like

Try this.

alias: ‘Front Porch Auto Light On/Off’
trigger:
  - platform: state
    entity_id: binary_sensor.doorbell_person_detected
    to: 'on'
action:
  - choose:
      - conditions:
          - condition: sun
            after: sunset
            after_offset: “-01:00:00”
        sequence:
          - service: light.turn_on
            entity_id: light.entry_light

          - wait_template: "{{ is_state('binary_sensor.doorbell_person_detected','off') }}"

          - delay: 30

          - service: light.turn_off
            entity_id: light.entry_light
1 Like

Thanks, that makes sense.

Thanks. Didn’t know about wait template. Below is what I ended up with, seems like it’s working. One more question though, is there any advantage of using the choose function when there is only one outcome?

  - alias: 'Front Porch Auto Light On/Off'
    trigger: 
      - platform: state
        entity_id: binary_sensor.doorbell_person_detected
        to: 'on'
    mode: restart
    condition:
      - condition: sun
        after: sunset
        after_offset: "-01:00:00"
    action:
      - service: light.turn_on
        entity_id: light.entry_light
      - wait_template: "{{ is_state('binary_sensor.doorbell_person_detected','off') }}"
      - delay: 30
      - service: light.turn_off
        entity_id: light.entry_light

No. I could and should have put that in the condition block which would prevent the automation from firing at all unless the condition was met. I was just quickly offering a solution, guess I didn’t look it over too well. Nice catch.

1 Like

You should know that the example posted above doesn’t do this:

It waits until the binary_sensor changes to off and then immediately waits for 30 seconds before turning off the light. Basically, the moment someone stops moving, the countdown begins. It’s not the same as requiring the binary_sensor to maintain an ‘off’ state for a minimum of 30 seconds which implies no one is present for at least 30 seconds.

In addition, I believe the suggested automation should have mode: restart (unless specified, it defaults to using mode: single). In its current form, if motion occurs again during the 30 second delay, it will be ignored and the light will be turned off even though someone is still present.