I have this simple automation that detects motion in a room, or if a door into the room has been opened and then turns on 2 lights, both via smart plugs:
There is then a 5 minute delay and the lights go out again.
I was hoping that by making the mode: restart, it would effectively refresh the 5 minute timer if people are moving about in the room, but every now and then, despite being active in the room, the lights go out after 5 minutes.
I think this is because the automation isnt actually triggered again unless the motion sensor returns to a clear state first, which it never is when people are moving around.
This will turn on when motion is detected and turn off after 5 minutes. Unless more motion is detected, then the timer will reset.
Remove the delay from your automation and just look at the binary sensor. When it goes on and its dark - turn on the lights. When it goes off - turn off the lights:
alias: Kitchen - Lights On when motion detected or door state changed
description: ""
trigger:
- platform: state
entity_id: binary_sensor.motion_detected
to: ['on', 'off']
from: ['on', 'off']
action:
- choose:
- alias: Motion detected and its dark
condition:
- "{{ trigger.to_state.state == 'on' }}"
- condition: numeric_state
entity_id: sensor.lumi_lumi_motion_ac02_illuminance
below: input_number.kitchen_illuminance_threshold
sequence:
- type: turn_on
device_id: eaa3e410315ce8d9b83da37ea83335a5
entity_id: switch.tradfri_plug_kitchen_leds_switch
domain: switch
- type: turn_on
device_id: 956725bc947f0414fa6e41ca315ee017
entity_id: switch.tradfri_plug_kitchen_lamp_switch
domain: switch
- alias: Motion stopped for 5 min
condition: "{{ trigger.to_state.state == 'off' }}"
sequence:
- type: turn_off
device_id: eaa3e410315ce8d9b83da37ea83335a5
entity_id: switch.tradfri_plug_kitchen_leds_switch
domain: switch
- type: turn_off
device_id: 956725bc947f0414fa6e41ca315ee017
entity_id: switch.tradfri_plug_kitchen_lamp_switch
domain: switch
As an added bonus, this way it won’t break when you restart HA. Restarting HA (or modifying this particular automation) stops the automation wherever it is. Which means if its running it probably stops during the delay. It doesn’t resume after that so your lights just get left on.
Trigger template sensors survive restarts so this automation and sensor combo works fine in those cases.
I am using timers in this cases, if a new movement is detected while the timer is still active i restart the timer to the origin value (say 10 minutes), so it will start a new count.
Whats interesting is that the automation hangs on the wait for trigger, and my hunch is because of the three minute condition.
Looking at my automation traces, I see the following:-
Automation start at 08:15:23 then since then has paused at 08:15:24 on the wait for trigger.
Looking at the motion sensors updates, I have the following:-
Cleared (no motion detected)
08:17:23 - 21 minutes ago
Detected motion
08:15:23 - 23 minutes ago
So motion was detected and the automation fired, but the motion sensor sent a clear state only 2 minutes later, and nothing after that (everyone left the room). But actually, 21 minutes have now passed since that clear state, and thats what I thought the automation would check against for the 3 minute condition i set. It actually looks like it needs another ‘clear’ message after the three minutes for that condition to be satisfied, which is not what I was expecting.
That sets a timeout of zero, but not to continue past the trigger if there is a timeout.
So basically… do not pass the wait state until no motion is detected, no matter how long it takes to detect it.
I used the UI to add the wait trigger and its added that part automatically. I don’t think thats the issue.
Not sure whats happening there. If you use the UI to create the wait for trigger action, it automatically adds a timeout section and if you slide the ‘continue on timeout’ to false, but leave the rest as 00:00:00 it clearly confuses it.
I’m on mobile now but basically you need two triggers in the same automation.
One for motion, and one for no motion for x minutes.
Both need different trigger id’s. “id: motion” and “id: no-motion” for example.
Then you make different “choose” actions where you use the trigger id’s as condition.
Here is an example that triggers both for door open and closed and performs different actions depending on which trigger that fired the automation:
This means never timeout. By setting all these to zeroes you told the automation to wait for the trigger indefinitely, no limit on how long it should wait.
@CentralCommand - that’s exactly what I thought it would do. But if I remove the entire timeout section from my automation it works as intended. (I assume no timeout section at all is basically the same as what I had with zeroes and false)