Motion Sensor Bug - how to solve

Hi there,

I use node red at my home assistant.
This is my current flow:

[{"id":"af7416ee.37c178","type":"tab","label":"Deckenlampe Flur","disabled":false,"info":""},{"id":"973114867756a562","type":"switch","z":"af7416ee.37c178","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":650,"y":260,"wires":[["bf52dbcb745d6dd0"],["bcd0fd015afa5557","f3ee92165a0a5b36"]]},{"id":"34879843e54e2e97","type":"api-call-service","z":"af7416ee.37c178","name":"Tag 100%","server":"818794df.29c548","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"","data":"{\t   \"entity_id\": \"light.deckenlampe_flur\",\t   \"brightness_pct\": 100\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1420,"y":100,"wires":[["bcd0fd015afa5557","f3ee92165a0a5b36"]]},{"id":"669e14131ca9294b","type":"trigger-state","z":"af7416ee.37c178","name":"","server":"818794df.29c548","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.flur_bewegungsmeldung_occupancy","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":"is","comparatorValueDatatype":"bool","comparatorValue":"true","propertyValue":"new_state.state"},{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":"is","comparatorValueDatatype":"bool","comparatorValue":"false","propertyValue":"new_state.state"}],"inputs":1,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"habool","enableInput":true,"x":290,"y":260,"wires":[["973114867756a562"],["973114867756a562"]]},{"id":"bf52dbcb745d6dd0","type":"time-range-switch","z":"af7416ee.37c178","name":"Tag 5:30-22:00","lat":"50.11092","lon":"8.68213","startTime":"5:30","endTime":"22:00","startOffset":0,"endOffset":0,"x":1120,"y":140,"wires":[["6db1b2fb28cec70d"],["f34ae9890e2488d0"]]},{"id":"2d6d31857865b99f","type":"api-call-service","z":"af7416ee.37c178","name":"Nacht 30%","server":"818794df.29c548","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"","data":"{\t   \"entity_id\": \"light.deckenlampe_flur\",\t   \"brightness_pct\": 30\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1710,"y":200,"wires":[["bcd0fd015afa5557","f3ee92165a0a5b36"]]},{"id":"6db1b2fb28cec70d","type":"api-current-state","z":"af7416ee.37c178","name":"< 30 lx","server":"818794df.29c548","version":2,"outputs":2,"halt_if":"30","halt_if_type":"num","halt_if_compare":"lte","entity_id":"sensor.flur_bewegungsmeldung_illuminance","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1290,"y":120,"wires":[["34879843e54e2e97"],[]]},{"id":"f34ae9890e2488d0","type":"time-range-switch","z":"af7416ee.37c178","name":"Nacht 22:00-23:00","lat":"","lon":"","startTime":"22:00","endTime":"23:00","startOffset":0,"endOffset":0,"x":1370,"y":240,"wires":[["2d6d31857865b99f"],["820a1a31516b2f7d"]]},{"id":"e57280f09f8f90c9","type":"api-call-service","z":"af7416ee.37c178","name":"Nacht 10%","server":"818794df.29c548","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"","data":"{\t   \"entity_id\": \"light.deckenlampe_flur\",\t   \"brightness_pct\": 10\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1710,"y":300,"wires":[["bcd0fd015afa5557","f3ee92165a0a5b36"]]},{"id":"297d51b5077d171c","type":"time-range-switch","z":"af7416ee.37c178","name":"Tag 5:30-22:00","lat":"","lon":"","startTime":"5:30","endTime":"22:00","startOffset":0,"endOffset":0,"x":400,"y":500,"wires":[["7f7c3ec279475b35"],["1628aa0897b1860a"]]},{"id":"1628aa0897b1860a","type":"time-range-switch","z":"af7416ee.37c178","name":"Nacht 22:00-23:00","lat":"","lon":"","startTime":"22:00","endTime":"23:00","startOffset":0,"endOffset":0,"x":610,"y":500,"wires":[["9c47284691d735a3"],["f47eec82f5e7475f"]]},{"id":"863098b73f51e85e","type":"server-state-changed","z":"af7416ee.37c178","name":"Deckenlampe Schlafzimmer","server":"818794df.29c548","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.deckenlampe_schlafzimmer","entityidfiltertype":"exact","outputinitially":false,"state_type":"habool","haltifstate":"true","halt_if_type":"bool","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":180,"y":500,"wires":[["297d51b5077d171c"],[]]},{"id":"f47eec82f5e7475f","type":"api-call-service","z":"af7416ee.37c178","name":"Nacht 10%","server":"818794df.29c548","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"","data":"{\t   \"entity_id\": \"light.deckenlampe_schlafzimmer\",\t   \"brightness_pct\": 10\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":810,"y":540,"wires":[[]]},{"id":"9c47284691d735a3","type":"api-call-service","z":"af7416ee.37c178","name":"Nacht 30%","server":"818794df.29c548","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"","data":"{\t   \"entity_id\": \"light.deckenlampe_schlafzimmer\",\t   \"brightness_pct\": 30\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":810,"y":480,"wires":[[]]},{"id":"7f7c3ec279475b35","type":"api-call-service","z":"af7416ee.37c178","name":"Tag 100%","server":"818794df.29c548","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"","data":"{\t   \"entity_id\": \"light.deckenlampe_schlafzimmer\",\t   \"brightness_pct\": 100\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":560,"y":440,"wires":[[]]},{"id":"820a1a31516b2f7d","type":"api-current-state","z":"af7416ee.37c178","name":"< 30 lx","server":"818794df.29c548","version":2,"outputs":2,"halt_if":"30","halt_if_type":"num","halt_if_compare":"lte","entity_id":"sensor.flur_bewegungsmeldung_illuminance","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1570,"y":280,"wires":[["e57280f09f8f90c9"],[]]},{"id":"bcd0fd015afa5557","type":"api-current-state","z":"af7416ee.37c178","d":true,"name":"","server":"32e60e8e.d121f2","version":2,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.flur_beleuchtung_pause","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"x":1120,"y":320,"wires":[["df1b3d400e375aba"],["f3ee92165a0a5b36"]]},{"id":"df1b3d400e375aba","type":"api-call-service","z":"af7416ee.37c178","name":"","server":"32e60e8e.d121f2","version":3,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.flur_beleuchtung_pause","data":"","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1600,"y":360,"wires":[[]]},{"id":"f3ee92165a0a5b36","type":"trigger","z":"af7416ee.37c178","name":"","op1":"","op2":"off","op1type":"nul","op2type":"str","duration":"1","extend":true,"overrideDelay":false,"units":"min","reset":"true","bytopic":"all","topic":"topic","outputs":1,"x":1550,"y":420,"wires":[["9d0321800f18252e"]]},{"id":"9d0321800f18252e","type":"api-call-service","z":"af7416ee.37c178","name":"","server":"818794df.29c548","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.deckenlampe_flur","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1750,"y":420,"wires":[[]]},{"id":"d64a386e60b2fb73","type":"server-state-changed","z":"af7416ee.37c178","name":"Deckenlampe Flur","server":"818794df.29c548","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.deckenlampe_flur","entityidfiltertype":"exact","outputinitially":false,"state_type":"habool","haltifstate":"true","halt_if_type":"bool","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":210,"y":320,"wires":[["67e152a12832f65f"],[]]},{"id":"67e152a12832f65f","type":"trigger","z":"af7416ee.37c178","name":"","op1":"","op2":"1","op1type":"nul","op2type":"str","duration":"5","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":450,"y":320,"wires":[["973114867756a562"]]},{"id":"818794df.29c548","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30},{"id":"32e60e8e.d121f2","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

I have the problem, that the sensor not always detect motion.
Or sometimes the light turns on and 5 sec later it turns off.

The motion sensor is a philips hue motion sensor and the light is controlled by a shelly dimmer 2.

Can anybody find the bug in my flow?

Regards
Timo

Kind of difficult to read with the language barrier, but what I see is if the trigger state goes false (no motion detected), it starts a 1 min trigger and once that expires, it turns the light off.

But you have no logic in there to say, “but if you see motion again, stop the timer”. So even if the motion sensors sees motion again, it will still turn the light off after 1 min.

Heres one of my simples motions flows. I like to use varitime-delay node.

So pretty much,

  1. Motion on will turn light turns on and STOP message is sent to timer node
  2. Motion off starts 5 minute timer
  3. IF 5 minute timer expires, light is turned off.
  4. BUT if motion is sensed again, it sends a STOP which stops the timer and resets it.

Cause you have to account for your motion sensor not sensing you for a short period of time, but that doesnt necessarily mean you arent still in the room. Maybe you just moved into a corner for awhile or stopped moving.

So pretty much in my flow, its needs to see no motion triggered for a continous 5 minutes before it will turn off the light. In your flow, it just needs to see no motion for 1 times and then it will turn the light off after 1 minutes, EVEN IF it sees motion after the timer starts.

thank you for the quick response and helpful answer!
Can you please share your code, so I can import this to my flow to modify it?

edit:

[{"id":"8eb7fd1a6a8a231c","type":"server-state-changed","z":"d87a3b8fb6a5944b","name":"Flur Bewegung true","server":"818794df.29c548","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.flur_bewegungsmeldung_occupancy","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"true","halt_if_type":"bool","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"},{"property":"payload","propertyType":"msg","value":"stop","valueType":"str"}],"x":150,"y":140,"wires":[["38b98661fa306ddc","01aabd19cc0f4ce1"],[]]},{"id":"818794df.29c548","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

I created this flow, but something seems to be wrong…?

Sorry for slow response. Here you go.

[{"id":"5bda42c7a7024899","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"bde4337666715481","type":"server-state-changed","z":"5bda42c7a7024899","name":"Garage motion on","server":"ab3e2c53.e3491","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.motion_garage","entityidfiltertype":"exact","outputinitially":false,"state_type":"habool","haltifstate":"true","halt_if_type":"bool","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"stop","valueType":"str"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":240,"y":165,"wires":[["1acbd3ae5ca1277c","593c0cbdba1ce948"],[]]},{"id":"d116184699f9f534","type":"server-state-changed","z":"5bda42c7a7024899","name":"Garage motion off","server":"ab3e2c53.e3491","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.motion_garage","entityidfiltertype":"exact","outputinitially":false,"state_type":"habool","haltifstate":"false","halt_if_type":"bool","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":240,"y":215,"wires":[["593c0cbdba1ce948"],[]]},{"id":"593c0cbdba1ce948","type":"stoptimer-varidelay","z":"5bda42c7a7024899","duration":"5","durationType":"num","units":"Minute","payloadtype":"num","payloadval":"0","name":"5 min","reporting":"every_second","persist":true,"ignoretimerpass":false,"x":445,"y":195,"wires":[["921e2d427616e44d"],[],[]]},{"id":"1acbd3ae5ca1277c","type":"api-call-service","z":"5bda42c7a7024899","name":"Turn on light","server":"ab3e2c53.e3491","version":3,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.garage","data":"","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":475,"y":120,"wires":[[]]},{"id":"921e2d427616e44d","type":"api-call-service","z":"5bda42c7a7024899","name":"Turn off light","server":"ab3e2c53.e3491","version":3,"debugenabled":true,"service_domain":"light","service":"turn_off","entityId":"light.garage","data":"","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":630,"y":180,"wires":[[]]},{"id":"ab3e2c53.e3491","type":"server","name":"AvilaSmartHome","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]
1 Like

Thank you - I will try it, as soon as I have time for it :slight_smile: