Do `for` checks in actions work after an automation has been called?

Hi all,

Long time lurker, first time poster. I’ve been using HA for about a year now and I’m absolutely loving it!

I hope I can draw on your knowledge for my minor issue which my brain cant seem to read in the documentation (I’m sure its in there :frowning: ).

I`m trying to create an automation which turns off the lights in the hallway after no motion has been detected for > 3 mins. I’m trying to cram as many cases into single automations for the sake of clarity and overview; hence all light on/off cases are handled by a single automation.

I have created below automation, which triggers based on the state change of the sensor.

  • It checks if the motion sensor goes into a detected state
    • if yes, the light turns on
    • Else, the lights should turn off if no motion has been detected for three minutes.

However, the automation is not working as intended.
I suspect that when the automation is triggered by the device’s state change, the for: minutes: 3 is not met and the automation is stopped.

My questions are:

  1. Can a for be used in an action, or only in a trigger?
  2. Is this issue maybe that I have a second if statement within my firstelse? Because I have this construct in more automations then just this one.
  3. How could I solve this issue.

Thank you for any help you could provide.


alias: Hallway Lights Turn On/Off with motion sensor
description: Turns off the hallway lights when no motion has been detected for 10 mins
trigger:
  - platform: state
    entity_id:
      - binary_sensor.lumi_lumi_sensor_motion_aq2_iaszone_2
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition: []
action:
  - if:
      - type: is_motion
        condition: device
        device_id: 83ab09c3ea49234c4d6b789518ec75b3
        entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_iaszone_2
        domain: binary_sensor
    then:
      - service: light.turn_on
        target:
          entity_id: light.hallway_lights_main_lights
        data:
          transition: 5
          brightness_pct: 100
    else:
      - if:
          - type: is_no_motion
            condition: device
            device_id: 83ab09c3ea49234c4d6b789518ec75b3
            entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_iaszone_2
            domain: binary_sensor
            for:
              hours: 0
              minutes: 3
              seconds: 0
        then:
          - service: light.turn_off
            target:
              entity_id: light.hallway_lights_main_lights
            data:
              transition: 10
mode: single
Executed: 6 January 2023 at 08:43:23
Result:

choice: else

## if

Executed: 6 January 2023 at 08:43:23
Result:

result: false

## if/condition/0

### Iteration 1

Executed: 6 January 2023 at 08:43:23
Result:

result: false

## if/condition/0

### Iteration 2

Executed: 6 January 2023 at 08:43:23
Result:

result: false

## if/condition/0/entity_id/0

Executed: 6 January 2023 at 08:43:23
Result:

result: false state: 'off' wanted_state: 'on'

You trigger at the moment binary_sensor.lumi_lumi_sensor_motion_aq2_iaszone_2 changes state, then you check to see if it has been reporting no motion for 3 minutes.

That’s not going to work :wink:

There’s a built in blueprint that does what you want, or you can do something more like this:

alias: Hallway Lights Turn On/Off with motion sensor
description: Turns off the hallway lights when no motion has been detected for 10 mins
trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_iaszone_2
    to: 'on'
action:
  - service: light.turn_on
    target:
      entity_id: light.hallway_lights_main_lights
    data:
      transition: 5
      brightness_pct: 100
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_iaszone_2
        to: 'off'
        for: '00:03:00'
  - service: light.turn_off
    target:
      entity_id: light.hallway_lights_main_lights
    data:
      transition: 10
mode: single
1 Like