Trying to dim lights, then turn them off after a few second delay

Hello, I am trying to make the lights in my bedroom dim, and then check if the occupancy is clear for a few seconds before turning off. This gives me a chance to activate the sensor again if necessary. I previously tried to just add a delay between dimming and turning off the light, but if I tried to activate the sensor during that delay, it raise the brightness, finish the delay, then turn off. However when I try to do this with an if condition it appears that it doesn’t run even after having the occupancy be clear for more than 10 seconds. I am doing this via the UI, and pasted the YAML below. Any help is appreciated, thank you.

alias: Turn Bedroom Lights Off When Unoccupied
description: When no occupancy is detected in the bedroom, the bedroom lights turn off.
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.aqara_presence_sensor_fp1e_occupancy
    from: null
    to: "off"
conditions:
  - condition: state
    entity_id: light.bedroom_lights
    state: "on"
actions:
  - sequence:
      - action: light.turn_on
        metadata: {}
        data:
          brightness_step_pct: -25
        target:
          entity_id: light.bedroom_lights
      - if:
          - condition: state
            entity_id: binary_sensor.aqara_presence_sensor_fp1e_occupancy
            state: "off"
            for:
              hours: 0
              minutes: 0
              seconds: 10
        then:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.bedroom_lights
mode: single

This automation triggers the very moment the sensor goes to “off.” It immediately gets to the if condition, which fails because the sensor has not been in that state for 10 seconds yet. The automation won’t run again until the state changes away from “off” and back into “off” again. The “if” condition won’t ever be satisfied.

1 Like

To add to @d921’s answer above, neither an if nor its condition checks are a “delay” – it will never wait until its conditions are satisfied. For this to work as intended you would need to use something like a wait_for_trigger.

- wait_for_trigger:
    - trigger: state
      entity_id: binary_sensor.aqara_presence_sensor_fp1e_occupancy
      to_state: "off"
      for:
        seconds: 10
  timeout: 10
- if: "{{ wait.completed }}"
  then:
    - action: light.turn_off
      target:
        entity_id: light.bedroom_lights