[Solved] Automation: motion activated light doesn't turn off when timer is added

Hi - I’ve made an automation to turn on light when motion is detected, and off again when no motion is detected.
Now i would like to delay turn off for a short time.
I thought it was as simple as to add a time to the condition like this

but when i do this the light never turns off.
If i clear the time - it works again.

The full automation that is working look like this:

alias: Autolys Akvarie Lab og Baggang
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.philips_sml001_occupancy
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.philips_sml001_occupancy
            state: "on"
        sequence:
          - action: light.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: light.akvarie_lab_og_baggang
      - conditions:
          - condition: state
            entity_id: binary_sensor.philips_sml001_occupancy
            state: "off"
        sequence:
          - action: light.turn_off
            metadata: {}
            data:
              transition: 10
            target:
              entity_id: light.akvarie_lab_og_baggang
mode: single

When i add 15sec delay - and it never turns off:

alias: Autolys Akvarie Lab og Baggang
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.philips_sml001_occupancy
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.philips_sml001_occupancy
            state: "on"
        sequence:
          - action: light.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: light.akvarie_lab_og_baggang
      - conditions:
          - condition: state
            entity_id: binary_sensor.philips_sml001_occupancy
            state: "off"
            for:
              hours: 0
              minutes: 0
              seconds: 15
        sequence:
          - action: light.turn_off
            metadata: {}
            data:
              transition: 10
            target:
              entity_id: light.akvarie_lab_og_baggang
mode: single

Isn’t this the right way?

alias: Autolys Akvarie Lab og Baggang
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.philips_sml001_occupancy
    state: on
  - trigger: state
    entity_id:
      - binary_sensor.philips_sml001_occupancy
    state: off
    for:
      seconds: 15
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.philips_sml001_occupancy
            state: "on"
        sequence:
          - action: light.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: light.akvarie_lab_og_baggang
      - conditions:
          - condition: state
            entity_id: binary_sensor.philips_sml001_occupancy
            state: "off"
            for:
              hours: 0
              minutes: 0
              seconds: 15
        sequence:
          - action: light.turn_off
            metadata: {}
            data:
              transition: 10
            target:
              entity_id: light.akvarie_lab_og_baggang
mode: single

Thank for taking time to help me with this.
I do see it works to move the timer up to the trigger, and it isn’t needed conditions. But i don’t understand why.
I’ve added another motion sensor to the automation, and it is working.
When i create the automation, the YAML looks a bit different.
But this is working :slightly_smiling_face:

alias: Autolys Akvarie Lab og Baggang
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.philips_sml001_occupancy
    from: null
    to: "on"
  - trigger: state
    entity_id:
      - binary_sensor.ikea_motion_sensor_2_motion
    from: null
    to: "on"
  - trigger: state
    entity_id:
      - binary_sensor.ikea_motion_sensor_2_motion
    from: null
    to: "off"
    for:
      hours: 0
      minutes: 3
      seconds: 0
  - trigger: state
    entity_id:
      - binary_sensor.philips_sml001_occupancy
    from: null
    to: "off"
    for:
      hours: 0
      minutes: 3
      seconds: 0
    enabled: true
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.philips_sml001_occupancy
            state: "on"
        sequence:
          - action: light.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: light.akvarie_lab_og_baggang
      - conditions:
          - condition: state
            entity_id: binary_sensor.ikea_motion_sensor_2_motion
            state: "on"
        sequence:
          - action: light.turn_on
            metadata: {}
            data: {}
            target:
              area_id: akvarie_lab
      - conditions:
          - condition: and
            conditions:
              - condition: state
                entity_id: binary_sensor.ikea_motion_sensor_2_motion
                state: "off"
              - condition: state
                entity_id: binary_sensor.philips_sml001_occupancy
                state: "off"
        sequence:
          - action: light.turn_off
            metadata: {}
            data:
              transition: 10
            target:
              area_id: akvarie_lab
mode: single

The reason your didn’t work was because it triggers at 0 seconds but your condition says it has to be 15 seconds.
That’s why it fails

That makes sense - I thought the trigger was fired then the action would get delayed.
Thank you for enlightening me :slightly_smiling_face: