# If condition with a function

**URL:** <https://discourse.nodered.org/t/if-condition-with-a-function/25389>\
**Category:** General\
**Created:** [24 April 2020 00:14 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389 "2020-04-24T00:14:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [24 April 2020 00:14 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/1 "2020-04-24T00:14:22Z")

</div>

Hi all node red maniacs!!

I'm working on a simple function condition. I'm a javascript beginner so please patience jejeje.

I have to make several if conditions so far i have.

```auto
let night = msg.payload
let day = msg.payload
let home = msg.payload
let away = msg.payload

if (night === true) {
    msg.payload = 'noche'
} else {
    msg.payload = 'dia'
}

return msg;

```

That works ok and i can retrieve the correct payload depending the day or night.

This is the output:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/0484d3deea4b808ac7daf8e3549749c40d2992e8.png)

NOW, i have to also make the condition for the others two variables (home and away) but no luck with that because the conditions make a little mess.

How can i achieve that to perform correctly?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [24 April 2020 05:35 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/2 "2020-04-24T05:35:11Z")

</div>

Hello,  
There is two problems here.  
Firstly you are assigning your message payload value to all variables. So var night and day and so on will all have the same value. That’s the first thing as it doesn’t make sense to have four variables that have exactly the same value.  
Secondly by doing `if(somevar === true)` you are only checking if `somevar` is a boolean type with a value of `true`. So you can only ever have two states true or false but you want to check for four different states.  
So you need to put something like a string or number into the function that can have more states and than you can compare the value of msg.payload directly.  
For example for a string something like: `if(msg.payload = "afternoon")` and than use if, else if etc.  
An alternative would be to have a look at switch cases which are made for exactly this purpose:

> **[switch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch)**
>
> The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case, as well as statements in cases that follow the matching case.

  
They compare one input value to a bunch of other values.  
Johannes

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [24 April 2020 16:18 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/3 "2020-04-24T16:18:55Z")

</div>

Ok, let me see if i get it.

First i do something like:

`let night, day, home, away = msg.payload` is that ok ?

Any example on how to manage the conditions?

I also will check the switch. Maybe i can do that with a node directly?.

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [24 April 2020 16:29 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/4 "2020-04-24T16:29:46Z")

</div>

What is in the msg.payload you put in the function? Can you please tell us?  
Why am i asking? If the msg.payload you receive just includes one piece of information there is no point in assigning it to for different variables. The only thing you achieve is that you now have the same piece of information 4 times. So testing any of the for vars would do exactly the same so there is no point.

Edit and yes if you just want to test the msg.payload for a certain content mostly the switch node is the easier option.

Edit 2 that syntax wont work

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [24 April 2020 19:11 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/5 "2020-04-24T19:11:20Z")

</div>

The function is declared as is above with the declared variables and so on.  
Also there is the payload info that i receive and that condition is well delivered.

Maybe i don't get it something.

Thanks !

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [24 April 2020 19:15 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/6 "2020-04-24T19:15:12Z")

</div>

You receive `msg.payload`.  
Check conditions on the content of the payload.

Like @JGKK stated:

```auto
if(msg.payload == "afternoon"){

}

```

What is your input ?

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [24 April 2020 19:18 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/7 "2020-04-24T19:18:05Z")

</div>

Hi bakman2!.

This is my output, as the image that i posted above.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/0484d3deea4b808ac7daf8e3549749c40d2992e8.png)

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [24 April 2020 19:18 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/8 "2020-04-24T19:18:55Z")

</div>

what do you put into the function not what comes out

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [24 April 2020 19:21 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/9 "2020-04-24T19:21:27Z")

</div>

Sorry maybe i dont get it correctly.

At the beginning of the post there is my whole function and the output. That works ok. The complex part that im doing is use more conditions inside the function.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [24 April 2020 19:31 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/10 "2020-04-24T19:31:00Z")

</div>

I wrote in the first answer if than else if, so:

```auto
if(){
    some code;
} else if (){
    some code;
}else if (){
    some code;
} else {
    some code;
}

```

or a switch case.  
But you don’t need this part:

> [@Marty1982](#):
>
> let night = msg.payload let day = msg.payload let home = msg.payload let away = msg.payload

as this means that day, night, home and away will all be a copy of msg.payload. So there all the same thing.  
You can just use `msg.payload === true` or `msg.payload === false` directly in an `if` or `else if` statement. As i also wrote above.  
Why do you want 4 variables that all have the same value?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [24 April 2020 19:55 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/11 "2020-04-24T19:55:59Z")

</div>

> [@Marty1982](#):
>
> At the beginning of the post there is my whole function and the output.

Yes, but what are you feeding **into** the function node?  
Instead of a function node, add a debug node, and show us what data you are working with.

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [24 April 2020 20:42 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/12 "2020-04-24T20:42:43Z")

</div>

This is my node.

[{"id":"fbac250c.314aa8","type":"nighttime","z":"3c9968a7.da8038","name":"Day/night","lon":"-58.381557","lat":"-34.603683","start":"sunrise","end":"sunsetStart","x":120,"y":625,"wires":[["b5963aeb.561fe8"],["b5963aeb.561fe8"]]},{"id":"b5963aeb.561fe8","type":"function","z":"3c9968a7.da8038","name":"day/night & home/away","func":"let night = msg.payload\nlet day = msg.payload\nlet home = msg.payload\nlet away = msg.payload\n\nif (night === true) {\n msg.payload = 'noche'\n} else {\n msg.payload = 'dia'\n}\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":450,"y":660,"wires":[["4d84070d.1ed408"]]},{"id":"4d84070d.1ed408","type":"debug","z":"3c9968a7.da8038","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":570,"y":760,"wires":}]

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [24 April 2020 20:44 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/13 "2020-04-24T20:44:55Z")

</div>

> [@Paul-Reed](#):
>
> Yes, but what are you feeding **into** the function node?  
> Instead of a function node, add a debug node, and show us what data you are working with.

Marty, you are not answering our questions!  
Both JGKK and myself have asked you the same question....

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [24 April 2020 20:48 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/14 "2020-04-24T20:48:20Z")

</div>

Im feeding the day or night node into the function node.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [24 April 2020 20:54 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/15 "2020-04-24T20:54:39Z")

</div>

I think you might have a basic miss understanding of concepts.  
Lets do this step by step. Are `night`, `day` etc the values you receive?  
Please attach a debug node before! the function node and show us a picture of an msg that you see in the debug tab that is send to the function node.  
This will help everybody trying to help you as its easier if we can understand the exact structure of the msg.payload that the function node handles.  
Unfortunately i cant import your code as im on my phone right now.

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [24 April 2020 21:20 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/16 "2020-04-24T21:20:23Z")

</div>

This is send before the function.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/148f8b3704c92c99af01b46721742b80a07c6489.png)

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [24 April 2020 21:30 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/17 "2020-04-24T21:30:56Z")

</div>

So just to make clear that I understand this correctly. You want to write a function that combines a check for two things:  
1st is it day or night?  
2nd is somebody home or not?  
Both informations come from different sources.  
If I understood it right my next question is do you want to save this state somewhere or do you want to root other messages base on the properties above and is home/away also a boolean?

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [24 April 2020 21:40 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/18 "2020-04-24T21:40:01Z")

</div>

Right, the thing is like this.

First I want to check if is day or night. That will have an output true or false because is boolean definded by the node by default. So, i "transcribed" that for day or night depending the boolean output. The function node (as described in top of the topic) do that correctly.

Second, i want to check if im home or not and depending on that the lights will turn on or off. It's useless turn on the lights if it's daylight right?

Maybe i'm doing the thing more complex than it is.

Thanks again!

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [24 April 2020 21:50 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/19 "2020-04-24T21:50:55Z")

</div>

Did you read the nightime nodes description?  
It sets a global var `isNight` to a boolean. You can just use this in a simple switch node. So use two switch nodes in series behind your home/away output one to test the msg.payload if you just arrived or left, the second one to check if global.isNight is true.  
simple as that and no function needed.

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [24 April 2020 21:59 UTC](https://discourse.nodered.org/t/if-condition-with-a-function/25389/20 "2020-04-24T21:59:45Z")

</div>

Perfect! i was thinking in switches too but maybe i can do that with a function, i made the thing more complicated i think.

Thanks a lot!!

[Next page](https://discourse.nodered.org/t/if-condition-with-a-function/25389.md?page=2)
