I have a bunch of presence and motion sensors (like four, but shush) and when people leave rooms I would like to turn the lights off after five minutes, however if someone returns to the room before that five minutes is up, I'd like to start that countdown again. Is there a grateful way to do this that isn't me just doing if no activity for five minutes, turn off the lights, else wait five minutes and then turn off the lights. Because that's ugly, rigid and not very smart at all.
You should be able to handle this in HA within a single automation:
Trigger: Room is occupied (occupancy > 0)
Condition: Light off
Action:
Call service: Turn Light On
Wait for Trigger: Room is clear for 5 minutes (occupancy < 1)
Call service: Turn Light Off
If you are basing your occupancy on more than one sensor state, you could build a helper to combine the states into a single sensor value, which itself might need a Hysteresis helper.
I've started moving some of my own automations over to this method. It works pretty well, but it is susceptible to being interrupted by restarts or reboots. You may need to build in additional logic to reset things to a known state on startup.
Bloody hell! So I saw this and it was like my vision cleared! I'm actually lost for words, but thank you. I don't know why I didn't realise what wait for trigger did, but holy fuck, you're beautiful, thank you so much!
Are you doing all the logic in HA or are you using NodeRED?
I used to have a similar timer reset for my "going to bed" button that I could cancel/snooze before the events started firing. It was done in NodeRED, though, and HA just glued that logic to the UI and buttons.
The top row is triggered when the motion detector changes to "on". It turns the lights on without delay.
The second row only triggers if the motion detector has been "off" for 5 minutes. If it goes back to "on" within those 5 minutes, the timer starts over automatically.
You can try importing it using the following code:
When I restart mine, I restart HA only, and all of the add-ons (including NodeRED) keep running. The problem is as soon as HA restarts, it blasts all of the states to NodeRED, and for the most part (I think), they all go from "unavailable" to "on/off". This restarts whatever timers NodeRED keeps track of. I don't know of a good way around this, but I restart HA so infrequently that it doesn't matter for me.
No, but you CAN use HA timers in NodeRED. You just have to trigger off the state of the timer itself. I did get it to work but it was quite a bit of spaghetti, I found it easier to use eventID triggers in the standard automation GUI for dealing with persistent timers.
OMG! What type of person are you? I ask a simple question and you introduce me to cool new stuff and now I need to learn all about it. You will be recommending me NodeRed videos for weeks now.
I have something like this setup for my porch lights. Light goes on when it detects motion, then it uses wait_for_trigger to wait until the motion stops before starting a 20 second and turning off the lights.
All simple enough so far but, crucially, the "mode" for the automation is set to restart. That way if the sensor detects motion during the 20 second countdown it cancels the whole run and starts again from the top.