# Simple If statement doesn´t work

**URL:** <https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937>\
**Category:** General\
**Created:** [23 December 2020 18:03 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937 "2020-12-23T18:03:25Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![oma](https://avatars.discourse-cdn.com/v4/letter/o/977dab/32.png) [@oma](https://discourse.nodered.org/u/oma)\
**Post date:** [23 December 2020 18:03 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/1 "2020-12-23T18:03:26Z")

</div>

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:

```auto
var watt = msg.payload.value;

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

```

can someone shine a light for me?

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [23 December 2020 18:16 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/2 "2020-12-23T18:16:49Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 December 2020 22:04 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/3 "2020-12-23T22:04:43Z")

</div>

> [@oma](#):
>
> `var watt = msg.payload.value;`

My take would be:

`var watt = parseInt(msg.payload);`

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [23 December 2020 22:36 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/4 "2020-12-23T22:36:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 December 2020 22:47 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/5 "2020-12-23T22:47:07Z")

</div>

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.

> [@dceejay](#):
>
> original message already showed that msg.payload.value was a float not an int.

Is that because of the `41.1` mentioned?

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [24 December 2020 05:01 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/6 "2020-12-24T05:01:10Z")

</div>

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?

```auto
msg.payload = {
     value : '41.1'
}

```

or is it

```auto
msg.payload = {
     value : 41.1
}

```

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

---

<div class="post-metadata">

**Author:** ![oma](https://avatars.discourse-cdn.com/v4/letter/o/977dab/32.png) [@oma](https://discourse.nodered.org/u/oma)\
**Post date:** [24 December 2020 11:18 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/7 "2020-12-24T11:18:24Z")

</div>

thanks for getting back. The payload is really 41.08:

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

```

which is why

```auto
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...

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 December 2020 11:55 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/8 "2020-12-24T11:55:31Z")

</div>

> [@oma](#):
>
> How does the else statement needs to change to simply send nothing

return null;

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 December 2020 11:56 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/9 "2020-12-24T11:56:26Z")

</div>

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.

> [@oma](#):
>
> How does the else statement needs to change to simply send nothing?

```auto
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.

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [24 December 2020 12:21 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/10 "2020-12-24T12:21:15Z")

</div>

sorry just woke up Ignore previous post.

---

<div class="post-metadata">

**Author:** ![Dirk\_L](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@Dirk\_L](https://discourse.nodered.org/u/Dirk_L)\
**Post date:** [24 December 2020 13:10 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/11 "2020-12-24T13:10:11Z")

</div>

You can also use this:

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

This would just return only something if needed.

---

<div class="post-metadata">

**Author:** ![Eckehard\_070](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eckehard_070/32/53691_2.png) [@Eckehard\_070](https://discourse.nodered.org/u/Eckehard_070)\
**Post date:** [26 December 2020 11:55 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/12 "2020-12-26T11:55:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BlackCatPeanut](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/blackcatpeanut/32/35132_2.png) [@BlackCatPeanut](https://discourse.nodered.org/u/BlackCatPeanut)\
**Post date:** [11 January 2021 19:44 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/13 "2021-01-11T19:44:00Z")

</div>

> [@Dirk\_L](#):
>
> {  
> msg.payload = true;  
> return msg  
> }

I'm trying to do something very similar. Would you be able to explain why I'm still seeing a reference error:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/eab0870447c1b8cef95cbacad4cfc9a55f5c684b.png)  
Inbound payload:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/fe94b3193f0986d5d2934ce98562ac6b9cb34377.png)  
Current function:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/e/ae35a2ed9380dc72c30421e8b21b065d888ce1d4.png)  
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...

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [11 January 2021 19:57 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/14 "2021-01-11T19:57:24Z")

</div>

In a function node, the input object is named `msg`

So you would use `msg.payload.value`

---

<div class="post-metadata">

**Author:** ![BlackCatPeanut](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/blackcatpeanut/32/35132_2.png) [@BlackCatPeanut](https://discourse.nodered.org/u/BlackCatPeanut)\
**Post date:** [11 January 2021 20:05 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/15 "2021-01-11T20:05:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 March 2021 20:05 UTC](https://discourse.nodered.org/t/simple-if-statement-doesn-t-work/37937/16 "2021-03-12T20:05:46Z")

</div>

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