If Else Function

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

this flow function

this flow template

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.

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

1 Like

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:

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

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.

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

[{"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"]]}]

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

1 Like

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



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.

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

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.

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.

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

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

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