First, sorry it took so long but I have recovered from a crash this weekend due to a faulty SSD cable, so I have lost some work and had to go back to an older project, but I think it will give more insight. Okay, as to the flow of this node, it is part of a routine that, based on presence will turn lights on or off and when turning them off will provide a warning first.
[{"id":"70fef1d0c5ee98b6","type":"comment","z":"593fb70375108267","name":"Disable Perms if user_id = user","info":"Zodra er in in (automation) groep wordt ingegrepen Perm eraf","x":670,"y":1840,"wires":[]},{"id":"d8cf07494168e65c","type":"server-events","z":"593fb70375108267","name":"","server":"2fba4297.e4145e","version":2,"eventType":"state_changed","exposeToHomeAssistant":false,"eventData":"","haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"}],"event_type":"","x":160,"y":1920,"wires":[["4e5f7f800423eecf"]]},{"id":"4e5f7f800423eecf","type":"switch","z":"593fb70375108267","name":"1=Light,2=Presence","property":"payload.entity_id","propertyType":"msg","rules":[{"t":"cont","v":"light","vt":"str"},{"t":"cont","v":"input_boolean.presence","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":380,"y":1920,"wires":[["7c9af3e0e9d7a2fd"],["b5e8e2180d5bbb56"]]},{"id":"7c9af3e0e9d7a2fd","type":"function","z":"593fb70375108267","name":"What permission to turn off","func":"var entity = msg.payload.entity_id;\nvar user_id = msg.payload.context.user_id;\nvar parent_id = msg.payload.context.parent_id;\n\nvar bas_id = \"011714f9a5034d9ea20d8c731238a231\";\nvar ellen_id = \"b95355d6c20b42f090420c0c4a71f303\";\nvar supervisor_id = \"c073a75c7cbf4ff896c4043e4710d8ab\";\nmsg.data = \"\"\nconst area_arr =[\n/**1 */ {naam: \"woonkmr\", area: \"woonkamer\"},\n/**2 */ {naam: \"badkmr\", area: \"badkamer\"},\n/**3 */ {naam: \"slaapkmr\", area: \"slpkamer\"},\n/**4 */ {naam: \"werkkmr\", area: \"werkkamer\"},\n/**5 */ {naam: \"kastenkmr\", area: \"kastenkamer\"},\n/**6 */ {naam: \"logeerkmr\", area: \"logeerkamer\"},\n/**7 */ {naam: \"greenrm\", area: \"greenroom\" },\n ];\nvar len = area_arr.length; \nif (entity.includes(\"light\")) {\n for (let k = 0; k < len; k++) {\n var tmpstrg = area_arr[k].naam;\n if (entity.includes(tmpstrg)) {\n var area = area_arr[k].area;\n entity = \"input_boolean.automate_perm_\" + area_arr[k].naam;\n }\n /** Als Bas of Ellen iets instelt dan automation gelijk af. */\n if (user_id == bas_id || user_id == ellen_id) {\n\n msg.topic = \"\";\n msg.payload = { domain: \"input_boolean\", service: \"turn_off\", data: { \"entity_id\": entity } };\n msg.topic = entity;\n node.send(msg);\n } /** Als Node-red het doet is user_id die van supervisor */\n else if (user_id == supervisor_id) {\n /** do something */\n } /** Als HA automation het doet is user_id en PArent_id null */\n else if (user_id == null) {\n /** do something */\n }\n }\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":1880,"wires":[[]]},{"id":"dfc3913b4f315354","type":"link out","z":"593fb70375108267","name":"Not Present, lights out?","mode":"link","links":["4c2e5c492dd799e3","85f3456b6b8d2242"],"x":915,"y":2000,"wires":[]},{"id":"652a8a42b0afd895","type":"comment","z":"593fb70375108267","name":"Listen to HA","info":"","x":130,"y":1840,"wires":[]},{"id":"b5e8e2180d5bbb56","type":"function","z":"593fb70375108267","name":"update house Array (hard coded)","func":"var loaded = msg.payload;\nvar entity = msg.payload.entity_id;\nvar state = msg.payload.event.new_state.state;\nif(state == \"on\" || state == true){\n state = \"on\";\n var msg1 = {}; \n msg1.topic = \"start_light\";\n msg1.payload = entity;\n} else{\n state = \"off\";\n var msg2 = {};\n msg2.topic = msg.topic;\n msg2.payload = loaded;\n}\n\nif (entity.includes(\"woonk\")){\nglobal.set (\"house.room[1].presence\", state);\n}\nif (entity.includes(\"badk\")) {\n global.set(\"house.room[2].presence\", state);\n}\nif (entity.includes(\"werkk\")) {\n global.set(\"house.room[4].presence\", state);\n}\nif (entity.includes(\"greenr\")) {\n global.set(\"house.room[6].presence\", state);\n}\n\nreturn [msg1, msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":1980,"wires":[[],["dfc3913b4f315354","556575b152c63244"]]},{"id":"d29859edde27580f","type":"comment","z":"593fb70375108267","name":"Verdeel op inhoud","info":"","x":370,"y":1880,"wires":[]},{"id":"eb350e0cb674eae7","type":"comment","z":"593fb70375108267","name":"Status Aanwezigheid","info":"","x":630,"y":1940,"wires":[]},{"id":"85f3456b6b8d2242","type":"link in","z":"593fb70375108267","name":"not present = lights out","links":["4b833fecc1ca8d63","de1b0ff328a7a137","b46d50c732b6cbf3","dfc3913b4f315354"],"x":175,"y":2220,"wires":[["556575b152c63244"]]},{"id":"4a08ca0a53c8eedc","type":"function","z":"593fb70375108267","name":"Warn and off msg","func":"var entity = msg.payload.entity_id;\nvar staat = msg.payload.event.new_state.state;\nmsg.data = \"\"\nconst area_arr = [\n/**1 */ { naam: \"woonkmr\", area: \"woonkamer\" },\n/**2 */ { naam: \"badkmr\", area: \"badkamer\" },\n/**3 */ { naam: \"slaapkmr\", area: \"slpkamer\" },\n/**4 */ { naam: \"werkkmr\", area: \"werkkamer\" },\n/**5 */ { naam: \"kastenkmr\", area: \"kastenkamer\" },\n/**6 */ { naam: \"logeerkmr\", area: \"logeerkamer\" },\n/**7 */ { naam: \"greenrm\", area: \"greenroom\" },\n];\nvar len = area_arr.length;\nif (entity.includes(\"input_boolean.presence\")) {\n for (let k = 0; k < len; k++) {\n var tmpstrg = area_arr[k].naam;\n if (entity.includes(tmpstrg)) {\n var area = area_arr[k].area;\n }\n }\n\n msg.topic = \"no_pres_warn1 \" + area;\n msg.payload = { domain: \"light\", service: \"turn_on\", data: { \"area_id\": area, \"brightness_step_pct\": 10 } };\n node.send(msg);\n \n msg.topic = \"warn2\";\n msg.payload = { domain: \"light\", service: \"turn_on\", data: { \"area_id\": area, \"brightness_step_pct\": -10 } };\n node.send(msg);\n \n msg.topic = \"dim \" + area;\n msg.payload = { domain: \"light\", service: \"turn_on\", data: { \"area_id\": area, \"brightness_step_pct\": -40, \"transition\": 10 } };\n node.send(msg);\n\n msg.topic = \"out \" + area;\n msg.payload = { domain: \"light\", service: \"turn_off\", data: { \"area_id\": area } };\n node.send(msg);\n\n\n}\nreturn null;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":260,"y":2140,"wires":[["8318d08ea3351352"]]},{"id":"8318d08ea3351352","type":"switch","z":"593fb70375108267","name":"warn/dim/out","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"warn","vt":"str"},{"t":"cont","v":"dim","vt":"str"},{"t":"cont","v":"out","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":480,"y":2140,"wires":[["76459aeb039958ae"],["9df9feb3b4cee5c8"],["bb7ced8334b965f5"]]},{"id":"76459aeb039958ae","type":"trigger","z":"593fb70375108267","name":"Warning!","op1":"","op2":"","op1type":"pay","op2type":"payl","duration":"5","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":670,"y":2100,"wires":[["d92e97d64e7d7d0c"]]},{"id":"bb7ced8334b965f5","type":"delay","z":"593fb70375108267","name":"","pauseType":"delay","timeout":"4","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":670,"y":2180,"wires":[["0101d8586298b625"]]},{"id":"556575b152c63244","type":"switch","z":"593fb70375108267","name":"","property":"payload.event.new_state.state","propertyType":"msg","rules":[{"t":"eq","v":"off","vt":"str"},{"t":"eq","v":"on","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":260,"y":2220,"wires":[["4a08ca0a53c8eedc","2a14de80f6032584"],["54d148dee81c090b"]]},{"id":"0101d8586298b625","type":"q-gate","z":"593fb70375108267","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"peek","dropCmd":"drop","statusCmd":"status","maxQueueLength":"100","keepNewest":false,"qToggle":false,"persist":false,"storeName":"default","x":800,"y":2240,"wires":[["d92e97d64e7d7d0c"]]},{"id":"d92e97d64e7d7d0c","type":"link out","z":"593fb70375108267","name":"Turn Off Empty Area","mode":"link","links":["f7901627d7e8fd14"],"x":835,"y":2160,"wires":[]},{"id":"2a14de80f6032584","type":"change","z":"593fb70375108267","name":"open","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"open","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":2180,"wires":[["0101d8586298b625"]]},{"id":"54d148dee81c090b","type":"change","z":"593fb70375108267","name":"close","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"close","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":2220,"wires":[["0101d8586298b625"]]},{"id":"9df9feb3b4cee5c8","type":"delay","z":"593fb70375108267","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":670,"y":2140,"wires":[["0101d8586298b625"]]},{"id":"3ddea2940ecd53f3","type":"server-state-changed","z":"593fb70375108267","name":"Motion Greenroom","server":"2fba4297.e4145e","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.pir_greenrm_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":false,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"on","valueType":"str"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"},{"property":"data","propertyType":"msg","value":"","valueType":"triggerId"}],"x":170,"y":1720,"wires":[["de04624d97ed95f1"],["5443bddcce39016f"]]},{"id":"de04624d97ed95f1","type":"change","z":"593fb70375108267","name":"Set presence.greenrm","rules":[{"t":"set","p":"house.room[6].presence","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":1720,"wires":[["a6765c68a5e2a079"]]},{"id":"eaed52b88d0969cf","type":"countdown","z":"593fb70375108267","name":"","topic":"presence greenrm","payloadTimerStart":"","payloadTimerStartType":"nul","payloadTimerStop":"off","payloadTimerStopType":"str","timer":"1800","resetWhileRunning":true,"outputOnReset":false,"setTimeToNewWhileRunning":true,"startCountdownOnControlMessage":true,"x":570,"y":1740,"wires":[["de04624d97ed95f1"],[]]},{"id":"5443bddcce39016f","type":"function","z":"593fb70375108267","name":"Set Greenroom timer","func":"msg.data = msg.data;\nmsg.topic = \"control\"\nvar pay = global.get(\"house.room[6].presence_timer\");\npay = parseFloat(pay) *60;\nmsg.payload = pay;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":1740,"wires":[["eaed52b88d0969cf"]]},{"id":"a6765c68a5e2a079","type":"function","z":"593fb70375108267","name":"Trigger SC","func":"var service = msg.payload;\nif (service == true || service == \"on\"){\n service = \"on\";\n} else if (service == false || service == \"off\"){\n service = \"off\";\n}\nvar entity = msg.data;\nif (entity == undefined){\n entity = msg.topic\n}\nflow.set(\"pres_enti\", entity);\nmsg.payload = null;\n\nconst area_arr = [\n/**1 */ { naam: \"woonkmr\", area: \"woonkamer\", objnr: 1 },\n/**2 */ { naam: \"badkmr\", area: \"badkamer\", objnr: 2 },\n/**3 */ { naam: \"slpkmr\", area: \"slaapkamer\", objnr: 3 },\n/**4 */ { naam: \"werkkmr\", area: \"werkkamer\", objnr: 4 },\n/**5 */ { naam: \"kastenkmr\", area: \"kastenkamer\", objnr: 5 },\n/**6 */ { naam: \"logeerkmr\", area: \"logeerkamer\", objnr: 7 },\n/**7 */ { naam: \"greenrm\", area: \"greenroom\", objnr: 6 },\n/**8 */ { naam: \"primelamp\", area: \"greenroom\" },\n/**9 */ { naam: \"bureaulamp\", area: \"greenroom\" }\n];\nvar lengte = area_arr.length;\nfor (let k = 0; k < lengte; k++) {\n var chkstrg = area_arr[k].naam;\n \n if (entity.includes(chkstrg)) {\n var naam = area_arr[k].naam;\n var area = area_arr[k].area;\n entity = \"input_boolean.presence_\" + naam;\n flow.set(\"entity[\" + k + \"]\", entity);\n msg.topic = \"bool\";\n msg.payload = { domain: \"input_boolean\", service: \"turn_\"+ service, data: { \"entity_id\": entity } };\n node.send(msg); \n }\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":970,"y":1760,"wires":[["aae1d9c349cfdeb8"]]},{"id":"aae1d9c349cfdeb8","type":"api-call-service","z":"593fb70375108267","name":"Input Boolean {Payload}","server":"2fba4297.e4145e","version":5,"debugenabled":false,"domain":"input_boolean","service":"{{service}}","areaId":[],"deviceId":[],"entityId":[],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"data"}],"queue":"none","x":1190,"y":1800,"wires":[[]]},{"id":"2fba4297.e4145e","type":"server","name":"Home Assistant BasAdmin","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true,"heartbeat":true,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]
(it is a bit messy, it's old)
The motion sensor triggers presence and a "off" timer, the events node listens to changes of the boolean (Home Assistant Helper), when nobody is in the room (according to PIR) the lights will flash, then dim and the the whole area will switch off. If after first flash or dim movement is detected, the routine will stop and lights will restore.
To provide a key example of where this hangs to most, is in the wait step, using a countdown (right now I'm thinking of going for the delay, but have to recalculate the timer settings then). The countdown node will overwrite your payload, without any option (besides context) of retaining that data.
And this wil go for more nodes, so in order to maintain information from begin to end, without using context which poses it's own set of challenges, I have to move payload to data, plus, when there already is data in msg.data find a way to "layer" that retention. The payload contains the actionable data, the data is meta in my view and as such it may not be needed in one hop, from one node to another, it may well be needed later in the flow. So whilst I do use the context for more of the non-volatile data ( and data that is needed in more flows), the use of the msg construct is needed, simply because using context for that many variables would be madness. So now you need a switch or function node after an "action" node to move around data. Why not provide an option to "retain original message" and move msg.payload to msg.data, at which time the results of the node can be stored in payload again.