I have a programming question, which way is the more efficient way to code. I have an alert when a temperature reaches a certain value and only alerts me when a boolean to turn "On" for the monitoring.
Should I start with an "Events : States" that runs anytime the temperature is updated, then the next step to continue is if the boolean is True? Or is there a way to only have the flow activated when the boolean is True? So it's not firing every time the temperature changes at night when I don't care.
I found the "Poll State" that could check if the monitor boolean is True then check the temperature, but I don't want it to run every xxx seconds. I'd rather only check when the temperature changes.
Hi, can't really answer as you've not given any info on what is triggering things, what nodes you are using for the monitor, etc.
You could use a node-red-contrib-simple-gate to block the temperature message when the boolean is false. Then if the message gets through the gate you know that the boolean is true so can then go on and test the temperature. In fact that is probably how I would do it.
I left it vague as it was more of a best practice with node-red then a specific example.
Basically, how do you monitor an entity state change, only when a boolean is true. (If / And statement) Or does it have to be in two steps, either monitor the boolean first, then the entity change (or vice versa).
I looked at the simple-gate, and does the same thing as the Traffic Light that I also like to use.
The alternative is to use a Join node to join the boolean and the value into one message, then you can test them both in one function. Simpler to use a gate node though.
This seems to be written from a home assistant perspective (?)
For variables that need to be checked before continuing, I use global context variables with a switch node.
I have a lux meter which produces a lux value, which i write to a global variable (every 5 minutes).
Now I have a PIR sensor that should trigger the lights when someone was detected, but only if lux < x value and between certain times.
So first check with a switch node is: if global lux variable < x value, continue (output 1), else nothing (output 2).
Personally, I avoid global and flow variables unless the alternative is complex. Globals make the flow less clear as you effectively have invisible wires joining nodes. So I still prefer the gate node. It is a matter of personal choice though.