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.
It’s also important to set the mode of the automation to “restart”, then it works flawlessly
Aha! THAT’S what that does. Thank YOU too!
I don’t want to set a misunderstanding: this does not solve the state on reboot issue, maybe “flawlessly” is not the correct word. On a reboot, all automations are always stopped, so that does not help here.
No, no. I do sometimes have to contend with race conditions and reentrant triggers. I never considered looking into whether it was possible to change the execution mode, even if I did read that documentation without understanding what it meant.
Thanks for the pointer back to it. Now I grok it.
What do you mean? Is that different than
repeat until
?
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!❤️
hi logically i thing you are on the right track. A timer is defiantly the right way to go. You can try my blueprint for your automation. Hope it helps
Hey, this is pretty cool! I need to study this, I’m trying to get the idea of blueprints into my head.
Thank you, I imported it and plan to learn from it.
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.
OP would be able to accomplish this in NodeRED pretty easily. It’s probably pretty easy in HA too, but my brain only works with NodeRED haha.
Why do you love NodeRed so much?
I’ve been using HA for about 7 or 8 years (maybe longer?). Everything used to be YAML only, including automations. Now you can set up devices in the GUI, make automations in the GUI, etc., but I learned NodeRED when things weren’t as streamlined. I’ve never bothered to learn HA automations because NodeRED is so great.
How do you feel about the progress that Home Assistant has seen while you’ve been using it? And looking at the climbing adoption, you must feel like a proud parent?
It has come a long way, absolutely.
There was a while where if you didn’t check the change logs before updating, you’d likely be in for some surprises. That’s pretty rare now.
I don’t really feel like a proud parent because I haven’t contributed much, even in the way of bug reports. I’ve just been along for the ride.
You say that, but the last update broke my thermostat with their new climate controls and also some of my lights needed switching to the light service from the switch service.
The thermostat backend didn’t change, just the card right? It looks different but didn’t lose any functionality. Sorry you had trouble with it. I have 4 or 5 different thermostats and they never stopped working.
How did you get into NodeRed?
I was complaining to a friend about how clunky it was to setup automations in HA, and they told me about NodeRED. lol. Haven’t looked back.
This was before the UI or recently?
About 5 years ago, so probably before.
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.
In NodeRED it would look like this:
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:
NodeRED code dropdown
[ { "id": "1fca03dc3d9e7066", "type": "server-state-changed", "z": "f3446183.7e46a", "name": "Motion On", "server": "144bcad2.81b925", "version": 5, "outputs": 2, "exposeAsEntityConfig": "", "entityId": "", "entityIdType": "exact", "outputInitially": false, "stateType": "str", "ifState": "on", "ifStateType": "str", "ifStateOperator": "is", "outputOnlyOnStateChange": true, "for": "0", "forType": "num", "forUnits": "minutes", "ignorePrevStateNull": false, "ignorePrevStateUnknown": false, "ignorePrevStateUnavailable": false, "ignoreCurrentStateUnknown": false, "ignoreCurrentStateUnavailable": false, "outputProperties": [ { "property": "payload", "propertyType": "msg", "value": "", "valueType": "entityState" }, { "property": "data", "propertyType": "msg", "value": "", "valueType": "eventData" }, { "property": "topic", "propertyType": "msg", "value": "", "valueType": "triggerId" } ], "x": 80, "y": 1160, "wires": [ [ "d01a14f220a57b71" ], [] ] }, { "id": "d01a14f220a57b71", "type": "api-call-service", "z": "f3446183.7e46a", "name": "Lights On", "server": "144bcad2.81b925", "version": 5, "debugenabled": false, "domain": "", "service": "", "areaId": [], "deviceId": [], "entityId": [], "data": "", "dataType": "jsonata", "mergeContext": "", "mustacheAltTags": false, "outputProperties": [], "queue": "none", "x": 300, "y": 1160, "wires": [ [] ] }, { "id": "5737d7e58d22e1a6", "type": "api-call-service", "z": "f3446183.7e46a", "name": "Lights Off", "server": "144bcad2.81b925", "version": 5, "debugenabled": false, "domain": "", "service": "", "areaId": [], "deviceId": [], "entityId": [], "data": "", "dataType": "jsonata", "mergeContext": "", "mustacheAltTags": false, "outputProperties": [], "queue": "none", "x": 300, "y": 1200, "wires": [ [] ] }, { "id": "b203774e1f8bea1f", "type": "server-state-changed", "z": "f3446183.7e46a", "name": "Motion Off for 5", "server": "144bcad2.81b925", "version": 5, "outputs": 2, "exposeAsEntityConfig": "", "entityId": "", "entityIdType": "exact", "outputInitially": false, "stateType": "str", "ifState": "off", "ifStateType": "str", "ifStateOperator": "is", "outputOnlyOnStateChange": true, "for": "5", "forType": "num", "forUnits": "minutes", "ignorePrevStateNull": false, "ignorePrevStateUnknown": false, "ignorePrevStateUnavailable": false, "ignoreCurrentStateUnknown": false, "ignoreCurrentStateUnavailable": false, "outputProperties": [ { "property": "payload", "propertyType": "msg", "value": "", "valueType": "entityState" }, { "property": "data", "propertyType": "msg", "value": "", "valueType": "eventData" }, { "property": "topic", "propertyType": "msg", "value": "", "valueType": "triggerId" } ], "x": 100, "y": 1200, "wires": [ [ "5737d7e58d22e1a6" ], [] ] }, { "id": "144bcad2.81b925", "type": "server", "name": "Home Assistant", "version": 5, "addon": true, "rejectUnauthorizedCerts": true, "ha_boolean": "y|yes|true|on|home|open", "connectionDelay": true, "cacheJson": true, "heartbeat": false, "heartbeatInterval": 30, "areaSelector": "friendlyName", "deviceSelector": "friendlyName", "entitySelector": "friendlyName", "statusSeparator": "at: ", "statusYear": "hidden", "statusMonth": "short", "statusDay": "numeric", "statusHourCycle": "h23", "statusTimeFormat": "h:m", "enableGlobalContextStore": true } ]
Do NodeRED timers survive a restart?
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.
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.
Thank you!
@sabreW4K3
Create a timer that you set to 5 minutes.
Create an automation with 3 triggers:- Motion is detected
- Motion clear
- Timer changes to idle
Choice of three actions:
When 1:
Switch light on
Cancel timer (this takes care of motion being detected again during the 5 minute delay)When 2:
Start timer (And let’s wait for 5. Minutes for timer to end)When 3:
Check that motion is clear (as motion also cancels the timer)
If so, switch light offThis broke my brain at first. I had to keep coming back to it. Thank you so much.