If else if case statements

Hi I am trying to work out how to do a case in a function.

I am getting a number from a modbus device and i need to output a text message depending on what number is received. Below is what i currently have but get error that output is a string...

Blockquote
var payload = msg.payload.ChargeState;
if (payload == "0")
{
return "START";
}
if (payload == "1")
{
return "NIGHT_CHECK";
}
if (payload == "2")
{
return "DISCONNECT";
}
if (payload == "3")
{
return "NIGHT";
}
if (payload == "4")
{
return "FAULT";
}
if (payload == "5")
{
return "MPPT";
}
if (payload == "6")
{
return "ABSORPTION";
}
if (payload == "7")
{
return "FLOAT";
}
if (payload == "8")
{
return "EQUALIZE";
}
if (payload == "9")
{
return "SLAVE";
}
msg.payload = char(payload);
return msg;

As you can see if for example i get a 5 i want the output to say MPPT..

Any assistance would be muchly appreciated.

You can't return a string.

you could set msg.payload and return msg at the end or return a new object with payload set.

e.g...

var payload = msg.payload.ChargeState;
if (payload == "0") {
  msg.payload = "START";
} else if (payload == "1") {
  msg.payload = "NIGHT_CHECK";
} else  if (payload == "2") {
  msg.payload = "DISCONNECT";
} else if (payload == "3") {
  msg.payload = "NIGHT";
} else if (payload == "4") {
  msg.payload = "FAULT";
} else if (payload == "5") {
  msg.payload = "MPPT";
} else if (payload == "6") {
  msg.payload = "ABSORPTION";
} else if (payload == "7") {
  msg.payload = "FLOAT";
} else if (payload == "8") {
  msg.payload = "EQUALIZE";
} else if (payload == "9") {
  msg.payload = "SLAVE";
} else {
  msg.payload = char(payload);
}
return msg;

HOWEVER, I would recommend a switch statement instead.

1 Like

So if i understand you i mean something like

Blockquote

if (payload == 5)
{
payload = "MPPT"
}

return msg;

Cheers thanks heaps for that, still trying to to get my head round node red programming. Out of interest how would i do it with a switch statement, or can you point me to where i can learn about it..

Thanks heaps mate

If you want to be a bit more ...... (something)

This could work, but it is a bit expensive on nodes used:

[{"id":"72ae34f0.65d9d4","type":"function","z":"88de1ac2.8b8c38","name":"input","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":170,"y":1120,"wires":[["c63e0b7c.ec595"]]},{"id":"c63e0b7c.ec595","type":"switch","z":"88de1ac2.8b8c38","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"},{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"2","vt":"str"},{"t":"eq","v":"3","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":320,"y":1120,"wires":[["4cae4b0d.a33c2c"],["9e8080df.540138"],["45ea1d3.9a016e4"],["f51a055f.3f8f3"]]},{"id":"4cae4b0d.a33c2c","type":"change","z":"88de1ac2.8b8c38","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":1060,"wires":[["daf9c9fd.2fd488"]]},{"id":"9e8080df.540138","type":"change","z":"88de1ac2.8b8c38","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":1090,"wires":[["daf9c9fd.2fd488"]]},{"id":"45ea1d3.9a016e4","type":"change","z":"88de1ac2.8b8c38","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":1120,"wires":[["daf9c9fd.2fd488"]]},{"id":"f51a055f.3f8f3","type":"change","z":"88de1ac2.8b8c38","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":1150,"wires":[["daf9c9fd.2fd488"]]},{"id":"daf9c9fd.2fd488","type":"function","z":"88de1ac2.8b8c38","name":"output","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":720,"y":1110,"wires":[[]]}]

Just to say.

ahh ok sweet, thanks... food for thought

It is just that way it (maybe) helps you see what is going on, but @Steve-Mcl way is neater and more compact.

Some more food
Another method

let values = {"0":"START",
            "1": "NIGHT_CHECK",
            "2": "DISCONNECT",
            "3": "NIGHT",
            "4": "FAULT", 
            "5": "MPPT",
            "6": "ABSORPTION",
            "7": "FLOAT",
            "8": "EQUALIZE",
            "9": "SLAVE"}
let chargeState = msg.payload.ChargeState.toString();
if ( Object.keys(values).includes(chargeState)){
msg.payload = values[chargeState];
}
else{
  msg.payload = msg.payload.ChargeState;
}
return msg;
1 Like

I think that may help me with a problem I have on another topic.

Thanks!