Simple If statement doesn´t work

I am sure it´s an "boy, tt´s so damn easy" question but i can´t see it anyway. I am getting a msg.payload value of 41.1 but my function still returns false:

var watt = msg.payload.value;

if (watt > 20)
    {
        msg.payload = true;
    }
else
    {
        msg.payload = false;
    }
    
return msg

can someone shine a light for me?

are you sure that msg.payload.value is a number and not alphanumeric?
try this

var watt = Number(msg.payload.value);

if (watt > 20)
    {
        msg.payload = true;
    }
else
    {
        msg.payload = false;
    }
    
return msg

It would help is you could post (copy paste the value of) your msg.payload for use to see.

My take would be:

var watt = parseInt(msg.payload);

That won’t work as a) msg.payload is an object and b) the original message already showed that msg.payload.value was a float not an int.

Sorry..... I am still not with the language structure.

I believe you can append commands/qualifiers to things and get stuff to happen that way.

So I thought msg.payload.value (particularly when value is bold) was one of these qualifiers and so made msg.payload into a value rather than a string.

My bad.

Is that because of the 41.1 mentioned?

So I paid no attention to the bolding of .value

To me I saw a unknown msg.payload with a object key of value and the key having a value of 41.1

now how is this value stored?

msg.payload = {
     value : '41.1'
}

or is it

msg.payload = {
     value : 41.1
}

hmmmmm .... thats why I asked for a copy of the msg.payload output from @oma

thanks for getting back. The payload is really 41.08:

{"_msgid":"d61780d1.39404","payload":41.08,"topic":""}

which is why

var watt = parseInt(msg.payload);

if (watt > 20)
    {
        msg.payload = true;
    }
else
    {
        msg.payload = false;
    }
    
return msg

works at least as expected. Is this still not the correct way of parsing it despite the fact that it´s working fine?
And one last thing to this: I am sending boolean true if watt > 20. How does the else statement needs to change to simply send nothing? I only want to send something if it´s really above 20 and not send anything at all if its below 20...

return null;

1 Like

Since the payload is already a number, not a string, you don't need the `parseInt()'. In fact that may (but not sure) convert it to 41.0 since it tells it to make it an int.

else
    {
        msg = null;
    }

returning null means don't send any message.

[Edit] I prefer setting msg to null rather than immediately returning null as in a more complex function it is easy to forget later on in the function that you have already returned.

sorry just woke up Ignore previous post.

You can also use this:

if (watt > 20)
{
msg.payload = true;
return msg
}

This would just return only something if needed.

sloppy type conversion may be a source of trouble in JS. ("23"<21+1) is false, but (23<"21"+"1") is true, as JS makes a string addition "21"+"1" = "211". Usually the use of Number() is the best solution to ensure numerical values regardless of the source.

I'm trying to do something very similar. Would you be able to explain why I'm still seeing a reference error:
image
Inbound payload:
image
Current function:
image
What I'm trying to do is trigger a true boolean in order to incorporate an IF statement looking at the illuminance levels. I plan to continue a flow if the light levels are below a certain level. Thanks in advance...

In a function node, the input object is named msg

So you would use msg.payload.value

1 Like

Thanks @Steve-Mcl - I'm very new and working through some Essentials youtube'ing. Appreciate the quick reply!

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