Light automation when motion detected with time out

Hi,
I created an automation using UI, the code is as followed:

alias: Turn WC 1 light on
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 8074edb539e8e478a32df9638e845e30
    entity_id: binary_sensor.motion_sensor_3
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
condition:
  - condition: state
    entity_id: light.toilet_light_1
    state: 'off'
action:
  - service: light.turn_on
    target:
      entity_id: light.toilet_light_1
  - type: is_no_motion
    condition: device
    device_id: 8074edb539e8e478a32df9638e845e30
    entity_id: binary_sensor.motion_sensor_3
    domain: binary_sensor
    for:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
      entity_id: light.toilet_light_1
mode: single

The automated on working without any issues but the turn off cannot work. I need to turn off the light after no motion is detected during 2 mins, is there any wrong in my code?

Yes. As soon as that device condition in the actions evaluates to false the actions stop being run. Your light will never turn off.

Try this:

alias: Turn WC 1 light on
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_3
    to: on
action:
  - service: light.turn_on
    target:
      entity_id: light.toilet_light_1
  - delay:
      minutes: 2
  - service: light.turn_off
    target:
      entity_id: light.toilet_light_1
mode: restart

The condition is incorrect. If you want to wait until the binary_sensor has been off for 2 minutes, you should consider using wait_for_trigger

alias: Turn WC 1 light on
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 8074edb539e8e478a32df9638e845e30
    entity_id: binary_sensor.motion_sensor_3
    domain: binary_sensor
action:
  - service: light.turn_on
    target:
      entity_id: light.toilet_light_1
  - wait_for_trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_3
      to: 'off'
      for: '00:02:00'
  - service: light.turn_off
    target:
      entity_id: light.toilet_light_1
mode: restart

EDIT

Replaced:

mode: single
max_exceeded: silent

with:

mode: restart
4 Likes

Ahh. That’s right, while the switch state is on, the automation stopped it self.

Thank you. I need wait_for_trigger when the sensor off after 2 minutes. The mode should be “restart” instead of “single”.

wait_for_trigger is not a good option. You cannot move your body continuously to keep the sensor on. Whenever you are stop moving, this automation start the timer and turn off the light in 2 mins after.
Have you got any other solution for that?

It is doing exactly what you asked for in your first post:

So how do you want it to work now?

Of cause I need it work. Wait for trigger start the timer when no motion is detected but I need another function to reset the timer if the sensor detect a motion again. Do you have any idea?

Thank you.

Replace this:

mode: single
max_exceeded: silent

with this:

mode: restart

Whenever motion is detected, the entire automation will be restarted (meaning the previous wait_for_trigger is cancelled).


NOTE

I updated the example to use restart.

1 Like

Thank you. I’ll try this!

May i ask why you need restart here? Doesn’t the wait_for_trigger already do everything you need in this automation?