Hereâs a slimmed down example. Here Iâm just controlling a simple on/off light switch:
[{"id":"edbb6ac7.d702b8","type":"change","z":"8c99a79b.f26338","name":"Stop","rules":[{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":764,"y":425,"wires":[["ee0addbb.644e"]]},{"id":"8abf5432.db6398","type":"api-call-service","z":"8c99a79b.f26338","name":"Set the lighting mode to motion","server":"d0cb210d.98634","version":1,"debugenabled":false,"service_domain":"input_select","service":"select_option","entityId":"input_select.master_bathroom_shower_lighting_mode","data":"{\"option\":\"Motion\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1405,"y":289,"wires":[[]]},{"id":"1aeabcfc.c27893","type":"api-call-service","z":"8c99a79b.f26338","name":"Set the lighting mode to idle","server":"d0cb210d.98634","version":1,"debugenabled":false,"service_domain":"input_select","service":"select_option","entityId":"input_select.master_bathroom_lighting_mode","data":"{\"option\":\"Idle\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1167,"y":493,"wires":[[]]},{"id":"fce2eed9.71b98","type":"api-current-state","z":"8c99a79b.f26338","name":"What is the lighting mode?","server":"d0cb210d.98634","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_select.master_bathroom_shower_lighting_mode","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":517,"y":316,"wires":[["9a2eda38.0b3398"]]},{"id":"7d1ec536.b0a98c","type":"server-state-changed","z":"8c99a79b.f26338","name":"Lighting mode changes","server":"d0cb210d.98634","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_select.master_bathroom_shower_lighting_mode","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":334,"y":465,"wires":[["ed484dcb.ba9f2"]]},{"id":"9a2eda38.0b3398","type":"switch","z":"8c99a79b.f26338","name":"Idle or Motion?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Idle","vt":"str"},{"t":"eq","v":"Motion","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":757,"y":316,"wires":[["7669cd23.3cd964"],["edbb6ac7.d702b8"]]},{"id":"7669cd23.3cd964","type":"api-current-state","z":"8c99a79b.f26338","name":"What's the current location mode?","server":"d0cb210d.98634","version":1,"outputs":2,"halt_if":"Away","halt_if_type":"str","halt_if_compare":"is_not","override_topic":false,"entity_id":"input_select.location_mode","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1057,"y":316,"wires":[["8abf5432.db6398","cca8863e.aa24e8"],[]]},{"id":"9e63c2e4.aae3d","type":"trigger-state","z":"8c99a79b.f26338","name":"Motion detected in the shower","server":"d0cb210d.98634","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.multi_sensor_master_bathroom_motion_2","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"gbh2tvonieh","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"id":"dtnaav82ioi","targetType":"entity_id","targetValue":"input_select.master_bathroom_lighting_mode","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"does_not_include","comparatorValueDatatype":"list","comparatorValue":"Locked, Manual"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":207,"y":316,"wires":[["fce2eed9.71b98"],[]]},{"id":"ed484dcb.ba9f2","type":"switch","z":"8c99a79b.f26338","name":"Locked or manual?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Locked","vt":"str"},{"t":"eq","v":"Manual","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":564,"y":465,"wires":[["edbb6ac7.d702b8"],["711560dc.60c46"]]},{"id":"3edbc48b.98bcec","type":"trigger-state","z":"8c99a79b.f26338","name":"Motion is no longer detected in the shower","server":"d0cb210d.98634","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.multi_sensor_master_bathroom_motion_2","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"8bb5d69y26p","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"},{"id":"hb5e2q1tuj","targetType":"entity_id","targetValue":"input_select.master_bathroom_shower_lighting_mode","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"does_not_include","comparatorValueDatatype":"list","comparatorValue":"Manual, Locked"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":503,"y":527,"wires":[["f2f75fc7.fad25"],[]]},{"id":"cca8863e.aa24e8","type":"api-call-service","z":"8c99a79b.f26338","name":"Turn on the can lights","server":"d0cb210d.98634","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.switch_master_bathroom_cans","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1377,"y":336,"wires":[[]]},{"id":"ff0b6e1a.f19fd","type":"api-call-service","z":"8c99a79b.f26338","name":"Turn off the can lights","server":"d0cb210d.98634","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.switch_master_bathroom_cans","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1147,"y":442,"wires":[[]]},{"id":"ee0addbb.644e","type":"stoptimer3","z":"8c99a79b.f26338","duration":"5","durationType":"num","units":"Minute","payloadtype":"num","payloadval":"0","name":"Stoptimer","x":937,"y":476,"wires":[["ff0b6e1a.f19fd","1aeabcfc.c27893"],[]]},{"id":"f2f75fc7.fad25","type":"change","z":"8c99a79b.f26338","name":"Duration","rules":[{"t":"set","p":"duration","pt":"msg","to":"3","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":767,"y":527,"wires":[["ee0addbb.644e"]]},{"id":"711560dc.60c46","type":"change","z":"8c99a79b.f26338","name":"Duration","rules":[{"t":"set","p":"duration","pt":"msg","to":"60","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":767,"y":476,"wires":[["ee0addbb.644e"]]},{"id":"d0cb210d.98634","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
This works off the principle that a room can have modes of operation. In my case, I set mine up with four modes: idle (waiting for an automation to turn it on), motion (on due to a motion event), manual (on because I turned it on, but reset to idle after 1 hour of inactivity), and locked (no automations allowed). I use an input_select
helper for this.
Depending on the mode, a motion event turns on the light (assuming someone is home). If the motion sensor times out and returns to clear, the time starts. If a new motion event is registered, a stop message is sent to the stoptimer, telling it to stop the countdown. As soon as the motion clears, the stoptimer starts over.
In this case, a motion clear event sets the duration for a few minutes, and switching to a manual room mode sets the duration for 1 hour. Also note, setting the room to locked stops the timer - constraints on the other nodes also prevent the flow from turning on the lights when locked. This takes care of the situation that can arise with the stoptimer where you change the level or something and then three minutes later it releases the message and messes up the lighting.
If the stoptimer successfully makes it to the end of the countdown, it sends the original message on, turning off the light and resetting the room mode to idle.
I have flavors of this that include multiple motion triggers, as well as control for dimmers based on room illuminance. Iâm getting really close to never touching the lights again. I think the last thing to do is leverage the timed-counter
node to gradually increase duration as more motion events are observed in a given time frame, effectively locking out a room that is heavily occupied. Like during a party. Or when my kids are going ape shit.