I am using the same basic logic for all my motion & lightlevel triggered lights.
Additional conditions are State of HomeMode (Night, Day) with different light settings
Maybe a bit overkill but works flawless for me for several months.
-
If Light already on (dont change)
You may also easily add the condition that it may not react to changes if it was triggered by you, and then you manually change the light
-
If Light is off, Turn On.
-
If no Motion detected (TimeOut then first dim, and then turn off unless new motion)
In order to achieve this, i basically save a value to the filesystem that tells node red if the light was turned on manually or via this automation.
Please find below my code for this:
> [{"id":"a058529a.2c996","type":"tab","label":"Light Dresser","disabled":false,"info":"Turn Light on / off based on Illuminance, Motion, HouseMode"},{"id":"f57f95fb.f45308","type":"switch","z":"a058529a.2c996","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":250,"y":120,"wires":[["ed17de52.d714f"],["464853cf.51897c"]]},{"id":"e4b9ac31.54e4f","type":"api-current-state","z":"a058529a.2c996","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.bedroom_dresser","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":540,"y":60,"wires":[["4337fa6f.ad86a4","5b10818a.9116f"],["440a1e8.c65a3e"]]},{"id":"461aa6b0.7314c8","type":"inject","z":"a058529a.2c996","name":"","topic":"","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":70,"y":60,"wires":[["ed17de52.d714f"]]},{"id":"865f204a.7298","type":"inject","z":"a058529a.2c996","name":"","topic":"","payload":"off","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":70,"y":180,"wires":[["464853cf.51897c"]]},{"id":"6e2d78e.477ff88","type":"api-call-service","z":"a058529a.2c996","name":"OFF","server":"2fba4297.e4145e","version":"1","service_domain":"light","service":"turn_off","entityId":"light.bedroom_dresser","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1890,"y":180,"wires":[["b4926d3d.d6"]]},{"id":"2ef1ba32.33fe26","type":"api-current-state","z":"a058529a.2c996","name":"Still Motion?","server":"2fba4297.e4145e","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"binary_sensor.xiaomi_dresser_motion","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1730,"y":180,"wires":[[],["6e2d78e.477ff88"]]},{"id":"66795ef0.ef5ba","type":"template","z":"a058529a.2c996","name":"cancel","field":"payload","fieldType":"msg","format":"handlebars","syntax":"plain","template":"cancel","output":"str","x":870,"y":120,"wires":[["c9549103.ef4ea","988662c5.040ac"]],"inputLabels":["on"],"outputLabels":["stop"]},{"id":"c9549103.ef4ea","type":"mytimeout","z":"a058529a.2c996","name":"30sec","outtopic":"","outsafe":" ","outwarning":"","outunsafe":"off","warning":"","timer":"30","debug":false,"ndebug":true,"ignoreCase":false,"repeat":false,"again":false,"x":1450,"y":180,"wires":[["f42ce4b6.0eccd8"],[]]},{"id":"f42ce4b6.0eccd8","type":"switch","z":"a058529a.2c996","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1590,"y":180,"wires":[["2ef1ba32.33fe26"]]},{"id":"c3c697fd.ba32f8","type":"api-current-state","z":"a058529a.2c996","name":"Already On?","server":"2fba4297.e4145e","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"light.bedroom_dresser","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":2190,"y":60,"wires":[[],["de0e5849.18e1b8"]]},{"id":"cc7b59f7.55e798","type":"delay","z":"a058529a.2c996","name":"0.5s","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2050,"y":60,"wires":[["c3c697fd.ba32f8"]]},{"id":"4f9a1681.0024a8","type":"template","z":"a058529a.2c996","name":"Off to On","field":"payload","fieldType":"msg","format":"handlebars","syntax":"plain","template":"On","output":"str","x":1300,"y":180,"wires":[["c9549103.ef4ea"]],"inputLabels":["on"],"outputLabels":["stop"]},{"id":"2f58c75a.443ac8","type":"api-current-state","z":"a058529a.2c996","name":"Already On?","server":"2fba4297.e4145e","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"light.bedroom_dresser","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":2190,"y":120,"wires":[[],["809c8499.b48d88"]]},{"id":"ea8ac314.a5ba8","type":"delay","z":"a058529a.2c996","name":"0.5s","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2050,"y":120,"wires":[["2f58c75a.443ac8"]]},{"id":"c0ddf20a.5e4b3","type":"api-current-state","z":"a058529a.2c996","name":"Still On?","server":"2fba4297.e4145e","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"light.bedroom_dresser","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":2180,"y":180,"wires":[["6e2d78e.477ff88"],["ab9471d6.89a18"]]},{"id":"b4926d3d.d6","type":"delay","z":"a058529a.2c996","name":"0.5s","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2050,"y":180,"wires":[["c0ddf20a.5e4b3"]]},{"id":"4337fa6f.ad86a4","type":"function","z":"a058529a.2c996","name":"Set Automation On","func":"flow.set(\"dresser\", 1, \"storeInFile\");\n\n","outputs":1,"noerr":0,"x":710,"y":60,"wires":[[]]},{"id":"440a1e8.c65a3e","type":"function","z":"a058529a.2c996","name":"Automation on?","func":"var newmsg = {}\nvar bathflow = flow.get('dresser',\"storeInFile\");\nif (bathflow == 1)\n newmsg.payload = \"True\";\nelse (newmsg = null)\n\nreturn newmsg;\n","outputs":1,"noerr":0,"x":700,"y":120,"wires":[["5b10818a.9116f","66795ef0.ef5ba"]]},{"id":"208e72d0.d1877e","type":"function","z":"a058529a.2c996","name":"Automation on?","func":"var newmsg = {}\nvar bathflow = flow.get('dresser',\"storeInFile\");\nif (bathflow == 1)\n newmsg.payload = \"True\";\nelse (newmsg = null)\n\nreturn newmsg;\n","outputs":1,"noerr":0,"x":700,"y":180,"wires":[["988662c5.040ac"]]},{"id":"464853cf.51897c","type":"api-current-state","z":"a058529a.2c996","name":"Already On?","server":"2fba4297.e4145e","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"light.bedroom_dresser","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":390,"y":180,"wires":[["208e72d0.d1877e"],["d1036a50.ff96b8"]]},{"id":"5b10818a.9116f","type":"api-current-state","z":"a058529a.2c996","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":60,"wires":[["5ed950f.a1ca4b"]]},{"id":"5ed950f.a1ca4b","type":"switch","z":"a058529a.2c996","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":1590,"y":60,"wires":[["de0e5849.18e1b8"],["809c8499.b48d88"]]},{"id":"ed17de52.d714f","type":"api-current-state","z":"a058529a.2c996","name":"Illuminance","server":"2fba4297.e4145e","version":1,"outputs":2,"halt_if":"1500","halt_if_type":"num","halt_if_compare":"gt","override_topic":true,"entity_id":"sensor.avgilluminance","state_type":"num","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":390,"y":60,"wires":[[],["e4b9ac31.54e4f"]]},{"id":"d69a59.b203f5a8","type":"api-current-state","z":"a058529a.2c996","name":"Still Motion?","server":"2fba4297.e4145e","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"binary_sensor.xiaomi_dresser_motion","state_type":"str","state_location":"","override_payload":"none","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1130,"y":180,"wires":[[],["4f9a1681.0024a8","f6772037.75ae9"]],"outputLabels":["","on"]},{"id":"f6772037.75ae9","type":"api-current-state","z":"a058529a.2c996","name":"still On?","server":"2fba4297.e4145e","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"light.bedroom_dresser","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1300,"y":240,"wires":[["e87d7297.44dfe"],[]]},{"id":"ea2a5a39.2d0c18","type":"api-current-state","z":"a058529a.2c996","name":"Already On?","server":"2fba4297.e4145e","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"light.bedroom_dresser","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":2190,"y":240,"wires":[[],["5aab6009.187c"]]},{"id":"899b02a.80eeb","type":"delay","z":"a058529a.2c996","name":"0.5s","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2050,"y":240,"wires":[["ea2a5a39.2d0c18"]]},{"id":"af04209b.c0738","type":"api-current-state","z":"a058529a.2c996","name":"Already On?","server":"2fba4297.e4145e","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"light.bedroom_dresser","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":2190,"y":300,"wires":[[],["cdee042f.e52d58"]]},{"id":"cefad57.d7ec528","type":"delay","z":"a058529a.2c996","name":"0.5s","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2050,"y":300,"wires":[["af04209b.c0738"]]},{"id":"e87d7297.44dfe","type":"api-current-state","z":"a058529a.2c996","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":1450,"y":240,"wires":[["12082fee.22fc8"]]},{"id":"12082fee.22fc8","type":"switch","z":"a058529a.2c996","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":1590,"y":240,"wires":[["5aab6009.187c"],["cdee042f.e52d58"]]},{"id":"de0e5849.18e1b8","type":"api-call-service","z":"a058529a.2c996","name":"Tag","server":"2fba4297.e4145e","version":"1","service_domain":"light","service":"turn_on","entityId":"light.bedroom_dresser","data":"{\"kelvin\":\"2700\",\"brightness_pct\":\"100\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1890,"y":60,"wires":[[]]},{"id":"809c8499.b48d88","type":"api-call-service","z":"a058529a.2c996","name":"Nacht","server":"2fba4297.e4145e","version":"1","service_domain":"light","service":"turn_on","entityId":"light.bedroom_dresser","data":"{\"color_name\":\"red\",\"brightness_pct\":\"20\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1890,"y":120,"wires":[[]]},{"id":"5aab6009.187c","type":"api-call-service","z":"a058529a.2c996","name":"Tag Dim","server":"2fba4297.e4145e","version":"1","service_domain":"light","service":"turn_on","entityId":"light.bedroom_dresser","data":"{\"kelvin\":\"2700\",\"brightness_pct\":\"35\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1900,"y":240,"wires":[["899b02a.80eeb"]]},{"id":"cdee042f.e52d58","type":"api-call-service","z":"a058529a.2c996","name":"Nacht Dim","server":"2fba4297.e4145e","version":"1","service_domain":"light","service":"turn_on","entityId":"light.bedroom_dresser","data":"{\"color_name\":\"red\",\"brightness_pct\":\"10\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1910,"y":300,"wires":[["cefad57.d7ec528"]]},{"id":"6ba39712.81bb38","type":"server-state-changed","z":"a058529a.2c996","name":"Ankleide Motion","server":"2fba4297.e4145e","version":1,"entityidfilter":"binary_sensor.xiaomi_dresser_motion","entityidfiltertype":"substring","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":120,"wires":[["f57f95fb.f45308"]]},{"id":"974ac7da.55e548","type":"server-state-changed","z":"a058529a.2c996","name":"Light Change?","server":"2fba4297.e4145e","version":1,"entityidfilter":"light.bedroom_dresser","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":400,"y":240,"wires":[["d1036a50.ff96b8"],[]]},{"id":"3691e4f3.b5a08c","type":"link in","z":"a058529a.2c996","name":"Set Automation Output","links":["94be304b.ddfcb","d1036a50.ff96b8","ab9471d6.89a18"],"x":715,"y":240,"wires":[["66d6558a.8cc9fc"]]},{"id":"66d6558a.8cc9fc","type":"function","z":"a058529a.2c996","name":"Set Automation off","func":"flow.set(\"dresser\", 0, \"storeInFile\");\n\n","outputs":1,"noerr":0,"x":850,"y":240,"wires":[[]]},{"id":"d1036a50.ff96b8","type":"link out","z":"a058529a.2c996","name":"Set Automation Input","links":["3691e4f3.b5a08c"],"x":635,"y":240,"wires":[]},{"id":"ab9471d6.89a18","type":"link out","z":"a058529a.2c996","name":"Set Automation Input","links":["3691e4f3.b5a08c"],"x":2295,"y":180,"wires":[]},{"id":"988662c5.040ac","type":"mytimeout","z":"a058529a.2c996","name":"60sec","outtopic":"","outsafe":" ","outwarning":"","outunsafe":"off","warning":"","timer":"60","debug":false,"ndebug":true,"ignoreCase":false,"repeat":false,"again":false,"x":870,"y":180,"wires":[["eb3bbd0e.bdd4c"],[]]},{"id":"eb3bbd0e.bdd4c","type":"switch","z":"a058529a.2c996","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":990,"y":180,"wires":[["d69a59.b203f5a8"]]},{"id":"2fba4297.e4145e","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]