Rate my flow

Hi, ive been diving into node red for my more advanced automations and I made something that I think could be done smoother and simpler.

I want the lights to turn off when I’m in bed (motion sensor) and my phone is charging.

Flow code
[{"id":"136c13e32b82c7cc","type":"api-call-service","z":"97aba0f8a5c773f2","name":"","server":"","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.osram_weis_level_light_color_on_off","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1790,"y":2240,"wires":[["c1d58ef68de71456"]]},{"id":"165fa34a49b72a46","type":"api-current-state","z":"97aba0f8a5c773f2","name":"","server":"","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.fig_lx1_is_charging","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1020,"y":2220,"wires":[["b9f0e58e7f45fb58","444d71a349657ca8"],[]]},{"id":"b9f0e58e7f45fb58","type":"debug","z":"97aba0f8a5c773f2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":930,"y":2160,"wires":[]},{"id":"2498ba88d1497e0c","type":"debug","z":"97aba0f8a5c773f2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1550,"y":2180,"wires":[]},{"id":"c32a01774329d4c8","type":"time-range-switch","z":"97aba0f8a5c773f2","name":"","lat":"","lon":"","startTime":"20:00","endTime":"01:00","startOffset":0,"endOffset":0,"x":1550,"y":2240,"wires":[["136c13e32b82c7cc","2498ba88d1497e0c"],[]]},{"id":"99f55ee0d6f4d4fd","type":"debug","z":"97aba0f8a5c773f2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":190,"y":2180,"wires":[]},{"id":"ad204a8f5782af07","type":"server-state-changed","z":"97aba0f8a5c773f2","name":"","server":"","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.philips_sml001_fc6b090b_occupancy","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","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"}],"x":340,"y":2240,"wires":[["99f55ee0d6f4d4fd","4af04180b89cfb58"],[]]},{"id":"c1d58ef68de71456","type":"debug","z":"97aba0f8a5c773f2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1810,"y":2180,"wires":[]},{"id":"4af04180b89cfb58","type":"msg-resend","z":"97aba0f8a5c773f2","interval":"1","intervalUnit":"secs","maximum":"120","bytopic":false,"clone":false,"firstDelayed":false,"addCounters":false,"waitForResend":false,"highRate":false,"outputCountField":"","outputMaxField":"","name":"","x":710,"y":2220,"wires":[["165fa34a49b72a46","49e679dd53a774f0"]]},{"id":"49e679dd53a774f0","type":"debug","z":"97aba0f8a5c773f2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":2160,"wires":[]},{"id":"444d71a349657ca8","type":"delay","z":"97aba0f8a5c773f2","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"allowrate":false,"outputs":1,"x":1350,"y":2240,"wires":[["c32a01774329d4c8"]]}]

First I tried to build an AND gate, but I had to inject the motion sensor cue into the charging state, so I simplified it and put them one after the other.

The motion sensor is almost under the bed, so it tracks if I walk in front of the bed. I plug the phone in afterwards so the state check for charging get retriggered by the “msg-resend” (I tried first with a trigger node but could figure out how to reset after a few minutes in a smart way).
To save traffic, I limit the signal afterwards and then do a time check and the turn the lights off.

I haven’t extensively tested this, but it seems to work, but it doesn’t feel very elegant, and I wonder if I could do it only with no extra installed msg resend node.
I also have more workflows in mind that rely on input_booleans that get set by multiple sensors, that don’t depend on each other like in this example and I haven’t found a good way for these “AND gate” style setups.

Cheers.

1 Like

Would a ‘Wait until’ node work for you? Something like this:

  • Bed Motion sensor state changes to on
  • Wait until phone is charging state changes to on
  • check the time window
  • turn off the light

You’d want to set an appropriate timeout on the wait until node, so it doesn’t just wait forever. Here is the flow code:

[{"id":"ad204a8f5782af07","type":"server-state-changed","z":"4ef527e6.8c4de8","name":"","server":"","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.philips_sml001_fc6b090b_occupancy","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","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,"x":280,"y":1260,"wires":[["99f55ee0d6f4d4fd","3ffce40863c4a479"],[]]},{"id":"99f55ee0d6f4d4fd","type":"debug","z":"4ef527e6.8c4de8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":130,"y":1200,"wires":[]},{"id":"3ffce40863c4a479","type":"ha-wait-until","z":"4ef527e6.8c4de8","name":"wait until charging","server":"cf706f48.6afbf8","outputs":2,"entityId":"binary_sensor.fig_lx1_is_charging","entityIdFilterType":"exact","property":"state","comparator":"is","value":"on","valueType":"str","timeout":"2","timeoutType":"num","timeoutUnits":"minutes","entityLocation":"data","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":650,"y":1260,"wires":[["c32a01774329d4c8","b9f0e58e7f45fb58"],[]]},{"id":"c32a01774329d4c8","type":"time-range-switch","z":"4ef527e6.8c4de8","name":"","lat":"","lon":"","startTime":"20:00","endTime":"01:00","startOffset":0,"endOffset":0,"x":850,"y":1260,"wires":[["136c13e32b82c7cc","2498ba88d1497e0c"],[]]},{"id":"b9f0e58e7f45fb58","type":"debug","z":"4ef527e6.8c4de8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":870,"y":1180,"wires":[]},{"id":"136c13e32b82c7cc","type":"api-call-service","z":"4ef527e6.8c4de8","name":"","server":"","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.osram_weis_level_light_color_on_off","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"x":1050,"y":1260,"wires":[["c1d58ef68de71456"]]},{"id":"2498ba88d1497e0c","type":"debug","z":"4ef527e6.8c4de8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1010,"y":1220,"wires":[]},{"id":"c1d58ef68de71456","type":"debug","z":"4ef527e6.8c4de8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1210,"y":1200,"wires":[]},{"id":"cf706f48.6afbf8","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

It’s not necessarily better than your approach, just sharing the way I’d do it.

Both motion and boolean as triggers, you never know in which order you’ll do things. Whenever either triggers, conditions check if smartphone is charging and if motion has been detected over the last 3 mins. If they check, then do.

[{"id":"291084d6.704c3c","type":"server-state-changed","z":"937ef78.ec6b408","name":"Triggers","server":"9405c3fe.d0a6c","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.philips_sml001_fc6b090b_occupancy, binary_sensor.fig_lx1_is_charging","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[],"x":140,"y":560,"wires":[["c350009e.22d89"]]},{"id":"c350009e.22d89","type":"api-current-state","z":"937ef78.ec6b408","name":"Charging?","server":"9405c3fe.d0a6c","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.fig_lx1_is_charging","state_type":"str","blockInputOverrides":true,"outputProperties":[],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":310,"y":560,"wires":[["43beffba.e3a0e"],[]]},{"id":"43beffba.e3a0e","type":"api-current-state","z":"937ef78.ec6b408","name":"Motion detected during the last 3 minutes?","server":"9405c3fe.d0a6c","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.philips_sml001_fc6b090b_occupancy","state_type":"str","blockInputOverrides":true,"outputProperties":[{"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":410,"y":620,"wires":[["c47fbe8f.2553"],["25e4a2c4.efc27e"]]},{"id":"e9515f9b.a2011","type":"api-call-service","z":"937ef78.ec6b408","name":"","server":"","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.osram_weis_level_light_color_on_off","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1110,"y":580,"wires":[[]]},{"id":"c47fbe8f.2553","type":"time-range-switch","z":"937ef78.ec6b408","name":"","lat":"","lon":"","startTime":"20:00","endTime":"01:00","startOffset":0,"endOffset":0,"x":870,"y":580,"wires":[["e9515f9b.a2011"],[]]},{"id":"25e4a2c4.efc27e","type":"switch","z":"937ef78.ec6b408","name":"off <= 3 min?","property":"data.timeSinceChangedMs","propertyType":"msg","rules":[{"t":"lte","v":"180000","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":690,"y":640,"wires":[["c47fbe8f.2553"]]},{"id":"9405c3fe.d0a6c","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}]

This would achieve the same thing. I’m not 100% sure though

[{"id":"bad90401.b5aff8","type":"api-current-state","z":"937ef78.ec6b408","name":"Motion detected during the last 3 minutes?","server":"9405c3fe.d0a6c","version":3,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.philips_sml001_fc6b090b_occupancy","state_type":"str","blockInputOverrides":false,"outputProperties":[],"for":"3","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":410,"y":300,"wires":[[],["92b5b5af.1f0b88"]]},{"id":"728767b7.1b2ae8","type":"server-state-changed","z":"937ef78.ec6b408","name":"Triggers","server":"9405c3fe.d0a6c","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.philips_sml001_fc6b090b_occupancy, binary_sensor.fig_lx1_is_charging","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[],"x":140,"y":240,"wires":[["5e43c593.8723cc"]]},{"id":"5e43c593.8723cc","type":"api-current-state","z":"937ef78.ec6b408","name":"Charging?","server":"9405c3fe.d0a6c","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.fig_lx1_is_charging","state_type":"str","blockInputOverrides":true,"outputProperties":[],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":310,"y":240,"wires":[["bad90401.b5aff8"],[]]},{"id":"1baeb26f.b85dbe","type":"api-call-service","z":"937ef78.ec6b408","name":"","server":"","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.osram_weis_level_light_color_on_off","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":970,"y":260,"wires":[[]]},{"id":"92b5b5af.1f0b88","type":"time-range-switch","z":"937ef78.ec6b408","name":"","lat":"","lon":"","startTime":"20:00","endTime":"01:00","startOffset":0,"endOffset":0,"x":730,"y":260,"wires":[["1baeb26f.b85dbe"],[]]},{"id":"9405c3fe.d0a6c","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}]

that would be a great idea too, better than mine I believe.

That works great. I didnt know you could use the “state” as the wait condition. Still lots to learn :slight_smile: