If && statement not working

Hi, I would like to use the if && function to give input to the switch function in case the two conditions are true.
I have a node that reads the temperature and one that reads if the thermostat is on “heat”. If this is correct, the function should pass the temperature value to the switch node.

I have tried this but it doesn’t work. Can anyone help me with this?

Thanks

var temp = {payload: msg.payload.temp};
var risc = {payload: msg.payload.risc};

if (temp < 20.0 && risc == "heat")
{
msg.payload = temp;
return msg;
}else{
    msg.payload = "no";
return msg;
}

I think var temp = and var risc = are wrong.
Shouldn’t they be:

var temp = msg.payload.temp;
var risc = msg.payload.risc;

I am doing something similar in my global brightness function node:

/*
var new_brightness = msg.payload;
if (new_brightness == 254) {new_brightness=255;}
*/
var new_brightness = (msg.payload == 254) ? 255 : msg.payload;
var old_brightness = global.get('nr_circadian_brightness');
if (old_brightness == new_brightness) {return null}
global.set('nr_circadian_brightness',new_brightness);
/* 
msg.payload = new_brightness;
return msg
*/
return {payload: new_brightness}

I would try this (no need for vars):

msg.payload = (msg.payload.temp < 20 && msg.payload.risc == "heat") msg.payload.temp ? "no";
return msg;

It gives me error:
Missing “;” before statement
but I don’t know how to fixable

I’ve try

msg.payload = (msg.payload.temp < 20 && msg.payload.risc == "heat");
msg.payload.temp;
return msg;

But result is “false” and is not correct. Actually temp is <20 and heat is on

Ah, my bad! try:

msg.payload = (msg.payload.temp < 20 && msg.payload.risc == "heat") ? msg.payload.temp : "no";
return msg
1 Like

Now it work but result is wrong. In debug msg.payload = no
but if temp is < 20 and term = heat i want msg.payload = temperature from msg.payload.temp

And if that works, I think those two lines could be combined into just one and eliminate the vars entirely:

return { "payload": (msg.payload.temp < 20 && msg.payload.risc == "heat") ? msg.payload.temp : "no"}

What is msg.payload.temp? Is it string or number??

number directly from temperature sensor

Add a debug node set to complete msg at the input to the function and copy/paste the result…

10/2/2022, 21:51:13node: 310f51c1f146c8ad
msg.payload : string[2]
"no"

That looks like the output from your function. I want to see the message being passed into your function…

10/2/2022, 21:58:54node: 310f51c1f146c8ad
msg.payload : number
0
10/2/2022, 21:58:54node: 310f51c1f146c8ad
msg.payload : string[4]
"18.3"

I’m confused. Why are there two instances??

I added this function node to my circadian flow which spits out a numeric payload.
image

[{"id":"b4667a51158ca22c","type":"function","z":"7ea0620.a243ca","name":"","func":"\nreturn { \"payload\": (msg.payload > 20 && msg.payload > 254) ? msg.payload : \"no\" }\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":360,"wires":[["b07d10fc7deef05c"]]}]

This is what I see:


The first output was with
return { "payload": (msg.payload > 20 && msg.payload > 25) ? msg.payload : "no" }
and the second with
return { "payload": (msg.payload > 20 && msg.payload > 254) ? msg.payload : "no" }
My payload currently is numeric 254.

this is mine:




How is wrong? Sorry but I’m very newbie and thank you for your patience

Okay I see now. You can’t just feed two separate msgs into function and expect it to know there are two in line. It only deals with one message at a time! You need to add a join node in front of the function…

1 Like

Done, error :expressionless:

Additionally, I see “18.3” which is a string.

Add these debug nodes:

[{"id":"ce0e50af0d4fa4ea","type":"debug","z":"7ea0620.a243ca","name":">>> PAYLOAD 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2050,"y":300,"wires":[]}]
[{"id":"392a863f4702ce1c","type":"debug","z":"7ea0620.a243ca","name":">>> PAYLOAD 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2050,"y":380,"wires":[]}]

and change your msg.payload node to “complete msg object” and add a name to it like “Function Output”

You need something like this:

Use manual mode…

this is the output

Okay, progress! but you have two payload objects with different values and join can’t deal with that.
Add two change nodes before the join:
image
but you want payload.temp on one and payload.risc on the other rather than my payload.brightness.
If that works then you can go back and try and figure out why your two current state nodes are not outputting the payload object the way you want so that you won’t need to use change node…