Is there a better way to do this for these and a lot more sensors

You could probably use a subflow to put all the common logic in one place, then use an input for which ‘area’ motion was sensed in. Check out this post: https://thewindev.net/creating-subflows-in-node-red

I get that concept but I’m still new to node-red and could not find an example. I tried to find a way to send in the message payload what sensor was triggered.

A lot of assumptions were made here but if your entity ids are similarly named you can use one flow it all.

sensor.garage_motion
input_number.garage_duration
timer.garage_timer
input_boolean.garage_occupied

[{"id":"cd36b5cb.a755c8","type":"server-state-changed","z":"ffbd7f06.4a014","name":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor\\.[a-z_]+_motion","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":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":214,"y":1536,"wires":[["c038e599.b92058"],[]]},{"id":"36d94c9b.e35354","type":"api-call-service","z":"ffbd7f06.4a014","name":"","version":1,"debugenabled":false,"service_domain":"timer","service":"start","entityId":"timer.{{topic}}_timer","data":"{\"duration\": $number(payload) * 60}","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":832,"y":1536,"wires":[[]]},{"id":"c038e599.b92058","type":"change","z":"ffbd7f06.4a014","name":"","rules":[{"t":"change","p":"topic","pt":"msg","from":"sensor.","fromt":"str","to":"","tot":"str"},{"t":"change","p":"topic","pt":"msg","from":"_motion","fromt":"str","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":1536,"wires":[["15ac5be7.e79644","d7bde128.ee367"]]},{"id":"15ac5be7.e79644","type":"api-current-state","z":"ffbd7f06.4a014","name":"get duration","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_number.{{topic}}_duration","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"","override_data":"none","blockInputOverrides":false,"x":662,"y":1536,"wires":[["36d94c9b.e35354"]]},{"id":"d7bde128.ee367","type":"api-call-service","z":"ffbd7f06.4a014","name":"","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_on","entityId":"input_boolean.{{topic}}_occupied","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":712,"y":1584,"wires":[[]]}]

If you don’t have similar named entity ids you can map the motion sensor to the others.

[{"id":"1b9feb85.7d2df4","type":"function","z":"ffbd7f06.4a014","name":"map entities","func":"const map = {\n    \"sensor.garage_motion\": {\n        duration: \"input_number.garage_duration\",\n        timer: \"timer.garage_timer\",\n        occupied: \"input_boolean.garage_occupied\"\n    },\n    \"sensor.guest_bedroom_motion\": {\n        duration: \"input_number.guest_bedroom_duration\",\n        timer: \"timer.guest_bedroom_timer\",\n        occupied: \"input_boolean.guest_bedroom_occupied\"\n    }\n}\nmsg.map = map[msg.topic];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":1872,"wires":[["6de535a2.e4aa0c","5cec3638.c08708"]]},{"id":"963a5062.ab0d2","type":"server-state-changed","z":"ffbd7f06.4a014","name":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor\\.[a-z_]+_motion","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":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":294,"y":1872,"wires":[["1b9feb85.7d2df4"],[]]},{"id":"91cbb639.4aca98","type":"api-call-service","z":"ffbd7f06.4a014","name":"","version":1,"debugenabled":false,"service_domain":"timer","service":"start","entityId":"{{map.timer}}","data":"{\"duration\": $number(payload) * 60}","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":912,"y":1872,"wires":[[]]},{"id":"6de535a2.e4aa0c","type":"api-current-state","z":"ffbd7f06.4a014","name":"get duration","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"{{map.duration}}","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"","override_data":"none","blockInputOverrides":false,"x":742,"y":1872,"wires":[["91cbb639.4aca98"]]},{"id":"5cec3638.c08708","type":"api-call-service","z":"ffbd7f06.4a014","name":"","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_on","entityId":"{{map.occupied}}","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":792,"y":1920,"wires":[[]]}]
1 Like

Since I don’t know what your function node and some of the others are doing I just made a subflow that accepts an area entity id and a duration and sends a push notification. Hopefully importing and experimenting with it should give you an understanding of subflows with inputs.

[{"id":"7a979687.e896d","type":"api-call-service","z":"bf441e68.a8a5a8","name":"Send notification","server":"b454cc2c.3067a","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_your_phone","entityId":"","data":"{\t   \"message\": payload.entity_id,\t   \"title\": payload.duration \t}","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":520,"y":100,"wires":[[]]},{"id":"b454cc2c.3067a","type":"server","name":"Home Assistant","legacy":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

Blueprint?
Subflows ?