One common requirement with home automation is to turn a light on with motion and then off again after a set period of no motion. I tried several different ways of doing this but always got stuck. Motion sensors tend to have a little sleep when the state changes. So it detects motion and then it waits. Sometimes this can be up to 2 minutes. The other issue, without starting to complicate automations is that a timer running will stop other things from happening. So, the lights come on. The timer starts. The automation is then sat in a situation where it won’t do anything until the timer finishes, including looking for more motion, if that makes sense.
What happens is that your lights go out after 5 minutes but it’s quite possible that your sensor is sleeping at this point so, despite the fact that you’re dancing around like a lunatic, the lights stay off until the sensor wakes up again.
I use Node-RED for my automations. If you do too, head over to Pastebin and grab this flow. Import it into Node-RED. What you’ll see is that the flow is split into 2 parts. 1 for motion and 1 for no motion. The no motion part is looking to see if there is no motion for, in my case, 5 minutes. Double click on either the motion or no motion nodes at the beginning to adjust the time. You’ll have to check each node anyway as your entity names will be different.
The other thing you’ll need to do is delete the 4 nodes in the middle that look like this image.
Then join the motion nodes back to the nodes that turn the lights on. They’re something I use which I’ll go into later.
The rough flow is:
And this is a video showing the flow running in Node-RED. You’ll need your sound on to hear me explaining what’s going in. Watch the labels under the “Motion on” and “Motion off” nodes on the left hand side. Interestingly, you’ll also see just how quickly the state changes on the flow when you hear me moving to activate it. This one has a few additional nodes in there. The “Office Automation” nodes are explained here. The “Lux < 20” one just means that the automation will only run when the light level is below a certain value. The “Is it night?” node is looking to see what mode the house is in. In this case, I have things set so that this automation won’t turn the lights on in the night as I have a switch for that instead. This is mainly because the room gets used as a spare bedroom sometimes and I don’t want the lights coming on if someone is in there and turns over in bed.