Troubleshooting a specific flow to mimic philips hue, or make a better way to do it?

I have a flow that i want to mimic Philips hue

Motion detected - > Turn lights on full
wait 5 mins for no motion
Dim lights
wait 2 mins
turn off lights

if motion is detected again at any stage it start back at motion detected full brightness,

Now this works mostly, but its flakey and sometimes its doesn't go back to motion detected and full brightness, any ideas?

What type of pir sensor do you use, one that gives a pulse every time there is movement, or one that has a built-in time delay?

the philips hue indoor sensors that push as soon as motion is detected

[{"id":"260e8ac429d0545f","type":"inject","z":"38412910c32a18b1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":110,"y":480,"wires":[["9a277257f84c649d"]]},{"id":"9a277257f84c649d","type":"function","z":"38412910c32a18b1","name":"function 13","func":"let msg1 = {}\nlet msg2 = {}\nlet msg3 = {}\nmsg3.reset = ''\n\nlet lux = flow.get('lux')\n\nlet pulse = true // false if pir have timeout\n\nlet invar = msg.payload\nswitch (invar) {\n    case 'on':\n        msg1.payload = lux\n        node.send([msg1, msg3, msg3])\n        if(pulse) {\n        msg2.payload = 'on'\n        node.send([null, msg2, null])\n        }\n        break\n    case 'off':\n        if(!pulse) {\n        msg2.payload = 'on'\n        node.send([null, msg2, null])\n        }\n        break\n}\n\n","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":480,"wires":[["ba1fcebbcab40872","e16392bf3f5d81a7"],["278243f9175161ae"],["ac7a68b5e196c905"]]},{"id":"ba1fcebbcab40872","type":"zigbee2mqtt-out","z":"38412910c32a18b1","name":"","server":"389af9cbf5b0a79d","friendly_name":"Krone1 (4058075208421)","device_id":"0xf0d1b8000017513c","command":"brightness","commandType":"z2m_cmd","payload":"payload","payloadType":"msg","optionsValue":"","optionsType":"nothing","x":890,"y":460,"wires":[]},{"id":"278243f9175161ae","type":"delay","z":"38412910c32a18b1","name":"","pauseType":"delay","timeout":"6","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":460,"y":500,"wires":[["ac92ca3490c5d4a5"]]},{"id":"ac7a68b5e196c905","type":"delay","z":"38412910c32a18b1","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":460,"y":560,"wires":[["2130ac0e97cc13fd"]]},{"id":"ac92ca3490c5d4a5","type":"function","z":"38412910c32a18b1","name":"function 14","func":"let msg1 = {}\nlet activ = flow.get('activ')\nlet lux = flow.get('lux')\n\nif (activ) {\n    msg1.payload = lux / 4\n    node.warn(\"lux/4\");\n}\nif (!activ) {\n    msg1.payload = lux / 20\n    node.warn(\"lux/20\");\n}\nnode.send(msg1);","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":500,"wires":[["ba1fcebbcab40872","ac7a68b5e196c905","e16392bf3f5d81a7"]]},{"id":"2130ac0e97cc13fd","type":"function","z":"38412910c32a18b1","name":"function 15","func":"let msg1 = {}\n\nmsg1.payload = 0\nnode.send(msg1);","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":560,"wires":[["ba1fcebbcab40872","e16392bf3f5d81a7"]]},{"id":"4fe037caae8f2558","type":"inject","z":"38412910c32a18b1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":110,"y":520,"wires":[["9a277257f84c649d"]]},{"id":"bcf763031ab16414","type":"inject","z":"38412910c32a18b1","name":"true at 21:00","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 21 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":140,"y":340,"wires":[["5be6ae88718cddbb"]]},{"id":"e65801e6292657cc","type":"inject","z":"38412910c32a18b1","name":"false at 05:00","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 05 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":140,"y":380,"wires":[["5be6ae88718cddbb"]]},{"id":"5be6ae88718cddbb","type":"function","z":"38412910c32a18b1","name":"function 16","func":"let msg1 = {}\nlet activ = flow.get('activ') || false\nlet lux = flow.get('lux') || 50\n\nif (msg.payload === true) {\n    activ = true\n    node.status({ fill: \"green\", shape: \"ring\", text: \"on\" });\n    lux = 100\n}\n\nif (msg.payload === false) {\n    activ = false\n    node.status({ fill: \"red\", shape: \"ring\", text: \"off\" })\n    lux = 50\n}\n\nmsg1.payload = activ\nmsg1.topic = 'control'\nflow.set('activ', activ)\nflow.set('lux', lux)\n\nreturn msg1","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":360,"wires":[[]]},{"id":"7b1201567f0b7548","type":"comment","z":"38412910c32a18b1","name":"let pulse = true // false if pir have timeout","info":"","x":380,"y":440,"wires":[]},{"id":"e16392bf3f5d81a7","type":"debug","z":"38412910c32a18b1","name":"debug 8","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":860,"y":400,"wires":[]},{"id":"389af9cbf5b0a79d","type":"zigbee2mqtt-server","name":"","host":"localhost:1883","mqtt_port":"1883","mqtt_username":"","mqtt_password":"","mqtt_qos":"0","tls":"","usetls":false,"base_topic":"zigbee2mqtt"}]

In function 16, you set lux high and lux low.
In function 13, you select whether the pier has a pulse or timeout.
Right now it is set to true, ie. sends a pulse every time there is movement.
Insert your own node.

not sure thats what im looking for. but I really do appreciate you sending it over.