Motion-based light with 10-second dimming countdown

Hi forum. New user here :wave:

I hope there is someone out there with some creative juices and relevant coding skills (note: I have none) that could help me improve my current setup.

I have the following flow for motion-based lighting in my living room.

When motion is no longer detected, the lights are dimmed to 50% for 10 seconds, then turned off. Before dimming, the current brightness value is stored in context and then called when motion is detected again (so lights revert to 100% brightness).

Sometimes, I might turn the lights on manually and adjust the brightness before walking into the room. When motion is detected (i.e. I walk in), the brightness will then reset back to the value called by the switch node.

What I'd like to be able to do is something like this:
Motion detected > Check if timeout active (during dimming phase) > If yes, double the brightness, if not then use existing (current) brightness value.

I'd be very grateful for any suggestions. Code below:

[{"id":"f04d96250610a63c","type":"api-call-service","z":"a64cb88673a8ada8","name":"Turn on light","server":"6e6a7fd05cb77906","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.living_room_lights"],"data":"{\"brightness\":msg.data.attributes.brightness}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":510,"y":60,"wires":[[]]},{"id":"60a19480ee470b41","type":"api-call-service","z":"a64cb88673a8ada8","name":"Turn off light","server":"6e6a7fd05cb77906","version":5,"debugenabled":false,"domain":"light","service":"turn_off","areaId":[],"deviceId":[],"entityId":["light.living_room_lights"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":510,"y":200,"wires":[[]]},{"id":"c06ea1849c9aa913","type":"trigger-state","z":"a64cb88673a8ada8","name":"Light turn off rules","server":"6e6a7fd05cb77906","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":["input_boolean.toggle_auto_living","input_boolean.nighttime","binary_sensor.shmos_living_motion","media_player.sony_bravia_tv"],"entityidfiltertype":"list","debugenabled":false,"constraints":[{"targetType":"entity_id","targetValue":"input_boolean.toggle_auto_living","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"targetType":"entity_id","targetValue":"media_player.sony_bravia_tv","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"},{"targetType":"entity_id","targetValue":"binary_sensor.shmos_living_motion","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"on"},{"targetType":"entity_id","targetValue":"input_boolean.nighttime","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":false,"x":130,"y":140,"wires":[["d6e52e666ac54149"],[]]},{"id":"a56933a07bec11b1","type":"trigger-state","z":"a64cb88673a8ada8","name":"Light turn on rules","server":"6e6a7fd05cb77906","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":["input_boolean.toggle_auto_living","input_boolean.nighttime","binary_sensor.shmos_living_motion"],"entityidfiltertype":"list","debugenabled":false,"constraints":[{"targetType":"entity_id","targetValue":"input_boolean.toggle_auto_living","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"targetType":"entity_id","targetValue":"input_boolean.nighttime","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"targetType":"entity_id","targetValue":"binary_sensor.shmos_living_motion","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":false,"x":130,"y":60,"wires":[["d6e52e666ac54149","be221a3e3f6ab66a"],[]]},{"id":"d6e52e666ac54149","type":"trigger","z":"a64cb88673a8ada8","name":"","op1":"dim","op2":"off","op1type":"str","op2type":"str","duration":"10","extend":false,"overrideDelay":false,"units":"s","reset":"on","bytopic":"all","topic":"topic","outputs":2,"x":330,"y":140,"wires":[["da173965314d66fc"],["60a19480ee470b41"]]},{"id":"be221a3e3f6ab66a","type":"change","z":"a64cb88673a8ada8","name":"get brightness","rules":[{"t":"set","p":"data","pt":"msg","to":"lightBrightness","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":60,"wires":[["f04d96250610a63c"]]},{"id":"99d231e4c9f0a000","type":"api-call-service","z":"a64cb88673a8ada8","name":"Turn on light","server":"6e6a7fd05cb77906","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.living_room_lights"],"data":"{\"brightness\":msg.data.attributes.brightness/2}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":670,"y":140,"wires":[[]]},{"id":"da173965314d66fc","type":"api-current-state","z":"a64cb88673a8ada8","name":"Light on?","server":"6e6a7fd05cb77906","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.living_room_lights","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"lightBrightness","propertyType":"flow","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":500,"y":140,"wires":[["99d231e4c9f0a000"],[]]},{"id":"6e6a7fd05cb77906","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

Hello @therealbuzzkill and welcome to the forum.

Here is a demo flow which I think does what you are after. It uses two trigger nodes to get two countdowns, 10s at full brightness, 10s at half brightness.

[{"id":"9328b198f90903e1","type":"trigger","z":"2fbaad6f2b46c829","name":"","op1":"100","op2":"50","op1type":"num","op2type":"num","duration":"10","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":530,"y":80,"wires":[["db623ad80740b29b","d4baeb3b2284de1e"]]},{"id":"fe42d9dc7dcb29e9","type":"inject","z":"2fbaad6f2b46c829","name":"motion detected","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"go","payloadType":"str","x":340,"y":80,"wires":[["9328b198f90903e1","a6e84223fdadf504"]]},{"id":"db623ad80740b29b","type":"trigger","z":"2fbaad6f2b46c829","name":"","op1":"","op2":"0","op1type":"nul","op2type":"num","duration":"10","extend":false,"overrideDelay":false,"units":"s","reset":"100","bytopic":"all","topic":"topic","outputs":1,"x":710,"y":120,"wires":[["d4baeb3b2284de1e"]]},{"id":"d4baeb3b2284de1e","type":"debug","z":"2fbaad6f2b46c829","name":"To lights control","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":80,"wires":[]},{"id":"a6e84223fdadf504","type":"debug","z":"2fbaad6f2b46c829","name":"Motion","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":40,"wires":[]}]

Thank you very much, @jbudd. This looks like it could do the trick with some adaptions, for example in my case, I need to have separate motion-on and motion-off triggers (i.e. the rules nodes), and also it seems that the demo would always set my light to 100% brightness which is not quite how I'd like it to behave.

I feel like I'm very close to the solution but I don't know Node-Red well enough yet...

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.