Simplify and Reuse Motion Sensor Lighting Flow

I’m looking from some advice on how I might better setup this Node-RED flow to make is reusable for multiple lights and sensors. At the moment it works as-is, but the entire flow has to be duplicated to work properly. I readily admit to being a novice at Node-RED, so something obvious may be escaping me.

The full flow is available here: https://pastebin.com/gvdQXr50
And it looks like this:

The end goal would be for any set of motion sensors, which are dedicated to a specific area of the house with one light, to be able to go through a single flow without the duplication of that flow as I’ve done here. Any takers?

Thanks in advance.

You can achieve this by multiple ways, my two ways are:

Subflow: Subflows : Node-RED (already present in Node-RED).
Component node: node-red-contrib-components (node) - Node-RED (install using the palette in Node-RED).

Also I highly recommend diving in to the flow structure, so you can work with the messages: Working with messages : Node-RED. Since I started playing with this and really figure it out, a whole new world has opened that makes creating flows so much cleaner and more advanced.

Ofcourse, you’ll learn most by just doing and googling other codes and copying them ;). In anycase: using Subflows will be a good starting point and allow you to easily create new flows. Huge benefit to this (applies to Component nodes as well) is that if you edit the ‘Subflow’ contents, then all Flows that you have setup using the Subflow, will change with it. So you don’t need to change all other Flows if you decide to change a small variable (like brightness or timer).

There is a lot going on. I’m not sure why the use of so many input helpers. This my simple flow for motion light @ night only. You can find more examples here

[{"id":"15759f56ec13ba63","type":"api-call-service","z":"bc7b1ecb.5a61d8","name":"","server":"6b1110b5.183a4","version":3,"debugenabled":true,"service_domain":"light","service":"turn_{{payload}}","entityId":"light.rgbcw, light.rgbcw_2","data":"","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1170,"y":880,"wires":[[]]},{"id":"c2596226c7a4b22b","type":"server-state-changed","z":"bc7b1ecb.5a61d8","name":"middle yard motion","server":"6b1110b5.183a4","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.xvr_middle_cross_region_detection","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,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":490,"y":880,"wires":[["53aabf179248f689"],[]]},{"id":"53aabf179248f689","type":"api-current-state","z":"bc7b1ecb.5a61d8","name":"Day/Night","server":"6b1110b5.183a4","version":3,"outputs":2,"halt_if":"above_horizon","halt_if_type":"str","halt_if_compare":"is","entity_id":"sun.sun","state_type":"str","blockInputOverrides":false,"outputProperties":[],"for":0,"forType":"num","forUnits":"minutes","x":720,"y":880,"wires":[[],["d3bc0ad135969819"]],"outputLabels":["Day","Night"],"icon":"font-awesome/fa-question"},{"id":"d3bc0ad135969819","type":"trigger","z":"bc7b1ecb.5a61d8","name":"One and wait","op1":"on","op2":"off","op1type":"str","op2type":"str","duration":"45","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":930,"y":880,"wires":[["15759f56ec13ba63"]]},{"id":"6b1110b5.183a4","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]
1 Like

Thanks for the feedback. I’m looking into subflows now, as well as custom messages.

@mstroud did you ever get anywhere with making this composable and reusable? I am trying to wrap my head around the problem now and kinda spinning my wheels

I did, but I’ve had some issues with it. Right now I’m reconsidering my flows with some new plugins. Here’s what I have but I’m not sure I’d recommend it:

[{"id":"69f4d73ca4b84b08","type":"subflow","name":"Lights w/ Override","info":"","category":"","in":[{"x":60,"y":160,"wires":[{"id":"ed305a652c4e8f78"}]}],"out":[],"env":[],"meta":{},"color":"#DDAA99","status":{"x":2040,"y":80,"wires":[{"id":"83383811b5ed4df3","port":0},{"id":"0fee8606c3952e6f","port":0},{"id":"c5c6766bdf56cf45","port":0}]}},{"id":"20468b3b945fa5b6","type":"change","z":"69f4d73ca4b84b08","name":"Cancel Delay","rules":[{"t":"set","p":"delay","pt":"msg","to":"stop","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":80,"wires":[["8f96fa1b058e3783"]]},{"id":"7896adf9c94c5528","type":"api-call-service","z":"69f4d73ca4b84b08","name":"Light Off","server":"c11bb00b.50dfb","version":5,"debugenabled":false,"domain":"light","service":"turn_off","areaId":[],"deviceId":[],"entityId":["{{flow.lightname}}"],"data":"{\"transition\":msg.lighttrans}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1040,"y":220,"wires":[["2f439d85c335e4a4"]]},{"id":"8f96fa1b058e3783","type":"stoptimer-varidelay","z":"69f4d73ca4b84b08","duration":"120","durationType":"num","units":"Second","payloadtype":"num","payloadval":"0","name":"","reporting":"every_second","persist":false,"ignoretimerpass":false,"x":700,"y":220,"wires":[["fd90acfd32576d83"],[],["44de5d91cf4b9ff4"]]},{"id":"44de5d91cf4b9ff4","type":"api-call-service","z":"69f4d73ca4b84b08","name":"Timer Display","server":"c11bb00b.50dfb","version":5,"debugenabled":false,"domain":"input_text","service":"set_value","areaId":[],"deviceId":[],"entityId":["input_text.time_remains"],"data":"{\"value\":msg.payload}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":840,"y":280,"wires":[[]]},{"id":"79a86dfdb1793718","type":"api-current-state","z":"69f4d73ca4b84b08","name":"get Override","server":"c11bb00b.50dfb","version":3,"outputs":2,"halt_if":"true","halt_if_type":"bool","halt_if_compare":"is","entity_id":"{{flow.override}}","state_type":"habool","blockInputOverrides":false,"outputProperties":[],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":470,"y":220,"wires":[[],["8f96fa1b058e3783"]]},{"id":"f46e53a8a6a0248a","type":"api-call-service","z":"69f4d73ca4b84b08","name":"Timer Name","server":"c11bb00b.50dfb","version":5,"debugenabled":false,"domain":"input_text","service":"set_value","areaId":[],"deviceId":[],"entityId":["input_text.time_sensor"],"data":"{\"value\":msg.topic}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":940,"y":80,"wires":[["c38c44acad00dd87"]]},{"id":"d89b11a80e29ad41","type":"function","z":"69f4d73ca4b84b08","name":"","func":"flow.sensorid=flow.get('msg.entity_id');\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":790,"y":80,"wires":[["f46e53a8a6a0248a"]]},{"id":"2f439d85c335e4a4","type":"delay","z":"69f4d73ca4b84b08","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1200,"y":220,"wires":[["83383811b5ed4df3"]]},{"id":"83383811b5ed4df3","type":"api-call-service","z":"69f4d73ca4b84b08","name":"Light Off","server":"c11bb00b.50dfb","version":5,"debugenabled":false,"domain":"light","service":"turn_off","areaId":[],"deviceId":[],"entityId":["{{flow.lightname}}"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1360,"y":220,"wires":[["0501096fa77bd1d8"]]},{"id":"0501096fa77bd1d8","type":"change","z":"69f4d73ca4b84b08","name":"Reset Flow","rules":[{"t":"set","p":"lightname","pt":"flow","to":"","tot":"str"},{"t":"set","p":"override","pt":"flow","to":"","tot":"str"},{"t":"set","p":"lightname","pt":"global","to":"","tot":"str"},{"t":"set","p":"override","pt":"global","to":"","tot":"str"},{"t":"set","p":"delay","pt":"msg","to":"","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"","tot":"str"},{"t":"set","p":"override","pt":"msg","to":"override","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":1510,"y":220,"wires":[[]]},{"id":"fd90acfd32576d83","type":"api-current-state","z":"69f4d73ca4b84b08","name":"get Override","server":"c11bb00b.50dfb","version":3,"outputs":2,"halt_if":"true","halt_if_type":"bool","halt_if_compare":"is","entity_id":"{{flow.override}}","state_type":"habool","blockInputOverrides":false,"outputProperties":[],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":880,"y":220,"wires":[[],["7896adf9c94c5528"]]},{"id":"60cc36186d667389","type":"switch","z":"69f4d73ca4b84b08","name":"Motion On or Off?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"},{"t":"null"}],"checkall":"true","repair":false,"outputs":3,"x":400,"y":160,"wires":[["20468b3b945fa5b6","d89b11a80e29ad41","c5c6766bdf56cf45"],["79a86dfdb1793718","d89b11a80e29ad41"],[]]},{"id":"ed305a652c4e8f78","type":"change","z":"69f4d73ca4b84b08","name":"","rules":[{"t":"set","p":"lightname","pt":"flow","to":"lightname","tot":"global"},{"t":"set","p":"override","pt":"flow","to":"override","tot":"global"},{"t":"set","p":"units","pt":"msg","to":"seconds","tot":"global"},{"t":"set","p":"override","pt":"msg","to":"override","tot":"flow"},{"t":"set","p":"override2","pt":"msg","to":"override2","tot":"flow"},{"t":"set","p":"lightname","pt":"msg","to":"lightname","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":200,"y":160,"wires":[["60cc36186d667389"]]},{"id":"c38c44acad00dd87","type":"api-call-service","z":"69f4d73ca4b84b08","name":"Override Display","server":"c11bb00b.50dfb","version":5,"debugenabled":false,"domain":"input_text","service":"set_value","areaId":[],"deviceId":[],"entityId":["input_text.override"],"data":"{\"value\":msg.override}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"all","x":1220,"y":80,"wires":[[]]},{"id":"591cde0efb2be778","type":"api-current-state","z":"69f4d73ca4b84b08","name":"get Brightness","server":"c11bb00b.50dfb","version":3,"outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"gt","entity_id":"input_number.light_level","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"lightbright","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":950,"y":160,"wires":[["8e976806785ac991"]]},{"id":"8e976806785ac991","type":"api-current-state","z":"69f4d73ca4b84b08","name":"get Delay","server":"c11bb00b.50dfb","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_number.light_delay","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"delay","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1120,"y":160,"wires":[["1be8da90d7ebbfcd"]]},{"id":"2d4b7d3ee2ee6c85","type":"api-current-state","z":"69f4d73ca4b84b08","name":"get Temp","server":"c11bb00b.50dfb","version":3,"outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"gt","entity_id":"input_number.color_temp","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"lighttemp","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":790,"y":160,"wires":[["591cde0efb2be778"]]},{"id":"0fee8606c3952e6f","type":"api-call-service","z":"69f4d73ca4b84b08","name":"Light On Kelvin","server":"c11bb00b.50dfb","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["{{flow.lightname}}"],"data":"{\"kelvin\":msg.colortemp, \"brightness\":msg.lightbright, \"transition\":msg.lighttrans}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1600,"y":160,"wires":[[]]},{"id":"1be8da90d7ebbfcd","type":"api-current-state","z":"69f4d73ca4b84b08","name":"get Color","server":"c11bb00b.50dfb","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_select.light_color","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"lightcolor","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1280,"y":160,"wires":[["5831b9ec32b1b5e8"]]},{"id":"5831b9ec32b1b5e8","type":"api-current-state","z":"69f4d73ca4b84b08","name":"Get K","server":"c11bb00b.50dfb","version":3,"outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"is","entity_id":"input_number.color_temp","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"colortemp","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1430,"y":160,"wires":[["0fee8606c3952e6f"]]},{"id":"15c633832eaf4e8d","type":"api-call-service","z":"69f4d73ca4b84b08","name":"Light On no kelvin","server":"c11bb00b.50dfb","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["{{flow.lightname}}"],"data":"{\"color_name\":msg.lightcolor, \"brightness\":msg.lightbright, \"transition\":msg.lighttrans}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1530,"y":100,"wires":[[]]},{"id":"c5c6766bdf56cf45","type":"api-call-service","z":"69f4d73ca4b84b08","name":"Light On Adaptive","server":"c11bb00b.50dfb","version":5,"debugenabled":true,"domain":"adaptive_lighting","service":"apply","areaId":[],"deviceId":[],"entityId":["switch.adaptive_lighting_adapt_color_lr_adaptive"],"data":"{\"lights\":msg.lightname, \"turn_on_lights\":true, \"prefer_rgb_color\":true, \"adapt_brightness\":true, \"adapt_color\":true, \"transition\":5}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1050,"y":480,"wires":[[]]},{"id":"c11bb00b.50dfb","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}]