So i’m trying to write a function node to turn my boiler off or on. For this i have a join node that combines an enable boolean, an auto mode boolean a setpoint number and the actual temperature number and combines them to a key/value object. I have done this previously with a less complex function in a similar fashion with success. But this time i’m ending up with the exact same message returned that i start out with.
I think you have a hole in your logic, and your function won’t always work as expected.
Say:
enabled - true
boiler_auto - true
setpoint == actual (I assume this is possible)
then setpoint > actual is false, and actual > setpoint is also false at the same time
msg.payload never gets set, and the function will return the exact same message you started with.
As everything is Boolean, there is no need for the conditional test. (enabled) is an expression that evaluates ‘enabled’ and returns true or false. Also, there is no need if the first conditional fails for a second conditional as the outcome of a Boolean is always True or not true. If (true) else going to be false.
The way I do these things is
start with the default answer which is ‘stop’. This ensures that I always get something, and I can concentrate on what I want to make it ‘start’.
var mp = "stop";
if (enabled) {
if (boiler_auto) {
if (actual < setpoint) {mp = "start"}
else {mp = "stop"}
}
else {mp = "stop" }
} else {mp = "stop" }
msg.payload = mp;
return msg;
With single line expressions in an ‘if - else’ it is possible to write the expression within {} on the same line, and also an ending ‘;’ is not required. This can help reduce the visual complexity.
Stepping back, I can now see that everything is “stop” unless
(enabled and boiler_auto and setpoint > actual), so I might be tempted to go with
msg.payload = "stop";
if (enabled & boiler_auto & actual < setpoint) {msg.payload = "start"}
Yes, setpoint == actual seems to be exactly what happened in an unfortunate turn of events. All your other points are very spot on. As a novice i usually start out writing very verbose, and then clean it up as i get things working. And i’m used to C, hence ;.
I’m going to add a heat pump later on which adds a few conditions. Haven’t decided if i’m including it in this flow or making another one, else i would also have done something like your last solution.
Thanks for the input!