Nested if statement help

Hi,

Looking for a little help, I’m trying to setup a light automation that will turn the lights off after a certain amount of time with the timeout varying depending on the time of night. I’ve set it up using the GUI but I’ve pasted the YAML below.

Any help is appreciated.

alias: Ensuite Lights Off
description: ""
trigger:
  - type: no_motion
    platform: device
    device_id: cdfc0e9129eac4ce312f885b1e0ee86e
    entity_id: binary_sensor.lumi_lumi_motion_ac02_764a4e00_ias_zone
    domain: binary_sensor
    for:
      hours: 0
      minutes: 2
      seconds: 0
condition: []
action:
  - if:
      - condition: time
        weekday:
          - sun
          - sat
          - fri
          - thu
          - wed
          - tue
          - mon
        after: "22:59:59"
        before: "05:30:00"
      - type: is_no_motion
        condition: device
        device_id: cdfc0e9129eac4ce312f885b1e0ee86e
        entity_id: binary_sensor.lumi_lumi_motion_ac02_764a4e00_ias_zone
        domain: binary_sensor
        for:
          hours: 0
          minutes: 1
          seconds: 0
    then:
      - type: turn_off
        device_id: f28f2c471af2b9dfbac70387b1034dba
        entity_id: light.hey_smart_bulb
        domain: light
      - type: turn_off
        device_id: c514899deb68b75102905c09b4e10621
        entity_id: light.hey_smart_bulb_2
        domain: light
    else:
      - if:
          - type: is_no_motion
            condition: device
            device_id: cdfc0e9129eac4ce312f885b1e0ee86e
            entity_id: binary_sensor.lumi_lumi_motion_ac02_764a4e00_ias_zone
            domain: binary_sensor
            for:
              hours: 0
              minutes: 3
              seconds: 0
        then:
          - type: turn_off
            device_id: f28f2c471af2b9dfbac70387b1034dba
            entity_id: light.hey_smart_bulb
            domain: light
          - type: turn_off
            device_id: c514899deb68b75102905c09b4e10621
            entity_id: light.hey_smart_bulb_2
            domain: light
mode: single

The nested if checks if the motion sensor has detected no motion for 3 minutes. That will never be true because the automation triggers when there’s no motion for 2 minutes.

Ah, ok. I was trying to do something along the lines of …

IF no motion for 2 mins & it’s between 11pm & 5.30 am turn off after a further 1min (3min total)
ELSE turn off after a further 3min (5min total).

I know I could probably achieve it easier with 2 separate automations, was just trying to get it all done in 1.

You used a State Condition and it doesn’t wait for something to happen. If the motion sensor hasn’t been off for 3 minutes at the moment the State Condition checks it then it immediately reports false.

There are other means of “waiting for something to happen” while in an automation’s action or in a script.

Scripting: Wait