Help with Node Red flow not always working correctly

I have a flow that detects movement from 2 motion sensors. It checks if the time is between sunset and sunrise and if so turns on an led strip under my bed. It waits 7 seconds and then turns it off. From time to time, the strip stays on after sunrise and I have to turn them off manually. I have a feeling it happens when the lights are turned on right before sunrise but I don’t know why the timer wouldn’t turn them off. Any ideas?

Nothing is jumping out at me but can you share an export of your flow? Then others (like me) can import it and see the config of those nodes and help more. There’s a lot of key details hidden in those rectangles.

The only thing I do notice from your screenshot is that there’s a green status icon next to off on your motion sensor nodes. That suggests to me that you have those nodes set to send out a message any time the state of the motion sensor changes, including if its stange changes to off (which usually means “no movement detected”). Is there a reason you have it configured that way? I would think you would only want the lights to turn on when they change to on (i.e. movement detected).

Not that this explains your issue, I would still expect the lights to turn off 7 seconds later, just thought it was odd and figured I’d ask.

Thanks for the quick response and your assistance. It is configured that way because I don’t always know what I am doing :wink: .

Here is my flow as you requested.

[{"id":"831ed45d.9d7328","type":"tab","label":"Bed Lights","disabled":false,"info":""},{"id":"aea51cf5.68858","type":"server-state-changed","z":"831ed45d.9d7328","name":"Left Bed Sensor Movement","server":"1d112e9e.b96771","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.left_bed_motion_sensor","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":130,"y":60,"wires":[["2dad1035.35a27"]]},{"id":"2dad1035.35a27","type":"time-range-switch","z":"831ed45d.9d7328","name":"","lat":"28.676290","lon":"-81.261660","startTime":"sunset","endTime":"sunrise","startOffset":0,"endOffset":0,"x":440,"y":60,"wires":[["ed14b3b9.d9427"],[]]},{"id":"ed14b3b9.d9427","type":"switch","z":"831ed45d.9d7328","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":650,"y":60,"wires":[["420597a4.9c4fa8"],[]]},{"id":"420597a4.9c4fa8","type":"api-call-service","z":"831ed45d.9d7328","name":"Turn on Bed LEDs Red","server":"1d112e9e.b96771","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.smart_led_light_strip_msl320c_main_channel","data":"{\"color_name\":\"red\",\"brightness_pct\":5}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":910,"y":60,"wires":[["11c452c6.57a44d"]]},{"id":"714f7f1a.f8f1","type":"api-call-service","z":"831ed45d.9d7328","name":"Turn off Bed LEDs","server":"1d112e9e.b96771","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.smart_led_light_strip_msl320c_main_channel","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1450,"y":80,"wires":[[]]},{"id":"8fd4481.d953bb8","type":"server-state-changed","z":"831ed45d.9d7328","name":"Right Bed Sensor Movement","server":"1d112e9e.b96771","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.right_bed_motion_sensor","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":140,"y":140,"wires":[["2dad1035.35a27"]]},{"id":"11c452c6.57a44d","type":"stoptimer","z":"831ed45d.9d7328","duration":"7","units":"Second","payloadtype":"num","payloadval":"0","name":"","x":1200,"y":80,"wires":[["714f7f1a.f8f1"],[]]},{"id":"1d112e9e.b96771","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true}]

Huh, looking at it I really don’t know, nothing looks wrong. I guess a few non-obvious notes that could be impacting this:

  • Stop timer is going to restart the timer every time it gets a new message. So if there’s a lot of movement going on around the motion sensors it might stay on longer then 7 seconds

  • If your motion sensors are like mine the state changes to on when movement is detected and back to off about 60 seconds or so later if no movement is detected at that time. Right now that flipping back to off is going to turn on the lights and restart the timer so is it possible that’s what you’re seeing? EDIT: Ignore this, I see your switch node prevents this

  • If you redeploy the stoptimer node then it will clear any pending timers. Any chance you’re messing with node red soon after you wake up and killing the timer in the middle? Note that you can essentially eliminate this effect if you change the deploy settings to “modified nodes” only, it defaults to full redeploy though which redeploys all nodes on each change.

Screen Shot 2021-05-25 at 12.32.16 PM

But other then that I’m out of ideas. Nothing really looks wrong and I can’t figure out any way you could be getting into a race condition so I’m not really sure how your lights could be getting left on.

Thanks for looking. I am not messing with Node Red when this happens. I doesn’t happen every day but sometimes it happens in the middle of the night when it gets triggered by my cat.

Try and start the timer in parallel to the light on action. Not after, but at the same time.
See if that changes things.

Great idea! I’ll give it a try. thx.

The above flow requires an off command to be sent in order to extend the timer. If motion starts a few seconds before the off timer expires and continues, the light will shut off. I use the on and check state node so I don’t send multiple on commands if there is continued motion. But the start of any movemnet extends the timer.

[{"id":"79ff4a79.521f44","type":"api-call-service","z":"bc8305a4.6ea4f8","name":"AC Light","server":"","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lifx1","data":"{\"brightness\":250}","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1580,"y":340,"wires":[["7d92c6e7.921ce8"]]},{"id":"7d92c6e7.921ce8","type":"stoptimer","z":"bc8305a4.6ea4f8","duration":"45","units":"Second","payloadtype":"num","payloadval":"0","name":"","x":1930,"y":340,"wires":[["8f9cb90f.c62d68"],[]]},{"id":"8f9cb90f.c62d68","type":"api-call-service","z":"bc8305a4.6ea4f8","name":"AC Light","server":"","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.lifx1","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":2280,"y":340,"wires":[[]]},{"id":"6100a6e2.aa0138","type":"api-current-state","z":"bc8305a4.6ea4f8","name":"","server":"","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.lifx1","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1350,"y":300,"wires":[["7d92c6e7.921ce8"],["79ff4a79.521f44"]]}]

Thank you!

Another reason could be that the turn_off service node gets triggered, but it doesn’t get send to the Light. Did you have similar issues when controlling it from HA directly? You could use file- or specific logging nodes for a better view of what’s going on.