Motion triggered light override if light already on

I have a smart blub in my foyer that does not operate off a switch. I’m trying to set up an automation that will trigger based on a motion sensor during a time period, but cancel if the light is alright on. I have this basic flow:

[{"id":"a672553e.2455c8","type":"server-state-changed","z":"2830869c.46727a","name":"Motion","server":"e8627faa.c1d02","version":1,"entityidfilter":"binary_sensor.great_room_motion","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"x":70,"y":2080,"wires":[["c1aefd5b.8573d"]]},{"id":"7a04ed8a.a8cd14","type":"api-call-service","z":"2830869c.46727a","name":"ON","server":"e8627faa.c1d02","version":1,"service_domain":"light","service":"turn_on","entityId":"light.foyer_light","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":770,"y":2060,"wires":[[]]},{"id":"32cf9924.7114ae","type":"switch","z":"2830869c.46727a","name":"on/off?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":360,"y":2080,"wires":[["1e66f7a3.7680a","80207e2a.1cc05"],["1e66f7a3.7680a"]]},{"id":"7cc06474.7570e4","type":"api-call-service","z":"2830869c.46727a","name":"OFF","server":"e8627faa.c1d02","version":1,"service_domain":"light","service":"turn_off","entityId":"light.foyer_light","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":770,"y":2120,"wires":[[]]},{"id":"1e66f7a3.7680a","type":"stoptimer","z":"2830869c.46727a","duration":"3","units":"Minute","payloadtype":"num","payloadval":"0","name":"","x":600,"y":2120,"wires":[["7cc06474.7570e4"],[]]},{"id":"80207e2a.1cc05","type":"api-current-state","z":"2830869c.46727a","name":"Already on?","server":"e8627faa.c1d02","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.back_patio_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":590,"y":2060,"wires":[["7a04ed8a.a8cd14"],[]]},{"id":"c1aefd5b.8573d","type":"time-range-switch","z":"2830869c.46727a","name":"Timer","lat":"41.51528","lon":"-83.68148","startTime":"21:00","endTime":"05:00","startOffset":0,"endOffset":0,"x":210,"y":2080,"wires":[["32cf9924.7114ae"],[]]},{"id":"e8627faa.c1d02","type":"server","z":"","name":"HA Fahrer","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

I’ve tired current state nodes just about everywhere and they seem to help out with part of my problem but they also create more. If I put a current state node for my light right after the event state for motion it will kill the flow if its on, but it also kills the off message that would be sent if the flow was actually running. I thought about trying it after the switch but same issue. Any suggestions?

I use a Current State node to check for the current status of an entity just before the command to turn said entity on. Something like this except in your case, the command would be light on.

I have that in my current flow for the on part and it works fine, but the problem is if I put it in the off part it’ll never turn the light off. Motion would turn the light on if it wasn’t already on but then when it goes to turn off it would check and see if it’s on and not turn it off.

What I did was create a new boolean sensor for Manually turned on.

Using the switch sets this to on or off. When there is motion it checks this boolean if it is already on then do nothing. If it is off, turn the light on (make sure the manual boolean is turned off) and set a timer to turn it off after 1 minute.

Yeah probably not the best explanation but if you look at the flow, it should make more sense.

[{"id":"3e989221.d89b1e","type":"tab","label":"Auto Laundry Room","disabled":false,"info":""},{"id":"43cebbae.b4e044","type":"switch","z":"3e989221.d89b1e","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":390,"y":310,"wires":[["d2b77475.d46708","c9dd4d00.6b694"],[]]},{"id":"d2b77475.d46708","type":"stoptimer","z":"3e989221.d89b1e","duration":"1","units":"Minute","payloadtype":"num","payloadval":"0","name":"","x":600,"y":330,"wires":[["3dcfe303.0b2f8c"],[]]},{"id":"1ad2e518.3bf19b","type":"api-call-service","z":"3e989221.d89b1e","name":"OFF","server":"d277d7e0.b4f288","version":1,"service_domain":"switch","service":"turn_off","entityId":"switch.laundry_room_light","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1250,"y":360,"wires":[[]]},{"id":"c07e9b2a.f4be88","type":"api-call-service","z":"3e989221.d89b1e","name":"ON","server":"d277d7e0.b4f288","version":1,"service_domain":"switch","service":"turn_on","entityId":"switch.laundry_room_light","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1150,"y":120,"wires":[[]]},{"id":"c9dd4d00.6b694","type":"api-current-state","z":"3e989221.d89b1e","name":"Already On?","server":"d277d7e0.b4f288","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"switch.laundry_room_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":750,"y":200,"wires":[["aca57997.7746f8"],["c07e9b2a.f4be88","5391568.52738a8","aca57997.7746f8"]]},{"id":"710ff4f1.e73c7c","type":"server-state-changed","z":"3e989221.d89b1e","name":"Motion","server":"d277d7e0.b4f288","version":1,"entityidfilter":"binary_sensor.laundry_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":190,"y":310,"wires":[["43cebbae.b4e044"]]},{"id":"b88a3d9c.0f8f","type":"server-state-changed","z":"3e989221.d89b1e","name":"Light Switch","server":"d277d7e0.b4f288","version":1,"entityidfilter":"switch.laundry_room_light","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"x":210,"y":440,"wires":[["a7a15e46.21e38","e97b48f4.4ce2b8"]]},{"id":"3dcfe303.0b2f8c","type":"api-current-state","z":"3e989221.d89b1e","name":"Still Motion?","server":"d277d7e0.b4f288","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"binary_sensor.laundry_motion","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":790,"y":330,"wires":[[],["95e5b01f.809ad"]]},{"id":"a7a15e46.21e38","type":"switch","z":"3e989221.d89b1e","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":390,"y":440,"wires":[["aa56d5ce.12fd98"],["50d49516.cfecac"]]},{"id":"95e5b01f.809ad","type":"api-current-state","z":"3e989221.d89b1e","name":"Light on from Switch?","server":"d277d7e0.b4f288","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"input_boolean.manual_launrdery","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1000,"y":320,"wires":[["8c9d7da7.43493"],["1ad2e518.3bf19b","71924db3.69e934","8c9d7da7.43493"]]},{"id":"71924db3.69e934","type":"api-call-service","z":"3e989221.d89b1e","name":"Boolean Motion Off","server":"d277d7e0.b4f288","version":1,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.manual_launrdery","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1230,"y":420,"wires":[[]]},{"id":"aa56d5ce.12fd98","type":"api-call-service","z":"3e989221.d89b1e","name":"Boolean Light On","server":"d277d7e0.b4f288","version":1,"service_domain":"input_boolean","service":"turn_on","entityId":"input_boolean.manual_launrdery","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":610,"y":420,"wires":[["ff90ce8c.8a081"]]},{"id":"50d49516.cfecac","type":"api-call-service","z":"3e989221.d89b1e","name":"Boolean Light Off","server":"d277d7e0.b4f288","version":1,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.manual_launrdery","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":610,"y":480,"wires":[["ff90ce8c.8a081"]]},{"id":"c027edfb.3003f","type":"api-call-service","z":"3e989221.d89b1e","name":"Manual Laundry Off","server":"d277d7e0.b4f288","version":1,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.manual_launrdery","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1170,"y":200,"wires":[[]]},{"id":"5391568.52738a8","type":"delay","z":"3e989221.d89b1e","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":960,"y":200,"wires":[["c027edfb.3003f"]]},{"id":"aca57997.7746f8","type":"debug","z":"3e989221.d89b1e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":930,"y":80,"wires":[]},{"id":"e97b48f4.4ce2b8","type":"debug","z":"3e989221.d89b1e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":460,"y":600,"wires":[]},{"id":"8c9d7da7.43493","type":"debug","z":"3e989221.d89b1e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1390,"y":300,"wires":[]},{"id":"ff90ce8c.8a081","type":"debug","z":"3e989221.d89b1e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":940,"y":500,"wires":[]},{"id":"d277d7e0.b4f288","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

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.

  1. 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

  2. If Light is off, Turn On.

  3. 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}]
1 Like

Hi,

i am new at node-red … i am testing your flow, but i am not getting pas the MODE ( input_select.mode) . how can i make the entity: input_select.mode?

cause i am not seeing this in my intergation? hope you can help me out

Well that is just an entity created by homeassistant.

Simply add it to your integration as you require it. Im using 3 Modes: Home, Away and Night.

Hi thanks for the tip…

i think i got … i can see the mode … going ON home… but the lights are not going on . … do i need to changes the function node?

Well, yes… my actual states are in German so you would need to adjust the node for your naming.

My actual states im using are: “Away” = “not_home”, “Tag” = “Day” & “Nacht” = “Night”
Simply open the switch node (that comes directly after the state node “mode”) and adjust to your naming of day and night or however you want to build your logic.

Hi,
i have changed it what you told me:
Screen Shot 2020-02-13 at 21.47.16

after motion my light doesnot start

this is what i have in the function node: flow.set(“dresser”, 1, “storeInFile”);
i was thinking that i should change dresser?!?

hope to hear from you

1 Like

You probably do not need to touch the function node. The references in the function nodes are relative and only need to be touched for cosmetic reasons.

However, from the Screenshot above I have 2 Questions:

  1. The “Day” flow doesnt lead anywhere. So if lights do not turn on, that could be the reason if the mode is day.
  2. Can you post a screenshot of the switch node config? You also need to change the switch node to adjust to your naming in the input_select.mode

I’ve been iterating over a subflow to manage this for the past year or so. I use it across 5 rooms, so it’s in active stable use for some time.

My design takes a Presence sensor input, and one or more Light sensor inputs. The behaviour is:

  • If the Presence sensor trips and the light is off, it turns it on and records state as being “on by motion”, and will turn off automatically after a delay since the last motion event (so repeat motion events will keep it on)
  • If anything else turns a light on, it will stay on indefinitely (“on by switch”) and motion events are ignored
  • If anything else turns all lights (from the inputs) are turned off, motion events are ignored for a “cool-down” period. (This is so you can turn a light off and have time to escape a room without triggering motion to turn it back on).

Additional condition filters (time of day, house state) I add as input filters between the service events and the sub-flow. Most of mine have a House State filter, so they only work at night.

For advanced settings, you can have several light inputs to manage triggering of manual/switch events, but specify a sub-list of lights to turn on automatically. For example, in our Living Room we have two ceiling lights, and a bench lamp. Motion-trigger only switches the bench lamp on/off, but if either of the main lights is on, the motion events (and hence the bench lamp) are ignored.

You can also change the default brightness on the fly. eg; sunset vs night vs “sleep mode”.

I’ve started to put some of my configs onto GitHub to share with a few friends, if you want to check it out:
https://github.com/dortamur/ha-node-red-flows
Top-level directory includes some example flows that use the sub-flows. There’s more specific docs on the sub-flows in the subflows folder.

I used to handle all Config by injecting a “config” topic with a JSON object of config params. Now, most of the config is handled through Sub-Flow config params, but a few of the advanced ones still rely on the “config” injection (which also allows dynamically changing the config on the fly).

At the time I originally started on this, I came across this Reddit post, which formed the initial basis of the sub-flow, but it’s evolved and been tweaked quite a bit since then:
https://www.reddit.com/r/homeassistant/comments/afk6cd/building_smarter_motion_lighting_nodered_finite/

I’d be keen to know if others find this useful (and if I’ve exported it to github in an understandable and reusable fashion). :grin:

2 Likes

Many of your ideas are helpful.
However, I am trying to, without static “sets” figure out how to apply node-red flows to sets of lights like all lights (and switches) within a room and to turn them on or off and even changing brightness or color based on some input trigger?

Seems most of the flows deal with each light independently which seems tedious and not flexible.

Currently, I setup static switches to represent “scenes” so there would be button in lovelace.
The idea would be to pass the scene-switch to to anode-red switch with parameters for on/off.

Any ideas would be welcome.
-jim

Hey Dortamur,
I really like your flow.
Did you happen to revise the flow in the last few months? I would like to take over the flow. However, I currently have the problem that I cannot stop the timers with events/buttons.
Perhaps an update can help me finding my problem. Thank you.

Hi!
I’ve just updated to the latest version on my github at https://github.com/dortamur/ha-node-red-flows .

I’m not sure what the problem you’re seeing is?
If a light is manually switched on or off, then it will cancel the “motion” triggered timers. So, if a light is turned off and then on by something other than the Lights Motion Control node, then it will be considered “manually” turned on, and stay on until something else turns it off.

The “manualCooldown” config item is the time in seconds after something else turns a light off, before a motion event is allowed to trigger turning them back on. This allows a user to turn a light off via a wall switch or phone app, and still walk through the room without the motion immediately turning everything back on.

The problem is that the manual switch-on mode can only be activated if no timer is currently running.
I have it quite often that I walk through a motion detector, the timer starts and only then do I get to the switch.
However, Home Assistant does not generate an event when I switch on a lamp if it is already on.

I would like to send a topic “stay_mode_on” to your subflow so that I can use it to stop all running timers.