Creating Motion-Activated Lights with Timer Reset


I'm working on my first automation project using Node-RED, and I need assistance in creating a flow that controls my lights based on motion detection. Before I proceed, let me address the guidelines:

  1. Research: Yes, I have done some research on Node-RED basics of motion sensor integration but I was unable to find or understand the solutions given.

  2. Explanation of Attempts: I've attempted to create a basic flow, but I'm stuck on the logic for handling the timers and restarting them when motion is detected. Some people wrote that using a stoptimer node is what I'm looking for, but I'm not understanding how to implement them, so they achieve this automation.

  3. Platform and Version: I'm using Node-RED version 14.5.0 as an Addon to Home Assistant OS.

Let me explain my project in detail:

I want to create a Node-RED flow that accomplishes the following:

  1. When motion is detected
  2. turn on the scene that turns on light.
  3. If no motion is detected for 90 seconds, dim the lights.
    3.1. If however motion is detected within these 90 seconds, reset the timer from step 3.
  4. If no motion is detected for an additional 30 seconds after dimming, turn off the lights.
    4.1 If however motion is detected during these 30 seconds, undim the lights and start over from step 2.

For a more visual explanation, look at the screenshot provided.

I know, the "no motion" current state notes are wrong. They only act to visualize the flow, I want to accomplish.

I've already set up my motion sensor and lights in Node-RED, but I'm struggling with how to create the logic to handle the timers and motion detection events.

Question: Could you please help me design the Node-RED flow that accomplishes this motion-activated lighting control scenario? I'm specifically looking for guidance on how to structure the flow, use timers effectively, and trigger events based on motion detection.

Additionally, if there are any specific Node-RED nodes or plugins that would be useful for this project, please let me know.

Thank you for your assistance.

Hello @kineci and welcome to the forum.

I don't personally use Home Assistant so I (and most others on this forum) don't really understand it.

Looking at your diagram, every node but one seems to be part of Home Assistant.
The exception "If Night" is confusing too because it has the logo of a Switch node but the colour of a Function node.

So it's very hard to suggest alterations to your flow.

Here is a very simple flow purely in Node-red which seems to do what you want (I've changed the durations to 9sec and 3 sec for the example).
I don't know how you would apply this in your HA setup because I don't know what data HA sends when movement is sensed.

EDIT: - added switch node to route ON, DIM and OFF messages

[{"id":"f1e0fad3ba6790fe","type":"inject","z":"315c5276902454be","name":"Motion ","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ON","payloadType":"str","x":90,"y":60,"wires":[["54dc4cec1727d462"]]},{"id":"54dc4cec1727d462","type":"trigger","z":"315c5276902454be","name":"Send ON, wait 9s, DIM","op1":"ON","op2":"DIM","op1type":"str","op2type":"str","duration":"9","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":280,"y":60,"wires":[["0f16a2789d1788ca","184e477cbf1eaeae"]]},{"id":"0f16a2789d1788ca","type":"trigger","z":"315c5276902454be","name":"Send nothing, wait 3s, OFF","op1":"","op2":"OFF","op1type":"nul","op2type":"str","duration":"3","extend":false,"overrideDelay":false,"units":"s","reset":"ON","bytopic":"all","topic":"topic","outputs":1,"x":300,"y":120,"wires":[["184e477cbf1eaeae"]]},{"id":"e312408f0b34a7f3","type":"debug","z":"315c5276902454be","name":"ON","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":40,"wires":[]},{"id":"184e477cbf1eaeae","type":"switch","z":"315c5276902454be","name":"ON, DIM or OFF?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"ON","vt":"str"},{"t":"eq","v":"DIM","vt":"str"},{"t":"eq","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":550,"y":80,"wires":[["e312408f0b34a7f3"],["46283fb43ff9d006"],["4e7dcf5c717a133e"]]},{"id":"46283fb43ff9d006","type":"debug","z":"315c5276902454be","name":"DIM","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":80,"wires":[]},{"id":"4e7dcf5c717a133e","type":"debug","z":"315c5276902454be","name":"OFF","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":120,"wires":[]}]

Note that the first trigger node has "Extend delay if new message arrives" checked.
The second trigger has "reset if messge.payload == ON"

1 Like

Thanks a lot. Using the trigger node definitely pointed me in the right direction. I eventually created something that works exactly as expected using this guide, even though I didn't understand what I was doing 60% of the time. :smile:

I'm glad you got it working.

I started watching the video you linked but it was going mostly over my head.
It seems that Home Assistant has a concept of "state" and I am not sure there is any equivalent in Node-red.