Switch Node: Preventing Repeated Execution in Battery Monitoring

I'm extremely new to Node-RED. I'm monitoring the state of charge for a battery via MQTT. I created a switch node with three conditions: <10, between 10 and 15, and over 15. It works well except for one issue: it keeps executing the nodes repeatedly. I don't understand how to make it execute just one time. Let me explain: the battery goes over 15% and the switch node sends the payload to the correct branch. Fantastic, but... if the SoC goes to 15.01, the exit node >15 is executed again. The same happens for the other exit nodes. They are repeated over and over. I would like it to happen just one time and then wait for another change to execute the relevant exit node. Is there a node that handles this behavior, or do I need to write custom code with global variables?

Deunan

Welcome to the forums @Deunan

I feel your answer may lie in the filter node, this can reject messages unless a threshold is met, and maybe in combination with the delay node in rate limit mode in addition to your switch node

I don't use these nodes myself - but Im pretty sure this is where the goodies are, for you requirement.

1 Like

Hi and welcome.
Marcus is correct that you will need to use the filter node, but you will also need to set another property that does not change unless out of range. You would set this property to say "under", "between", "over" on each switch node output. You can the use this property in the filter node.
e.g.

[{"id":"08e1a0fcab885332","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":170,"y":5720,"wires":[["19eb5a433f0b061f"]]},{"id":"19eb5a433f0b061f","type":"switch","z":"d1395164b4eec73e","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"10","vt":"num"},{"t":"btwn","v":"10","vt":"num","v2":"15","v2t":"num"},{"t":"gt","v":"15","vt":"num"}],"checkall":"false","repair":false,"outputs":3,"x":330,"y":5760,"wires":[["3bb53f850ade45d5"],["24e6fd616832cced"],["123d1d804233c5a7"]]},{"id":"093a8924e5ba83cf","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"13","payloadType":"num","x":170,"y":5760,"wires":[["19eb5a433f0b061f"]]},{"id":"9d67f29126d4be80","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":170,"y":5800,"wires":[["19eb5a433f0b061f"]]},{"id":"3bb53f850ade45d5","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"range","pt":"msg","to":"under","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":5740,"wires":[["6cca02b4c971f831"]]},{"id":"24e6fd616832cced","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"range","pt":"msg","to":"between","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":5780,"wires":[["6cca02b4c971f831"]]},{"id":"123d1d804233c5a7","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"range","pt":"msg","to":"over","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":5820,"wires":[["6cca02b4c971f831"]]},{"id":"6cca02b4c971f831","type":"rbe","z":"d1395164b4eec73e","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"range","topi":"topic","x":650,"y":5780,"wires":[["523946f57c21b8ab"]]},{"id":"523946f57c21b8ab","type":"debug","z":"d1395164b4eec73e","name":"debug 2489","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":650,"y":5860,"wires":[]}]

How to import/export a flow

2 Likes

Thank you both.
This is exactly what I needed. If I understand it correctly, the change node adds a property called 'range' to the message and sets it according to the exit node. Then the filter node operates on the property and not on the payload of the message. It's very easy to modify if needed and is really compact. Thanks again!!!

1 Like

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