Motion detection and how to translate "for" to node-red

Not sure if it is of any help, but here my solution for motion.
During night time it is in a dimmed state, while on daytime it is 100%
Also, any additional motion will reset the timer, as well as any manual setting.

[{"id":"4e20e179.bea5","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"d8114e57.9d8eb","type":"mqtt in","z":"4e20e179.bea5","name":"Motion Hallway Downstairs","topic":"TasmotaBRG1/tele/RESULT","qos":"2","datatype":"auto","broker":"f90cfe3b.a8bd7","x":170,"y":240,"wires":[["19b03f1f.ea05f1"]]},{"id":"19b03f1f.ea05f1","type":"switch","z":"4e20e179.bea5","name":"Only turn on","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"D41D0E","vt":"str"}],"checkall":"true","repair":true,"outputs":1,"x":370,"y":240,"wires":[["1d3f3e59.58f192"]]},{"id":"a0833ac2.a3d378","type":"function","z":"4e20e179.bea5","name":"Turn on 15%","func":"msg.payload = {\n    domain: \"light\",\n    service: \"turn_on\",\n    data: {\n        entity_id: \"light.light_hallway_downstairs\",\n        \"brightness_pct\": 15\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":710,"y":220,"wires":[["beb1310.34a90d","97e7ca59.f908c8"]]},{"id":"1bacd546.236b7b","type":"function","z":"4e20e179.bea5","name":"Turn on 100%","func":"msg.payload = {\n    domain: \"light\",\n    service: \"turn_on\",\n    data: {\n        entity_id: \"light.light_hallway_downstairs\",\n        \"brightness_pct\": 100\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":720,"y":260,"wires":[["97e7ca59.f908c8","beb1310.34a90d"]]},{"id":"97e7ca59.f908c8","type":"switch","z":"4e20e179.bea5","name":"Only turn on","property":"payload.service","propertyType":"msg","rules":[{"t":"cont","v":"turn_on","vt":"str"}],"checkall":"true","repair":true,"outputs":1,"x":910,"y":260,"wires":[["59aca2ae.60293c"]]},{"id":"beb1310.34a90d","type":"function","z":"4e20e179.bea5","name":"Reset 3m Delay","func":"msg.delay = 180000;\nvar m1 = {reset:true};\nreturn [[m1,msg]];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":920,"y":220,"wires":[["67d8d3f8.40b06c"]]},{"id":"67d8d3f8.40b06c","type":"delay","z":"4e20e179.bea5","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1100,"y":220,"wires":[["68a89c09.ce2d44"]]},{"id":"68a89c09.ce2d44","type":"function","z":"4e20e179.bea5","name":"Turn off","func":"msg.payload = {\n    domain: \"light\",\n    service: \"turn_off\",\n    data: {\n        entity_id: \"light.light_hallway_downstairs\"\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1280,"y":220,"wires":[["59aca2ae.60293c"]]},{"id":"59aca2ae.60293c","type":"api-call-service","z":"4e20e179.bea5","name":"payload light","server":"37d68302.2aabcc","version":1,"debugenabled":false,"service_domain":"homeassistant","service":"","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1230,"y":260,"wires":[[]]},{"id":"1d3f3e59.58f192","type":"time-range-switch","z":"4e20e179.bea5","name":"","lat":"51.83125","lon":"4.33552","startTime":"sunset","endTime":"sunrise","startOffset":0,"endOffset":0,"x":540,"y":240,"wires":[["a0833ac2.a3d378"],["1bacd546.236b7b"]]},{"id":"db81c53e.497538","type":"server-state-changed","z":"4e20e179.bea5","name":"","server":"37d68302.2aabcc","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.light_hallway_downstairs","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":610,"y":180,"wires":[["beb1310.34a90d"],[]]},{"id":"f90cfe3b.a8bd7","type":"mqtt-broker","z":"","name":"","broker":"hassio.aceindy.nl","port":"1883","clientid":"node-red","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""},{"id":"37d68302.2aabcc","type":"server","z":"","name":"Home Assistant"}]
3 Likes

I’ll post up later when I get a chance. I can’t remember if the trigger node works this way, but the stoptimer node is great because the timer gets reset with each message and it can also be stopped. So let’s say the first motion event turns the light on, each subsequent event resets the off timer. Send a stop command to leave the light on. Stoptimer3 is unique in that you can also change the duration.

1 Like

Here’s a slimmed down example. Here I’m just controlling a simple on/off light switch:

[{"id":"edbb6ac7.d702b8","type":"change","z":"8c99a79b.f26338","name":"Stop","rules":[{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":764,"y":425,"wires":[["ee0addbb.644e"]]},{"id":"8abf5432.db6398","type":"api-call-service","z":"8c99a79b.f26338","name":"Set the lighting mode to motion","server":"d0cb210d.98634","version":1,"debugenabled":false,"service_domain":"input_select","service":"select_option","entityId":"input_select.master_bathroom_shower_lighting_mode","data":"{\"option\":\"Motion\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1405,"y":289,"wires":[[]]},{"id":"1aeabcfc.c27893","type":"api-call-service","z":"8c99a79b.f26338","name":"Set the lighting mode to idle","server":"d0cb210d.98634","version":1,"debugenabled":false,"service_domain":"input_select","service":"select_option","entityId":"input_select.master_bathroom_lighting_mode","data":"{\"option\":\"Idle\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1167,"y":493,"wires":[[]]},{"id":"fce2eed9.71b98","type":"api-current-state","z":"8c99a79b.f26338","name":"What is the lighting mode?","server":"d0cb210d.98634","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_select.master_bathroom_shower_lighting_mode","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":517,"y":316,"wires":[["9a2eda38.0b3398"]]},{"id":"7d1ec536.b0a98c","type":"server-state-changed","z":"8c99a79b.f26338","name":"Lighting mode changes","server":"d0cb210d.98634","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_select.master_bathroom_shower_lighting_mode","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":334,"y":465,"wires":[["ed484dcb.ba9f2"]]},{"id":"9a2eda38.0b3398","type":"switch","z":"8c99a79b.f26338","name":"Idle or Motion?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Idle","vt":"str"},{"t":"eq","v":"Motion","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":757,"y":316,"wires":[["7669cd23.3cd964"],["edbb6ac7.d702b8"]]},{"id":"7669cd23.3cd964","type":"api-current-state","z":"8c99a79b.f26338","name":"What's the current location mode?","server":"d0cb210d.98634","version":1,"outputs":2,"halt_if":"Away","halt_if_type":"str","halt_if_compare":"is_not","override_topic":false,"entity_id":"input_select.location_mode","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1057,"y":316,"wires":[["8abf5432.db6398","cca8863e.aa24e8"],[]]},{"id":"9e63c2e4.aae3d","type":"trigger-state","z":"8c99a79b.f26338","name":"Motion detected in the shower","server":"d0cb210d.98634","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.multi_sensor_master_bathroom_motion_2","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"gbh2tvonieh","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"id":"dtnaav82ioi","targetType":"entity_id","targetValue":"input_select.master_bathroom_lighting_mode","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"does_not_include","comparatorValueDatatype":"list","comparatorValue":"Locked, Manual"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":207,"y":316,"wires":[["fce2eed9.71b98"],[]]},{"id":"ed484dcb.ba9f2","type":"switch","z":"8c99a79b.f26338","name":"Locked or manual?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Locked","vt":"str"},{"t":"eq","v":"Manual","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":564,"y":465,"wires":[["edbb6ac7.d702b8"],["711560dc.60c46"]]},{"id":"3edbc48b.98bcec","type":"trigger-state","z":"8c99a79b.f26338","name":"Motion is no longer detected in the shower","server":"d0cb210d.98634","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.multi_sensor_master_bathroom_motion_2","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"8bb5d69y26p","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"},{"id":"hb5e2q1tuj","targetType":"entity_id","targetValue":"input_select.master_bathroom_shower_lighting_mode","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"does_not_include","comparatorValueDatatype":"list","comparatorValue":"Manual, Locked"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":503,"y":527,"wires":[["f2f75fc7.fad25"],[]]},{"id":"cca8863e.aa24e8","type":"api-call-service","z":"8c99a79b.f26338","name":"Turn on the can lights","server":"d0cb210d.98634","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.switch_master_bathroom_cans","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1377,"y":336,"wires":[[]]},{"id":"ff0b6e1a.f19fd","type":"api-call-service","z":"8c99a79b.f26338","name":"Turn off the can lights","server":"d0cb210d.98634","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.switch_master_bathroom_cans","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1147,"y":442,"wires":[[]]},{"id":"ee0addbb.644e","type":"stoptimer3","z":"8c99a79b.f26338","duration":"5","durationType":"num","units":"Minute","payloadtype":"num","payloadval":"0","name":"Stoptimer","x":937,"y":476,"wires":[["ff0b6e1a.f19fd","1aeabcfc.c27893"],[]]},{"id":"f2f75fc7.fad25","type":"change","z":"8c99a79b.f26338","name":"Duration","rules":[{"t":"set","p":"duration","pt":"msg","to":"3","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":767,"y":527,"wires":[["ee0addbb.644e"]]},{"id":"711560dc.60c46","type":"change","z":"8c99a79b.f26338","name":"Duration","rules":[{"t":"set","p":"duration","pt":"msg","to":"60","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":767,"y":476,"wires":[["ee0addbb.644e"]]},{"id":"d0cb210d.98634","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

This works off the principle that a room can have modes of operation. In my case, I set mine up with four modes: idle (waiting for an automation to turn it on), motion (on due to a motion event), manual (on because I turned it on, but reset to idle after 1 hour of inactivity), and locked (no automations allowed). I use an input_select helper for this.

Depending on the mode, a motion event turns on the light (assuming someone is home). If the motion sensor times out and returns to clear, the time starts. If a new motion event is registered, a stop message is sent to the stoptimer, telling it to stop the countdown. As soon as the motion clears, the stoptimer starts over.

In this case, a motion clear event sets the duration for a few minutes, and switching to a manual room mode sets the duration for 1 hour. Also note, setting the room to locked stops the timer - constraints on the other nodes also prevent the flow from turning on the lights when locked. This takes care of the situation that can arise with the stoptimer where you change the level or something and then three minutes later it releases the message and messes up the lighting.

If the stoptimer successfully makes it to the end of the countdown, it sends the original message on, turning off the light and resetting the room mode to idle.

I have flavors of this that include multiple motion triggers, as well as control for dimmers based on room illuminance. I’m getting really close to never touching the lights again. I think the last thing to do is leverage the timed-counter node to gradually increase duration as more motion events are observed in a given time frame, effectively locking out a room that is heavily occupied. Like during a party. Or when my kids are going ape shit.

4 Likes


Unfortunattely I can’t see the settings of the one I was most interested. The stoptimer. I don’t know why but as you can see it doesn’t identify the node.
Can you send me a print screen of one that you use?

I was hopping to do it without using functions because I don’t know how and what to write in.

Thank you though for your suggestion. I hope one day I will be able to use something like that.

You need to download the node using the manage palettes feature. Click the three bar menu at the top right next to the deploy button. Select the manage pallette option, then click the install tab. In the search box, type stoptimer3, then click install.

You can re import that flow and it should come through clean now.

1 Like

For what it’s worth and anyone interested, here’s how I handle motion detection and other ‘triggers’ for lighting (and some other stuff).

Basically everything is splitted for optimal re-use, meaning i’ve got some kind of ‘scenes’ for my lights, with defined states, colors, brightnesses, etc.
Apart from that there are some ‘triggers’ which will call these scenes if required.
To keep everything organised, they are in separate tabs within nodered and are linked with ‘link in’ and ‘link out’ “wireless” nodes.

The movement sensors (1 or multiple per room) events are being received by node-red and this toggles an input-boolean to ON. After a certain time of no-movement, the input-boolean will be toggled off again.
This is also easy to re-use by other automations.

Based on these motion&activity toggles, certain scenes will be called for the lights.

Some example pictures: (‘Overloop’ is dutch for ‘hallway first floor’)
Tab: Movement & Activity:
Here, motion is detected and toggles the input-boolean ‘Movement_Overloop’, remaining ON for 5 minutes after last motion detection.

Tab: Light Triggers Floor 1:
Here, the change in input-boolean ‘Movement_Overloop’ is detected and the correct actions are triggered accordingly. In this case, turning the lights in this area ON (bright) during the day, turning them ON (Nightlight mode) between sunset and sunrise, and turning them OFF if the input-boolean changed to OFF.
Note the link-out is the end-result for this tab, the actual switching of the lights happens somewhere else.

Tab: Light Scenes Floor 1:
The before mentioned ‘somewhere else’ is here. A link-in triggers to start changing the light-settings of certain lights. All setting of the lights is done in separate template-nodes, this is for the sake of reusibility of the light settings (brightness, color, transition, etc) to multiple lights.

The usage of the template-node seems odd and not really necessary in this example, but makes thing highly more scalable and usable in different scenarios, i.e. in my living room where about 8 separate lights are being changed mixed through each other, based on different scenarios.
An example of how the Light-scenes Floor 0 tab looks like (in Dutch, i’m sorry) :


As you can see, a lot is going on here, but everything stays neatly organised which makes it easy to maintain and expand, one of the key-factors of me being very satisfied with this system.

2 Likes

I managed to have the lights turn on and off as I initially wanted. Meaning if there is no movement for x minutes, then close them.


What I am trying to do now is before turning them off, to check if the TV is ON. If yes, don’t close the lights, until the TV turns off.

The problem is with the node “wait until” (wait 5 min TV to be off). I can’t get it to work.
Any suggestions?

A quick question not very related to node red.
What happens with the input booleans in case HA restarts? They start with the state being off by default?

Hi Andy,
This looks awesome and exactly what I need. Just one problem… me!
Any chance you could help me out with what the input selects components would look like in your config YAML file so I can get the same working for me?
I would be really grateful. About changing servers, node types, entity IDs, and a few basics are about all I can muster.
Appreciate you.

Hey man! I configure all of my helpers in the GUI, so no YAML config for me on them.

1 Like

OMG! Thanks man. Appreciate it! Didn’t know you could do it through the GUI :man_facepalming: