FYI… some time ago i redid the whole thing, and its now much simpler… at least to me:
Using MQTT messages as states for triggering.
[{"id":"914a9b73.c28298","type":"subflow","name":"Motion Timeout 10s","info":"","category":"","in":[{"x":60,"y":140,"wires":[{"id":"38ecdf53.b21a"}]}],"out":[{"x":680,"y":140,"wires":[{"id":"ed97cd7e.57ca7","port":0}]}],"env":[],"color":"#DDAA99"},{"id":"38ecdf53.b21a","type":"function","z":"914a9b73.c28298","name":"on/off","func":"var stop = {payload:\"STOP\"}\n\nif (msg.payload === \"on\") {\n return [ msg, stop ];\n} else {\n return [ null, msg ];\n}\nreturn msg\n\n","outputs":2,"noerr":0,"x":270,"y":140,"wires":[["ed97cd7e.57ca7"],["2ce8c212.97a0ce"]]},{"id":"e5afe76f.aa45a8","type":"inject","z":"914a9b73.c28298","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":90,"y":80,"wires":[["38ecdf53.b21a"]]},{"id":"deefcb6a.ad8a38","type":"inject","z":"914a9b73.c28298","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":90,"y":40,"wires":[["38ecdf53.b21a"]]},{"id":"2ce8c212.97a0ce","type":"stoptimer","z":"914a9b73.c28298","duration":"10","units":"Second","payloadtype":"str","payloadval":"","name":"","x":430,"y":180,"wires":[["ed97cd7e.57ca7"],[]]},{"id":"ed97cd7e.57ca7","type":"rbe","z":"914a9b73.c28298","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":590,"y":140,"wires":[[]]},{"id":"2a67c903.a2c366","type":"mqtt in","z":"8957e05a.44595","name":"","topic":"NodeRed/Motion/Kitchen/Off/10sec","qos":"1","datatype":"auto","broker":"38f11bdf.cd0904","x":180,"y":560,"wires":[["cbd97dc8.64f5e"]]},{"id":"26879474.f8338c","type":"api-current-state","z":"8957e05a.44595","name":"Mode","server":"2fba4297.e4145e","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_select.mode","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":870,"y":560,"wires":[["5ec8949d.e1445c"]]},{"id":"5ec8949d.e1445c","type":"switch","z":"8957e05a.44595","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Tag","vt":"str"},{"t":"eq","v":"Nacht","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1010,"y":560,"wires":[["b08ef913.0576e8"],["14ae7314.4279ad"]]},{"id":"39ef33f.26896cc","type":"mqtt in","z":"8957e05a.44595","name":"","topic":"NodeRed/Motion/Kitchen/Off/30sec","qos":"1","datatype":"auto","broker":"38f11bdf.cd0904","x":180,"y":680,"wires":[["f9c71a1b.ac3e28"]]},{"id":"130db47a.4b065c","type":"function","z":"8957e05a.44595","name":"on/off?","func":"newmsg = {};\nif (msg.payload == \"off\")\n{\n newmsg.payload = \"True\";\n}else {\n newmsg.payload = \"cancel\";\n// global.set(\"motion\", 0);\n// flow.set(\"motion\", 0);\n}\n\nvar today = new Date();\nvar date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();\nvar time = today.getHours() + \":\" + today.getMinutes() + \":\" + today.getSeconds();\nvar dateTime = date+' '+time;\nnode.status({fill:\"green\",shape:\"dot\",text:dateTime});\n\nreturn newmsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":470,"y":100,"wires":[["7fe9d8ba.734f28","fae5a5ed.d11338"]]},{"id":"7fe9d8ba.734f28","type":"mytimeout","z":"8957e05a.44595","name":"20min","outtopic":"","outsafe":" ","outwarning":"","outunsafe":"20","warning":"900","timer":"1200","debug":false,"ndebug":false,"ignoreCase":false,"repeat":false,"again":false,"x":610,"y":100,"wires":[["5a0b9c6e.8581e4"],[]]},{"id":"5a0b9c6e.8581e4","type":"switch","z":"8957e05a.44595","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"20","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":770,"y":100,"wires":[["aba3b31c.d826a"],[]]},{"id":"aba3b31c.d826a","type":"mqtt out","z":"8957e05a.44595","name":"No Motion for 20min","topic":"NodeRed/Motion/Kitchen/Off/20min","qos":"1","retain":"","broker":"38f11bdf.cd0904","x":1020,"y":100,"wires":[]},{"id":"666c519d.5242a","type":"subflow:914a9b73.c28298","z":"8957e05a.44595","name":"","x":290,"y":100,"wires":[["130db47a.4b065c"]]},{"id":"6a6c0e1f.bcacf","type":"debug","z":"8957e05a.44595","name":"motion kitchen","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":280,"y":160,"wires":[]},{"id":"fae5a5ed.d11338","type":"mytimeout","z":"8957e05a.44595","name":"30/10sec","outtopic":"","outsafe":" ","outwarning":"10","outunsafe":"30","warning":"20","timer":"30","debug":false,"ndebug":false,"ignoreCase":false,"repeat":false,"again":false,"x":620,"y":160,"wires":[["642645e9.fb267c"],[]]},{"id":"642645e9.fb267c","type":"switch","z":"8957e05a.44595","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"10","vt":"num"},{"t":"eq","v":"30","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":770,"y":160,"wires":[["fdac22c6.e16f5"],["d79712a.d93c7f"]]},{"id":"fdac22c6.e16f5","type":"mqtt out","z":"8957e05a.44595","name":"No Motion for 10sec","topic":"NodeRed/Motion/Kitchen/Off/10sec","qos":"1","retain":"","broker":"38f11bdf.cd0904","x":1020,"y":160,"wires":[]},{"id":"d79712a.d93c7f","type":"mqtt out","z":"8957e05a.44595","name":"No Motion for 30sec","topic":"NodeRed/Motion/Kitchen/Off/30sec","qos":"1","retain":"","broker":"38f11bdf.cd0904","x":1020,"y":220,"wires":[]},{"id":"5d466d1.23fbc94","type":"api-current-state","z":"8957e05a.44595","name":"Mode","server":"2fba4297.e4145e","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_select.mode","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":870,"y":340,"wires":[["867d8183.a6f84"]]},{"id":"867d8183.a6f84","type":"switch","z":"8957e05a.44595","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Tag","vt":"str"},{"t":"eq","v":"Nacht","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1010,"y":340,"wires":[["74710206.3c721c"],["189d1c85.de9db3"]]},{"id":"e764f6d8.7aeb88","type":"mqtt in","z":"8957e05a.44595","name":"","topic":"NodeRed/Motion/Kitchen/Off/20min","qos":"1","datatype":"auto","broker":"38f11bdf.cd0904","x":180,"y":800,"wires":[["e58bd513.a71f28"]]},{"id":"58deb4e9.19295c","type":"comment","z":"8957e05a.44595","name":"Lights Automation based on Mode, Motion, Light","info":"","x":180,"y":20,"wires":[]},{"id":"55600b8d.25ed34","type":"comment","z":"8957e05a.44595","name":"Detect Motion - Trigger MQTT when off","info":"","x":190,"y":60,"wires":[]},{"id":"f52db1f1.30e3","type":"comment","z":"8957e05a.44595","name":"Turn on Light if conditions are met","info":"","x":180,"y":300,"wires":[]},{"id":"2dd5ab1c.02ad44","type":"comment","z":"8957e05a.44595","name":"MQTT Triggers light dimming after 2min","info":"","x":190,"y":520,"wires":[]},{"id":"1d8ded8b.cda832","type":"comment","z":"8957e05a.44595","name":"MQTT Triggers light off after 3min","info":"","x":170,"y":640,"wires":[]},{"id":"d27f57c4.7b9398","type":"comment","z":"8957e05a.44595","name":"MQTT Triggers light off after 20min (Manual turn on is overridden)","info":"","x":270,"y":760,"wires":[]},{"id":"2c2f05c8.8f817a","type":"comment","z":"8957e05a.44595","name":"Light off turns off automation variable","info":"","x":180,"y":860,"wires":[]},{"id":"b25a9498.6318d8","type":"server-state-changed","z":"8957e05a.44595","name":"Motion Küche","server":"2fba4297.e4145e","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.xiaomi_motion_kitchen_occupancy","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":110,"y":100,"wires":[["666c519d.5242a","6a6c0e1f.bcacf"]]},{"id":"7407d6c1.ab8c98","type":"api-current-state","z":"8957e05a.44595","name":"Light Off?","server":"2fba4297.e4145e","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"light.kuche","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":420,"y":340,"wires":[["8ba424ee.459cf8","5d466d1.23fbc94"],["3fcd1f49.8afc3"]]},{"id":"452d5c7.bb8cea4","type":"api-current-state","z":"8957e05a.44595","name":"luminance","server":"2fba4297.e4145e","version":1,"outputs":2,"halt_if":"250","halt_if_type":"num","halt_if_compare":"gt","override_topic":true,"entity_id":"sensor.avgluminance","state_type":"num","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":270,"y":340,"wires":[[],["7407d6c1.ab8c98"]]},{"id":"d1db8f43.4b8d4","type":"server-state-changed","z":"8957e05a.44595","name":"Motion Küche","server":"2fba4297.e4145e","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.xiaomi_motion_kitchen_occupancy","entityidfiltertype":"substring","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":110,"y":340,"wires":[["452d5c7.bb8cea4"],[]]},{"id":"74710206.3c721c","type":"api-call-service","z":"8957e05a.44595","name":"Tag","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.kitchen_left, light.kitchen_right, light.kitchen_top","data":"{\"kelvin\":\"2700\",\"brightness_pct\":\"100\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1170,"y":340,"wires":[[]]},{"id":"189d1c85.de9db3","type":"api-call-service","z":"8957e05a.44595","name":"Nacht","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.kitchen_top","data":"{\"color_name\":\"red\",\"brightness_pct\":\"25\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1170,"y":400,"wires":[[]]},{"id":"66a2971a.2ac038","type":"server-state-changed","z":"8957e05a.44595","name":"Light Off?","server":"2fba4297.e4145e","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.kuche","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"off","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":100,"y":900,"wires":[["100c12da.86f2dd"],[]]},{"id":"b08ef913.0576e8","type":"api-call-service","z":"8957e05a.44595","name":"Tag Dim","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.kitchen_left, light.kitchen_right, light.kitchen_top","data":"{\"kelvin\":\"2700\",\"brightness_pct\":\"50\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1180,"y":560,"wires":[[]]},{"id":"14ae7314.4279ad","type":"api-call-service","z":"8957e05a.44595","name":"Nacht Dim","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.kitchen_top","data":"{\"color_name\":\"red\",\"brightness_pct\":\"10\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1190,"y":620,"wires":[[]]},{"id":"78e3c5ba.0d578c","type":"api-call-service","z":"8957e05a.44595","name":"OFF","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.kitchen_left, light.kitchen_right, light.kitchen_top","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1170,"y":680,"wires":[[]]},{"id":"e58bd513.a71f28","type":"api-call-service","z":"8957e05a.44595","name":"OFF","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.kitchen_left, light.kitchen_right, light.kitchen_top","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1170,"y":800,"wires":[[]]},{"id":"8ba424ee.459cf8","type":"function","z":"8957e05a.44595","name":"Set Automation On","func":"flow.set(\"motion\", 1, \"storeInFile\");\n\nvar today = new Date();\nvar date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();\nvar time = today.getHours() + \":\" + today.getMinutes() + \":\" + today.getSeconds();\nvar dateTime = date+' '+time;\nnode.status({fill:\"green\",shape:\"dot\",text:dateTime});\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":650,"y":400,"wires":[[]]},{"id":"100c12da.86f2dd","type":"function","z":"8957e05a.44595","name":"Set Automation off","func":"flow.set(\"motion\", 0, \"storeInFile\");\n\nvar today = new Date();\nvar date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();\nvar time = today.getHours() + \":\" + today.getMinutes() + \":\" + today.getSeconds();\nvar dateTime = date+' '+time;\nnode.status({fill:\"green\",shape:\"dot\",text:dateTime});\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":650,"y":900,"wires":[[]]},{"id":"3fcd1f49.8afc3","type":"function","z":"8957e05a.44595","name":"Automation on?","func":"var newmsg = {}\nvar motion = flow.get('motion',\"storeInFile\");\nif (motion == 1)\n newmsg.payload = \"True\";\nelse (newmsg = null)\n\nvar today = new Date();\nvar date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();\nvar time = today.getHours() + \":\" + today.getMinutes() + \":\" + today.getSeconds();\nvar dateTime = date+' '+time;\nnode.status({fill:\"green\",shape:\"dot\",text:dateTime});\n\nreturn newmsg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":460,"wires":[["5d466d1.23fbc94"]]},{"id":"cbd97dc8.64f5e","type":"function","z":"8957e05a.44595","name":"Automation on?","func":"var newmsg = {}\nvar motion = flow.get('motion',\"storeInFile\");\nif (motion == 1)\n newmsg.payload = \"True\";\nelse (newmsg = null)\n\nvar today = new Date();\nvar date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();\nvar time = today.getHours() + \":\" + today.getMinutes() + \":\" + today.getSeconds();\nvar dateTime = date+' '+time;\nnode.status({fill:\"green\",shape:\"dot\",text:dateTime});\n\nreturn newmsg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":560,"wires":[["26879474.f8338c"]]},{"id":"f9c71a1b.ac3e28","type":"function","z":"8957e05a.44595","name":"Automation on?","func":"var newmsg = {}\nvar motion = flow.get('motion',\"storeInFile\");\nif (motion == 1)\n newmsg.payload = \"True\";\nelse (newmsg = null)\n\nvar today = new Date();\nvar date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();\nvar time = today.getHours() + \":\" + today.getMinutes() + \":\" + today.getSeconds();\nvar dateTime = date+' '+time;\nnode.status({fill:\"green\",shape:\"dot\",text:dateTime});\n\nreturn newmsg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":680,"wires":[["78e3c5ba.0d578c"]]},{"id":"fd242c4a.283ea","type":"inject","z":"8957e05a.44595","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":950,"y":860,"wires":[["e58bd513.a71f28"]]},{"id":"38f11bdf.cd0904","type":"mqtt-broker","name":"Mosquitto","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"2fba4297.e4145e","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]