Motion sensor & node red: how to get it to reset the timer

I’ve been struggling with this for a bit. I have a Hue lamp (Hue Integration) with a Aqura Motion sensor (linked through MQTT2Zigbee) and am using Node-Red to have it turn on and off based on motion.

The problem: I expected it to ‘keep alive’ everytime there is motion detected. The sensor is set to 30 second intervals. Whenever it stops detecting motion I don’t want it to directly turn off (not dancing in-front of it the entire time) but wait for a short while, and then turn off if still no motion is detected.

I currently use a for … seconds on the node. The problem is, it will start that and just then turn off after those seconds. Even if there clearly is motion during that time. How to I break it if there is motion? I’ve tried including a second check later on, but no luck.

My ultimate goal is to actually have it based on behavior how long it waits. So say normally it turns off if no motion is detected for 1 or 2 minutes, but when it’s at night, the lights are dimmed and the tv is on someone is probably just getting a snack and it turns off after 30 seconds no motion detected.
Even more ideal: when I press the Hue dimmer-switch to turn on the lamp manually that it fully ignores the motion sensor (or turns off after a much longer period of inactivity)

If you use the trigger node instead of the timer mode you can retrigger when a new message arrives or send msg.reset to cancel the timer completely

[{"id":"99ca33a526ca4e72","type":"trigger-state","z":"541531b2.741d98","name":"Living Motion Sensor off","server":"","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.living_livingmultisensor_motion_sensor_status","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"entity_id","targetValue":"light.kitchen_downlights","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"},{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"}],"inputs":1,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":true,"x":170,"y":1140,"wires":[["e21e24a787bba708"],[]]},{"id":"e21e24a787bba708","type":"trigger","z":"541531b2.741d98","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"2","extend":true,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":670,"y":1140,"wires":[["391ae9d5eebe5f3d"]]},{"id":"82aa9e307a031dcd","type":"change","z":"541531b2.741d98","name":"Reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":1100,"wires":[["e21e24a787bba708"]]},{"id":"d827481a039763b4","type":"api-current-state","z":"541531b2.741d98","name":"After dark?","server":"","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.after_dark","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"for":0,"forType":"num","forUnits":"minutes","x":670,"y":1080,"wires":[["6f5839f8e0df10c4"],[]]},{"id":"f6eec7a75037cc87","type":"trigger-state","z":"541531b2.741d98","name":"Living Motion Sensor on","server":"","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.living_livingmultisensor_motion_sensor_status","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"entity_id","targetValue":"light.kitchen_downlights","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"},{"targetType":"entity_id","targetValue":"light.dining","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"},{"targetType":"entity_id","targetValue":"light.living_livingdownlights_dimmer_1","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"},{"targetType":"entity_id","targetValue":"light.living_lamp","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"},{"targetType":"entity_id","targetValue":"remote.livingharmony_2","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"},{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"inputs":1,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":true,"x":170,"y":1080,"wires":[["d827481a039763b4","82aa9e307a031dcd"],[]]},{"id":"b7275aaf356bdc77","type":"api-call-service","z":"541531b2.741d98","name":"Kitchen Cabinet Lights","server":"","version":5,"debugenabled":false,"domain":"light","service":"","areaId":[],"deviceId":[],"entityId":["light.kitchen_cabinetlights"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"data"}],"queue":"none","output_location":"payload","output_location_type":"msg","x":1140,"y":1100,"wires":[[]]},{"id":"6f5839f8e0df10c4","type":"change","z":"541531b2.741d98","name":"Turn On 1%","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.service","pt":"msg","to":"turn_on","tot":"str"},{"t":"set","p":"payload.data.brightness","pt":"msg","to":"2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":890,"y":1080,"wires":[["b7275aaf356bdc77"]]},{"id":"391ae9d5eebe5f3d","type":"change","z":"541531b2.741d98","name":"Turn Off","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.service","pt":"msg","to":"turn_off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":1140,"wires":[["b7275aaf356bdc77"]]}]
2 Likes

Could you share the code, not just a picture please?

What do you mean? The export to Json of the flow? There is no real code written by me. Just the visual flows.

oh I never tried anything like that (most flows are quite ‘simple’ save for a few I actually wrote functions behind). Never tried much with the msg object. Going to try this!

The delay node can be set up to hold back messages until there have been no message for a while and then it will send it.
I can’t remember the settings to do it, but you can maybe figure it out.

I have in the past used the merge node for the same function.
It only have one setting and that is the delay with out message before it send the message on, so it is pretty simple to use.

This seems to work perfectly! Thank you! :slight_smile:

Disclaimer: it didn’t work at first because I also had another really dumb error. The check if the lux is above a number and then always turn the light off actually was set as LOWER (copy/paste and never fixed it it seems). So the light actually turned off when it became dark :slight_smile:

thank you for the flow idea. Is there a way to run the on event only on the initial start?
So i want to create a scene of the current state of a lamp, increase the brightness and after the “delay” i want to the initial state (what is saved in the scene).

Summary

[{"id":"99ca33a526ca4e72","type":"trigger-state","z":"9feb70acd550054e","name":"KInderzimmer Motion Sensor off","server":"c28397a4.d6d068","version":4,"inputs":1,"outputs":2,"exposeAsEntityConfig":"","entityId":"binary_sensor.kinderzimmer_bewegungsmelder_motion","entityIdType":"exact","debugEnabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"}],"customOutputs":[],"outputInitially":false,"stateType":"str","enableInput":true,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"x":170,"y":440,"wires":[["e21e24a787bba708"],[]]},{"id":"e21e24a787bba708","type":"trigger","z":"9feb70acd550054e","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"1","extend":true,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":650,"y":440,"wires":[["53c12d86e6c19f2b"]]},{"id":"82aa9e307a031dcd","type":"change","z":"9feb70acd550054e","name":"Reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":400,"wires":[["e21e24a787bba708"]]},{"id":"f6eec7a75037cc87","type":"trigger-state","z":"9feb70acd550054e","name":"Kinderzimmer Motion Sensor on","server":"c28397a4.d6d068","version":4,"inputs":1,"outputs":2,"exposeAsEntityConfig":"","entityId":"binary_sensor.kinderzimmer_bewegungsmelder_motion","entityIdType":"exact","debugEnabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"customOutputs":[],"outputInitially":false,"stateType":"str","enableInput":true,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"x":170,"y":380,"wires":[["82aa9e307a031dcd","7edf5ca84dd15fe4"],[]]},{"id":"2bef7ccbc8e1c886","type":"api-current-state","z":"9feb70acd550054e","name":"Helligkeit <= 25 Lux","server":"c28397a4.d6d068","version":3,"outputs":2,"halt_if":"25","halt_if_type":"num","halt_if_compare":"lte","entity_id":"sensor.kinderzimmer_bewegungsmelder_illuminance","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","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":700,"y":320,"wires":[["6c84b0b1c6931adf"],[]]},{"id":"7edf5ca84dd15fe4","type":"time-range-switch","z":"9feb70acd550054e","name":"","lat":"49.59893880126861","lon":"8.458586927239029","startTime":"sunset","endTime":"dawn","startOffset":0,"endOffset":0,"x":460,"y":320,"wires":[["2bef7ccbc8e1c886"],[]]},{"id":"6c84b0b1c6931adf","type":"api-call-service","z":"9feb70acd550054e","name":"Create Scene fensterbank_before_motion","server":"c28397a4.d6d068","version":5,"debugenabled":false,"domain":"scene","service":"create","areaId":[],"deviceId":[],"entityId":[],"data":"{\t \"scene_id\":\"fensterbank_before_motion\",\t \"snapshot_entities\":\"light.kinderzimmer_fensterbank\"\t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1020,"y":320,"wires":[["569c9498cf355059"]]},{"id":"569c9498cf355059","type":"api-call-service","z":"9feb70acd550054e","name":"Fensterbank Brightness 180","server":"c28397a4.d6d068","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.kinderzimmer_fensterbank"],"data":"{\t \"brightness\": 180,\t \"transition\": 1\t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1340,"y":320,"wires":[[]]},{"id":"53c12d86e6c19f2b","type":"api-call-service","z":"9feb70acd550054e","name":"Active Scene \"fensterbank_before_motion\"","server":"c28397a4.d6d068","version":5,"debugenabled":false,"domain":"scene","service":"turn_on","areaId":[],"deviceId":[],"entityId":["scene.fensterbank_before_motion"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1010,"y":440,"wires":[[]]},{"id":"c28397a4.d6d068","type":"server","name":"Home Assistant","addon":true}]