Motion sensor and too high lux after light got on and off

I just migrated to ZHA but I still have the same issue as in deCONZ :S
Is my flow correct:

[{"id":"9c11fd6a.02086","type":"server-state-changed","z":"873ca454.d66268","name":"Motion hall upper","server":"8cff5162.96e2d","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.hall_upper","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,"x":120,"y":1627,"wires":[["3c5006ca.6c562a"],[]]},{"id":"87906dfc.ed704","type":"api-call-service","z":"873ca454.d66268","name":"Hall light on","server":"8cff5162.96e2d","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.hall_upper","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":570,"y":1614,"wires":[[]]},{"id":"b822c830.a39638","type":"api-call-service","z":"873ca454.d66268","name":"Hall light off","server":"8cff5162.96e2d","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.hall_upper","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":810,"y":1720,"wires":[[]]},{"id":"f5dbbad2.f24028","type":"delay","z":"873ca454.d66268","name":"","pauseType":"delay","timeout":"6","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":560,"y":1720,"wires":[["b822c830.a39638"]]},{"id":"3c5006ca.6c562a","type":"api-current-state","z":"873ca454.d66268","name":"lux <= 5","server":"8cff5162.96e2d","version":1,"outputs":2,"halt_if":"5","halt_if_type":"num","halt_if_compare":"lte","override_topic":false,"entity_id":"sensor.hall_upper_illuminance","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":320,"y":1620,"wires":[["87906dfc.ed704","f5dbbad2.f24028"],[]]},{"id":"8cff5162.96e2d","type":"server","z":"","name":"Home Assistant","addon":true}]

It’s just that the lux value is updated to late making my flow think the light is still on.

@Jdruwe those example lights on/off flows never work. Lux changes up and when the continued motion on the sensor occurs, beucase you dont have any condition checks in place, the flow says lux is to high therefore turn off. Have a look at my flow below - it seems complex but isnt when you work through it (as you can see, I also have my own mode manager built to handle whichever mode the house is in as to what level to set the bulbs to). I also have an automation override being as theres times when you want the light to come on and stay on, or stay off even with motion; I can control these from Apple Homekit as switches.

My updated ZHA flow, with average lux settings (becuase I have 2 Hue motion sensors, using just one of the motion sensors lux value causes a race condition (the first sensor updates lux on motion however being as the light is then on by the time sensor 2 pickups up movement and updates lux it creates a conflict of lux values and the flow then thinks lux is higher than the trigger - hope that makes sense?):

[{"id":"f6de963f.d74b78","type":"tab","label":"Downstairs Hall","disabled":false,"info":""},{"id":"48dfdbf9.6418a4","type":"comment","z":"f6de963f.d74b78","name":"Turn off automation","info":"","x":130,"y":360,"wires":[]},{"id":"9234f274.ab90a","type":"server-state-changed","z":"f6de963f.d74b78","name":"Downstairs hall automation off","server":"9f14629c.16082","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.downstairs_hall_automation","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":160,"y":400,"wires":[["6348f373.e4265c"],[]]},{"id":"6348f373.e4265c","type":"change","z":"f6de963f.d74b78","name":"Stop timer","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload","pt":"msg","to":"STOP","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":400,"wires":[["954ddac8.cdf8f8"]]},{"id":"954ddac8.cdf8f8","type":"link out","z":"f6de963f.d74b78","name":"Cancel Timer","links":["ffb00bfc.08da28","b2741f1c.d6407","e6986fc6.5f82c","4a035e26.5930c","7ae27367.1e67fc"],"x":595,"y":400,"wires":[]},{"id":"eca95a35.6b67f8","type":"comment","z":"f6de963f.d74b78","name":"Turn on automation","info":"","x":130,"y":480,"wires":[]},{"id":"a94d71a1.aa4db","type":"server-state-changed","z":"f6de963f.d74b78","name":"Downstairs hall automation on","server":"9f14629c.16082","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.downstairs_hall_automation","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,"x":160,"y":520,"wires":[["ec1c7154.04d3c"],[]]},{"id":"ec1c7154.04d3c","type":"link out","z":"f6de963f.d74b78","name":"Resume timer","links":["ffb00bfc.08da28","b2741f1c.d6407","d48a7fa7.972","e6986fc6.5f82c","4a035e26.5930c","7ae27367.1e67fc"],"x":375,"y":520,"wires":[]},{"id":"8304418d.27c39","type":"api-current-state","z":"f6de963f.d74b78","name":"light on?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.downstairs_hall_lights","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":840,"y":180,"wires":[[],["e19afa60.131d28"]]},{"id":"3bb1279.b24a5d8","type":"api-current-state","z":"f6de963f.d74b78","name":"Automation enabled?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.downstairs_hall_automation","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1320,"y":440,"wires":[["c9b6eac3.553eb8"],[]]},{"id":"15459e08.c79f22","type":"api-call-service","z":"f6de963f.d74b78","name":"ON","server":"9f14629c.16082","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.downstairs_hall_lights","data":"{\"brightness_pct\":60,\"kelvin\":3200,\"transition\":0.1}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1530,"y":40,"wires":[[]]},{"id":"cc27df77.fea99","type":"api-current-state","z":"f6de963f.d74b78","name":"Evening?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.ha_mode_evening","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1282,"y":40,"wires":[["15459e08.c79f22"],[]]},{"id":"f9cff041.61eb7","type":"api-current-state","z":"f6de963f.d74b78","name":"Midweek?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.ha_mode_midweek","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1283,"y":100,"wires":[["ee280123.73bea"],[]]},{"id":"d450c1c3.9d97b","type":"api-current-state","z":"f6de963f.d74b78","name":"Midweek morning?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.ha_mode_midweek_morning","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1310,"y":280,"wires":[["f586b1bd.6bcae"],[]]},{"id":"5e2888e8.1136a8","type":"api-current-state","z":"f6de963f.d74b78","name":"Weekend?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.ha_mode_weekend","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1290,"y":160,"wires":[["ee280123.73bea"],[]]},{"id":"21b04558.6ee26a","type":"api-current-state","z":"f6de963f.d74b78","name":"Weekend morning?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.ha_mode_weekend_morning","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1310,"y":220,"wires":[["76897972.ac2c68"],[]]},{"id":"ee280123.73bea","type":"api-call-service","z":"f6de963f.d74b78","name":"ON","server":"9f14629c.16082","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.downstairs_hall_lights","data":"{\"brightness_pct\":100,\"kelvin\":3200,\"transition\":0.1}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1530,"y":120,"wires":[[]]},{"id":"76897972.ac2c68","type":"api-call-service","z":"f6de963f.d74b78","name":"ON","server":"9f14629c.16082","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.downstairs_hall_lights","data":"{\"brightness_pct\":50,\"kelvin\":3200,\"transition\":0.1}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1530,"y":220,"wires":[[]]},{"id":"5dbf758a.dec3ac","type":"api-current-state","z":"f6de963f.d74b78","name":"Night??","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.ha_mode_night","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1280,"y":340,"wires":[["f586b1bd.6bcae"],[]]},{"id":"f586b1bd.6bcae","type":"api-call-service","z":"f6de963f.d74b78","name":"ON","server":"9f14629c.16082","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.downstairs_hall_lights","data":"{\"brightness_pct\":10,\"kelvin\":3200,\"transition\":0.1}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1530,"y":300,"wires":[[]]},{"id":"c9b6eac3.553eb8","type":"api-call-service","z":"f6de963f.d74b78","name":"OFF","server":"9f14629c.16082","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.downstairs_hall_lights","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1550,"y":440,"wires":[[]]},{"id":"b2741f1c.d6407","type":"link in","z":"f6de963f.d74b78","name":"Downstairs hall continued","links":["ec1c7154.04d3c","954ddac8.cdf8f8"],"x":695,"y":340,"wires":[["317b2829.d36ff8"]]},{"id":"36a3e84d.be8ea8","type":"switch","z":"f6de963f.d74b78","name":"Lux threshold 30 or less","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"30","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1130,"y":720,"wires":[["3cddc65.727213a"],["a10347f.f2258b8"]]},{"id":"3cddc65.727213a","type":"change","z":"f6de963f.d74b78","name":"Enable light","rules":[{"t":"set","p":"light_enabled","pt":"flow","to":"yes","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1350,"y":700,"wires":[["650d52da.3c6efc"]]},{"id":"a10347f.f2258b8","type":"change","z":"f6de963f.d74b78","name":"Disable light","rules":[{"t":"set","p":"light_enabled","pt":"flow","to":"no","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1350,"y":740,"wires":[["ab6d9389.3617e"]]},{"id":"650d52da.3c6efc","type":"debug","z":"f6de963f.d74b78","name":"Enable Light","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1540,"y":700,"wires":[]},{"id":"ab6d9389.3617e","type":"debug","z":"f6de963f.d74b78","name":"Disable light","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1542,"y":740,"wires":[]},{"id":"e19afa60.131d28","type":"switch","z":"f6de963f.d74b78","name":"Light enabled?","property":"light_enabled","propertyType":"flow","rules":[{"t":"eq","v":"yes","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1020,"y":180,"wires":[["cc27df77.fea99","f9cff041.61eb7","5e2888e8.1136a8","21b04558.6ee26a","d450c1c3.9d97b","5dbf758a.dec3ac"]]},{"id":"69ee6846.c85b38","type":"comment","z":"f6de963f.d74b78","name":"Downstairs Hall Lux","info":"Store lux values when hall light is off","x":130,"y":600,"wires":[]},{"id":"59775a8b.5483f4","type":"api-current-state","z":"f6de963f.d74b78","name":"All Away?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.ha_mode_away","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":380,"y":260,"wires":[[],["defbfab8.cfd2c8"]]},{"id":"defbfab8.cfd2c8","type":"api-current-state","z":"f6de963f.d74b78","name":"Automation enabled?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.downstairs_hall_automation","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":580,"y":260,"wires":[["317b2829.d36ff8","8304418d.27c39"],[]]},{"id":"a25b01ce.9793b","type":"server-state-changed","z":"f6de963f.d74b78","name":"Front Door Motion","server":"9f14629c.16082","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.front_door_sensor_motion","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":130,"y":220,"wires":[["59775a8b.5483f4"]]},{"id":"f8b47115.05be1","type":"server-state-changed","z":"f6de963f.d74b78","name":"Downstairs Hall Motion","server":"9f14629c.16082","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.downstairs_hall_sensor_motion","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":140,"y":280,"wires":[["59775a8b.5483f4"]]},{"id":"317b2829.d36ff8","type":"stoptimer","z":"f6de963f.d74b78","duration":"30","units":"Second","payloadtype":"num","payloadval":"2","name":"","x":870,"y":320,"wires":[["94e90718.d86368"],[]]},{"id":"94e90718.d86368","type":"api-current-state","z":"f6de963f.d74b78","name":"DH Hall motion on?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"binary_sensor.downstairs_hall_sensor_motion","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1090,"y":400,"wires":[[],["3bb1279.b24a5d8"]]},{"id":"ff7e3e19.3c892","type":"link in","z":"f6de963f.d74b78","name":"Reset Light Timer In","links":["1c0d8020.638e9"],"x":755,"y":400,"wires":[["317b2829.d36ff8"]]},{"id":"ec30229.c3a0e6","type":"server-state-changed","z":"f6de963f.d74b78","name":"Front door","server":"9f14629c.16082","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.front_door_sensor_light","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":120,"y":660,"wires":[["d28eb865.5afea"]]},{"id":"96af0824.8851e8","type":"server-state-changed","z":"f6de963f.d74b78","name":"Downstairs Hall","server":"9f14629c.16082","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.downstairs_hall_sensor_light","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":100,"y":760,"wires":[["1d4fe5be.f34942"]]},{"id":"1d4fe5be.f34942","type":"change","z":"f6de963f.d74b78","name":"","rules":[{"t":"set","p":"DHLux","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":760,"wires":[["c6d7af90.df2538"]]},{"id":"d28eb865.5afea","type":"change","z":"f6de963f.d74b78","name":"","rules":[{"t":"set","p":"FDLux","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":660,"wires":[["c6d7af90.df2538"]]},{"id":"c6d7af90.df2538","type":"api-current-state","z":"f6de963f.d74b78","name":"DH Light Off","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.downstairs_hall_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":490,"y":720,"wires":[["ba4d3ef1.cb59","decb38f6.0c29c"],[]]},{"id":"decb38f6.0c29c","type":"change","z":"f6de963f.d74b78","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"FDLux","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":660,"wires":[["db5cfdfd.b6f218"]]},{"id":"ba4d3ef1.cb59","type":"change","z":"f6de963f.d74b78","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"DHLux","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":780,"wires":[["db5cfdfd.b6f218"]]},{"id":"db5cfdfd.b6f218","type":"average","z":"f6de963f.d74b78","name":"","topic":"","x":920,"y":720,"wires":[["36a3e84d.be8ea8"]]},{"id":"9f14629c.16082","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

The average node is using node-red-contrib-average

I will try to implement something similar. Just ordered a second motion sensor for my upper hall to calculate the average :wink: Will update later, thank you so much for your effort!

@Jdruwe this might be a bit of a simpler flow to follow:

[{"id":"95a3c81.37a8c38","type":"tab","label":"Garden","disabled":false,"info":""},{"id":"487e61fa.9a115","type":"comment","z":"95a3c81.37a8c38","name":"Garden Lux","info":"Store lux values when hall light is off","x":110,"y":420,"wires":[]},{"id":"a05b8197.99564","type":"comment","z":"95a3c81.37a8c38","name":"Turn off automation","info":"","x":130,"y":160,"wires":[]},{"id":"c6f1ae5d.e1826","type":"server-state-changed","z":"95a3c81.37a8c38","name":"Garden automation off","server":"9f14629c.16082","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.garden_automation","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":140,"y":200,"wires":[["66a8a000.9ed0b"],[]]},{"id":"66a8a000.9ed0b","type":"change","z":"95a3c81.37a8c38","name":"Stop timer","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload","pt":"msg","to":"STOP","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":200,"wires":[["d2cb96e5.52c578"]]},{"id":"d2cb96e5.52c578","type":"link out","z":"95a3c81.37a8c38","name":"Cancel Timer","links":["ffb00bfc.08da28","c4bf72df.0268d"],"x":515,"y":200,"wires":[]},{"id":"bf791216.7584d","type":"comment","z":"95a3c81.37a8c38","name":"Turn on automation","info":"","x":130,"y":260,"wires":[]},{"id":"c1112759.861988","type":"server-state-changed","z":"95a3c81.37a8c38","name":"Garden automation on","server":"9f14629c.16082","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.garden_automation","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,"x":140,"y":300,"wires":[["9d5297c6.d7e878"],[]]},{"id":"9d5297c6.d7e878","type":"link out","z":"95a3c81.37a8c38","name":"Resume timer","links":["ffb00bfc.08da28","c4bf72df.0268d"],"x":315,"y":320,"wires":[]},{"id":"9092da49.4e03e8","type":"api-current-state","z":"95a3c81.37a8c38","name":"Automation enabled?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.garden_automation","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1240,"y":140,"wires":[["c1142526.3ee918"],[]]},{"id":"c1142526.3ee918","type":"api-call-service","z":"95a3c81.37a8c38","name":"OFF","server":"9f14629c.16082","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.garden_light","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1430,"y":140,"wires":[[]]},{"id":"c4bf72df.0268d","type":"link in","z":"95a3c81.37a8c38","name":"Garden continued","links":["d2cb96e5.52c578","9d5297c6.d7e878"],"x":615,"y":180,"wires":[["659e66d7.b66718"]]},{"id":"1020962c.0765ca","type":"change","z":"95a3c81.37a8c38","name":"Set lux variable","rules":[{"t":"set","p":"GardenLuxData","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":460,"wires":[["2a21f65.e7c9a0a"]]},{"id":"2a21f65.e7c9a0a","type":"api-current-state","z":"95a3c81.37a8c38","name":"Garden lights on?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.garden_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":550,"y":460,"wires":[[],["cc8811b1.e8af1"]]},{"id":"cc8811b1.e8af1","type":"switch","z":"95a3c81.37a8c38","name":"Lux threshold 50 or less","property":"GardenLuxData","propertyType":"flow","rules":[{"t":"lte","v":"50","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":850,"y":460,"wires":[["f0d33a55.66dfc8"],["c335adff.19fa6"]]},{"id":"f0d33a55.66dfc8","type":"change","z":"95a3c81.37a8c38","name":"Enable light","rules":[{"t":"set","p":"light_enabled","pt":"flow","to":"yes","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1090,"y":440,"wires":[[]]},{"id":"c335adff.19fa6","type":"change","z":"95a3c81.37a8c38","name":"Disable light","rules":[{"t":"set","p":"light_enabled","pt":"flow","to":"no","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1090,"y":480,"wires":[[]]},{"id":"aaccf5be.d04028","type":"api-current-state","z":"95a3c81.37a8c38","name":"light on?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.garden_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":740,"y":60,"wires":[[],["a8e8dca7.819b1"]]},{"id":"a8e8dca7.819b1","type":"switch","z":"95a3c81.37a8c38","name":"Light enabled?","property":"light_enabled","propertyType":"flow","rules":[{"t":"eq","v":"yes","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":920,"y":60,"wires":[["52f70aef.441d94"]]},{"id":"8b591c64.0d188","type":"api-current-state","z":"95a3c81.37a8c38","name":"All Away?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.ha_mode_away","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":300,"y":60,"wires":[[],["3de0c43d.64493c"]]},{"id":"3de0c43d.64493c","type":"api-current-state","z":"95a3c81.37a8c38","name":"Automation enabled?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.garden_automation","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":500,"y":60,"wires":[["659e66d7.b66718","aaccf5be.d04028"],[]]},{"id":"72d7188c.5b0d88","type":"server-state-changed","z":"95a3c81.37a8c38","name":"Garden Motion","server":"9f14629c.16082","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.garden_sensor_motion","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":120,"y":60,"wires":[["8b591c64.0d188"]]},{"id":"52f70aef.441d94","type":"api-call-service","z":"95a3c81.37a8c38","name":"Garden Light Turn on","server":"9f14629c.16082","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.garden_light","data":"{\"brightness_pct\":100,\"rgb_color\":[255,232,179],\"transition\":0.5}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1140,"y":60,"wires":[[]]},{"id":"fde7b03b.9c5f1","type":"server-state-changed","z":"95a3c81.37a8c38","name":"Garden Light Sensor","server":"9f14629c.16082","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.garden_sensor_light","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":130,"y":460,"wires":[["1020962c.0765ca"]]},{"id":"659e66d7.b66718","type":"stoptimer","z":"95a3c81.37a8c38","duration":"5","units":"Minute","payloadtype":"num","payloadval":"2","name":"","x":760,"y":140,"wires":[["552c4430.e36bcc"],[]]},{"id":"552c4430.e36bcc","type":"api-current-state","z":"95a3c81.37a8c38","name":"Garden motion on?","server":"9f14629c.16082","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"binary_sensor.garden_sensor_motion","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":990,"y":160,"wires":[[],["9092da49.4e03e8"]]},{"id":"8a4dcf13.cfb7d","type":"link in","z":"95a3c81.37a8c38","name":"Reset Light Timer In","links":["1c0d8020.638e9"],"x":655,"y":220,"wires":[["659e66d7.b66718"]]},{"id":"9f14629c.16082","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
1 Like

Thx, will look into both once my sensor has arrived :wink:

What kind of average node are you using? I finally have some spare time fix this :smiley:

@Jdruwe Im using node-red-contrib-average. Good luck

Why are you setting flow variables before ‘DH light off’ and using those values to set the message payload after? Is it purely because that’s what the average node will expect?

@Jdruwe its being set before the light comes on into a variable as when the light is actually on, a new lux reading is triggered (because the motion sensors are a distance apart) which is then higher than the lux value to keep lights on.
Without this in place, when the timer in the flow above to control lights reaches its time out, the new lux value is to high and therefore the light is switched off.
Average node will expect multiple values to be handed to it which is why the variables are split down per sensor
If you are using Hue native integration, you might not see the issues occur being as Hue native polls every 3 to 5 seconds (enough time to reach the 2nd sensor while lights are still off so both sensor readings for lux are in the threshold to trigger light on). However as soon as you use push instead of polling (ie via ZHA or deconz) there’s no grace period.

eg (using 25 lux as the threshold to not switch on lights)
Behaviour with this code in place:

  • Lights are off.
  • Sensor 1 lux = 2. Stored in variable (lets call this var 1).
  • Sensor 2 lux = 2. Stored in variable (lets call this var 2).
  • Motion occurs.
  • Lux reading on sensor 1 = 2.
  • Flow evaluation occurs. var 1 and var 2 average = 2 lux.
  • Light comes on and 5 minute timer starts.
  • Motion goes past sensor 2. Var 2 value used to evaluate lux. Reset 5 minute timer.
  • Lux reading on sensor 2 = 50 however var 2 value = 2.
  • Light stays on after 5 minute timeout as evaluation using lux from before light on.
  • Continued motion, timer reset each time there is motion due to using var 1 and var 2 values (2).

Behaviour without the code in place:

  • Lights are off
  • Sensor 1 lux = 2.
  • Sensor 2 lux = 2.
  • Motion occurs.
  • Lux reading on sensor 1 = 2
  • Flow evaluation occurs. sensor 1 and 2 average = 2
  • Light comes on and 5 minute timer starts
  • Motion at sensor 2
  • Lux reading on sensor 2 = 50
  • Light on timer continues counting down as average = 26
  • Continued motion, timer continues counting down from original on time

Ok that makes more sense, guess i’ll have to work my way through the flow :wink:

The average node confuses me sometimes because it just seems to pick the lux value of my stair motion sensor as the average value, even though a value of 80.2 was provided for my office lux. Do you have an idea what is causing this or is this expected behaviour?

I was also wondering what happens here:

So in the case motion was detected after 30 seconds it will again check for motion, if no motion was detected your flow would turn the light off. But what happens if there was motion detected, the light would just be kept on and never turned off, am I right? I am thinking about adding some kind of node in there that would loop until the motion state is clear if that makes sense and then continue with the light off flow.

Update: I have added a wait until node which seems to works quite nice:

@jdruwe it looks like the problem here is sensor.hall_upper_office_illuminance is outputting as string instead of as number its effectively average node is comparing apples and oranges instead of 2 apples.
What does the change node config look like?
Also, what is the debug output for the motion sensor?

@Jdruwe I did try the ‘Wait Until’ node but I couldn’t ever get it working as needed for this exact reason (I didn’t want to evaluate motion at the point timer expired, but wanted the timer to reset each time there’s motion).
By using ‘Stop Timer’, every time there is new motion the timer is reset for a new 30 seconds period, when the stop timer expires because there’s been no motion, the lights off flow kicks in and to double check there’s no motion, the motion check node after the stop timer stops any errors occurring. Using this method, there’s no need to loop as its already looping by itself triggered by motion. Kind of the opposite to your explanation but you get the same outcome

1 Like

If I add a debug node after the illuminance event node I see the value indeed as a string but I am not sure if that is an issue

Flow:

[{"id":"4ff6cf8e.e665b","type":"comment","z":"e6af1ba5.bce1d8","name":"Hall upper lux","info":"Store lux values when hall light is off","x":110,"y":660,"wires":[]},{"id":"c7a875f8.9d95c8","type":"server-state-changed","z":"e6af1ba5.bce1d8","name":"Hall upper stairs illuminance","server":"8cff5162.96e2d","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.hall_upper_stairs_illuminance","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":160,"y":700,"wires":[["249cebe2.919974","f9f358d2.d6e4d8"]]},{"id":"e0a9f14f.13ebf","type":"server-state-changed","z":"e6af1ba5.bce1d8","name":"Hall upper office illuminance","server":"8cff5162.96e2d","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.hall_upper_office_illuminance","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":160,"y":820,"wires":[["2dbf8585.cfc6ba","8e337bb9.fbe978"]]},{"id":"249cebe2.919974","type":"change","z":"e6af1ba5.bce1d8","name":"Set flow var","rules":[{"t":"set","p":"hall_upper_stairs_lux","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":700,"wires":[["4b9c7c73.ed97f4"]]},{"id":"2dbf8585.cfc6ba","type":"change","z":"e6af1ba5.bce1d8","name":"Set flow var","rules":[{"t":"set","p":"hall_upper_office_lux","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":820,"wires":[["4b9c7c73.ed97f4"]]},{"id":"4b9c7c73.ed97f4","type":"api-current-state","z":"e6af1ba5.bce1d8","name":"Hall upper light off?","server":"bec03a27.c60438","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.hall_upper","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":610,"y":760,"wires":[["c1ff8f9c.30b3e","ef0d1952.f2dea8"],[]]},{"id":"c1ff8f9c.30b3e","type":"change","z":"e6af1ba5.bce1d8","name":"Stairs lux","rules":[{"t":"set","p":"payload","pt":"msg","to":"hall_upper_stairs_lux","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":727,"wires":[["58506d9e.ee76a4"]]},{"id":"ef0d1952.f2dea8","type":"change","z":"e6af1ba5.bce1d8","name":"Office lux","rules":[{"t":"set","p":"payload","pt":"msg","to":"hall_upper_office_lux","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":784,"wires":[["58506d9e.ee76a4"]]},{"id":"58506d9e.ee76a4","type":"average","z":"e6af1ba5.bce1d8","name":"","topic":"","x":1000,"y":760,"wires":[["1c41751c.7e4bdb","89477cde.d1c9f"]]},{"id":"8e337bb9.fbe978","type":"debug","z":"e6af1ba5.bce1d8","name":"office lux","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":400,"y":900,"wires":[]},{"id":"f9f358d2.d6e4d8","type":"debug","z":"e6af1ba5.bce1d8","name":"stairs lux","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":420,"y":620,"wires":[]},{"id":"1c41751c.7e4bdb","type":"switch","z":"e6af1ba5.bce1d8","name":"Lux <= 20","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"20","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1161,"y":760,"wires":[["7b652743.c98ba8"],["7a62823f.ee133c"]]},{"id":"89477cde.d1c9f","type":"debug","z":"e6af1ba5.bce1d8","name":"Average hall upper lux","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1232,"y":680,"wires":[]},{"id":"7b652743.c98ba8","type":"change","z":"e6af1ba5.bce1d8","name":"Enable light","rules":[{"t":"set","p":"light_enabled","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1335,"y":738,"wires":[["bb758f75.28df9"]]},{"id":"7a62823f.ee133c","type":"change","z":"e6af1ba5.bce1d8","name":"Disable light","rules":[{"t":"set","p":"light_enabled","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1335,"y":778,"wires":[["2745f7cd.7dee48"]]},{"id":"2745f7cd.7dee48","type":"debug","z":"e6af1ba5.bce1d8","name":"Disable light","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1510,"y":778,"wires":[]},{"id":"bb758f75.28df9","type":"debug","z":"e6af1ba5.bce1d8","name":"Enable Light","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1510,"y":738,"wires":[]},{"id":"8cff5162.96e2d","type":"server","z":"","name":"Home Assistant","addon":true},{"id":"bec03a27.c60438","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Ah ok I see, I am filtering at ‘on’ motion events atm, your strategy is worth checking out.

One issue is your hall upper office lux is not setting the variable correctly - change the change node from msg to flow on the top line when setting variable.
I can’t see why one of the devices is outputting string and the other outputting number.
Are both your motion sensors the same device type? Ie all my motion sensors are Hue motion sensors.
Are they both connected to the same coordinator? Ie not one to a hue bridge and one to a conbee stick?
Also, to help debugging, change the output from msg.payload to “complete msg object” this will then give you the full output instead of just part of the output

1 Like

Thanks for all the help. I am using hue motion sensors. The setup was giving me a bit too much of a headache so I decided to use the schedex node to enable or disable light based on suncalculation events instead of actual lux values. Also because some of my motion sensors are just by themselves and I don’t want to buy 2 sensors everywhere to calculate an average :stuck_out_tongue:

Your welcome.
Post 24 is the simpler flow without average lux values in - that works off just 1 motion sensor in the garden to turn on the garden light, regardless what time of day it is

1 Like

I’ve experienced some cases where the lux was updated to the higher value (as if the light was picked up) when going from motion detected to no motion

@Jdruwe export the entire sheet and pop the code on here and I will take a look for you. It’s frustrating sometimes how it goes.
Also remember Hue devices report back every 4 minutes unless they detect motion at which point the report back lux first then motion