# If Else Function

**URL:** <https://discourse.nodered.org/t/if-else-function/64702>\
**Category:** General\
**Created:** [5 July 2022 03:27 UTC](https://discourse.nodered.org/t/if-else-function/64702 "2022-07-05T03:27:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![riimeister](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@riimeister](https://discourse.nodered.org/u/riimeister)\
**Post date:** [5 July 2022 03:27 UTC](https://discourse.nodered.org/t/if-else-function/64702/1 "2022-07-05T03:27:52Z")

</div>

I'm currently trying to create an if else function  
but there is a problem when I create the function where there is an error.

this is the current flow i made, i want to make flow  
if msg.payload = 0 he is red  
if msg.payload = 1 he is yellow  
if msg.payload \>= 2 he is green

then when you want to display it on the dashboard template it becomes blank

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/8/c86071fd6147c69aca807d9cd57f9edb34f52f0b.png)

this flow function

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/7/57921d2b46cd6893338ff0f20cddb966aada0333.png)

this flow template

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8a6a244464c12be6af720f0851d4930402ab3d3e.png)

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [5 July 2022 04:44 UTC](https://discourse.nodered.org/t/if-else-function/64702/2 "2022-07-05T04:44:32Z")

</div>

First, you really aren't doing if else, it's three if statements. Just saying. What is the msg.payload being fed into the function?  
[Edit] I have a hunch I know your problem but I need to make sure.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [5 July 2022 05:17 UTC](https://discourse.nodered.org/t/if-else-function/64702/3 "2022-07-05T05:17:43Z")

</div>

And i would also ask - why bother ?? Something really simple like this - why not just use a switch node with 4 outputs

Output 1 = 0  
Output 2 = 1  
Output 3 = 2  
Output 4 = anything else

Craig

---

<div class="post-metadata">

**Author:** ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)\
**Post date:** [5 July 2022 05:56 UTC](https://discourse.nodered.org/t/if-else-function/64702/4 "2022-07-05T05:56:21Z")

</div>

As @craigcurtin points out, the switch node was done specifically for cases like this. If you don't want to redirect the flow but just change it accordingly, use change nodes on each exit to make sure you get the values you want, then bring the flows back together.

Or, if you prefer to do it in a function node, use a Switch Case instead of If statements:

```auto
switch(expression)
{
  case a:
      //Statement or expression;
  break;
  case b:
      //Statement or expression;
  break;
  (...)
  default:
     //default statement or expression;
}

```

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [5 July 2022 06:05 UTC](https://discourse.nodered.org/t/if-else-function/64702/5 "2022-07-05T06:05:32Z")

</div>

Or as @gerry said, why not re-write the construct with 'if' and 'else if'  
The 'if' 'else' ladder will exit as soon as the first match is found.

```auto
var A = "";
var B = "";
var C = "";

if (msg.payload == 0) {
  A = msg.payload;
}
else if (msg.payload == 1) {
  B = msg.payload;
}
else {
  C = msg.payload;
}

msg.payload = {}; // Clear original payload so a payload object can be used

msg.payload.red = A;
msg.payload.yellow = B;
msg.payload.green = C;
return msg;

```

Here's a simple test flow.  
 ![if_else](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/48a1e0bda914003f75ad59901da5592615728bd4.png)

```auto
[{"id":"9faa33101efec6d3","type":"function","z":"b7043698e086d930","name":"","func":"var A = \"\";\nvar B = \"\";\nvar C = \"\";\n\nif (msg.payload == 0) {\n A = msg.payload;\n}\nelse if (msg.payload == 1) {\n B = msg.payload;\n}\nelse {\n C = msg.payload;\n}\n\n// msg.payload = {}; // Clear msg.payload so a new entity can be added\n\nmsg.payload.red = A;\nmsg.payload.yellow = B;\nmsg.payload.green = C;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":260,"wires":[["1788c466b24cad9c"]]},{"id":"c753c50c29a45883","type":"inject","z":"b7043698e086d930","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":250,"y":220,"wires":[["9faa33101efec6d3"]]},{"id":"1788c466b24cad9c","type":"debug","z":"b7043698e086d930","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":260,"wires":[]},{"id":"65d770e43b7c31c1","type":"inject","z":"b7043698e086d930","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":250,"y":260,"wires":[["9faa33101efec6d3"]]},{"id":"f29a1a5330c3d4a1","type":"inject","z":"b7043698e086d930","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":250,"y":300,"wires":[["9faa33101efec6d3"]]},{"id":"ffd03712faf0a3d8","type":"inject","z":"b7043698e086d930","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":250,"y":340,"wires":[["9faa33101efec6d3"]]}]

```

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [5 July 2022 06:17 UTC](https://discourse.nodered.org/t/if-else-function/64702/6 "2022-07-05T06:17:54Z")

</div>

However, I also think they are comparing a string to a number so we still need to see what the msg.payload is

---

<div class="post-metadata">

**Author:** ![riimeister](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@riimeister](https://discourse.nodered.org/u/riimeister)\
**Post date:** [5 July 2022 06:43 UTC](https://discourse.nodered.org/t/if-else-function/64702/7 "2022-07-05T06:43:58Z")

</div>

Thanks for helping me guys, so far problem solved.  
and i used this methode

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/0/50d152bfff7b991ddc4ca34e1e5194eb8d87c6b2.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/22b59edeb5d03d1919e0e31de33c7f511629250a.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d3f5a2bbca6603f19540fcb5593328b4894a07e7.png)

---

<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:** [5 July 2022 06:56 UTC](https://discourse.nodered.org/t/if-else-function/64702/8 "2022-07-05T06:56:21Z")

</div>

The real problem with your code was at first you compare payload as if it were a number and then you go on to append properties to payload as if it were an object.

In short, payload cannot be a number and an object at the same time.

```auto
if (msg.payload == 0) { //<-- number?

  ...
  // Snipped for brevity
  ...

msg.payload.red = A; //<- object?
msg.payload.yellow = B;
msg.payload.green = C;

```

If the incoming payload was a number then all you needed to do was add `msg.payload = {}` above `msg.payload.red = A`

---

<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:** [5 July 2022 07:29 UTC](https://discourse.nodered.org/t/if-else-function/64702/9 "2022-07-05T07:29:51Z")

</div>

> [@riimeister](#):
>
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d3f5a2bbca6603f19540fcb5593328b4894a07e7.png)
> 
> image1405×377 12.2 KB

One other thing, if you are using this in a UI-Template - remove the `html`, `head` and `body` elements as the ui-template actually inserts your HTML into an existing body.

If you want the `style` section in the `head` then use another ui-template said for head mode.

> [@riimeister](#):
>
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/22b59edeb5d03d1919e0e31de33c7f511629250a.png)

You might want to make the 1st line `if (msg.payload <= 0) {` to cater for negative values?

* * *

Lastly, please post code as test (between a code fence) as we cannot copy your code and make edits from a picture.

---

<div class="post-metadata">

**Author:** ![riimeister](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@riimeister](https://discourse.nodered.org/u/riimeister)\
**Post date:** [5 July 2022 07:37 UTC](https://discourse.nodered.org/t/if-else-function/64702/10 "2022-07-05T07:37:28Z")

</div>

for ui-template yess i making this function for ui-template dashboard  
I made a function first and then displayed it to the template, if I don't need html in the future, I will delete it, thanks for the information.

and for if else problem solved and now my program is running.  
just a simple look.  
if it displays value 0 = data loss / red flag  
if he displays value 1 = there is 1 data /minutes / yellow flag (warning)  
if he displays value 2 = there are 2 data / minutes / green flag (data is normal)

this may sound strange to you but this is my simple program, the point of which is in 1 minute how much data is read

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/c/bc3048e67707699128a92f7b8c2d9856c839255d.png)

---

<div class="post-metadata">

**Author:** ![riimeister](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@riimeister](https://discourse.nodered.org/u/riimeister)\
**Post date:** [5 July 2022 07:39 UTC](https://discourse.nodered.org/t/if-else-function/64702/11 "2022-07-05T07:39:16Z")

</div>

I made the mqtt program in 1 data / 30sec, which is if 1 minutes there are 2 data. so I made the program to find out if in 1 minute whether there is a data flow or issue. that's all really

---

<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:** [3 September 2022 07:40 UTC](https://discourse.nodered.org/t/if-else-function/64702/12 "2022-09-03T07:40:02Z")

</div>

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