Ending up with the same message i began with

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.

var enabled = msg.payload.enable;
var setpoint = msg.payload.setpoint;
var actual = msg.payload.actual;
var boiler_auto = msg.payload.boiler_auto;

if(enabled == true){
    if(boiler_auto == true){
        if(setpoint > actual){
            msg.payload = "start";
        }
        else if(actual > setpoint){
            msg.payload = "stop";
        }
    }
    else if(boiler_auto == false){
        msg.payload = "stop";
    }

}
else{msg.payload = "stop";}

return msg;

And the message;

{"enable":true,"boiler_auto":true,"setpoint":25,"actual":16}

I have also tried creating a new message as per Writing Functions : Node-RED but can’t get that to work either. The message ends up as undefined.

Isn’t it typical. Minutes after posting i did something i don’t know what and it works.

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"}

As Spock would say, all very logical really.

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!