If Statement, so close to getting it right

Hey All,
I've been trying for the better part of a day and I can't get an if statement to work.
I've got serial data coming in from an Arduino to a Raspberry Pi running Mosquitto and Node-Red.

I'm trying to read the message, do a comparison and then send a new variable to the next node. The following code doesn't pass anything, not even null.

What am I doing wrong?
Thanks!
Rich

var newMsg = {payload: msg.payload, topic: msg.topic};
if (newMsg.payload == "Apt ON") { 
    payload: "ON"; 
    topic: ""; 
    }
else if (newMsg.payload == "Apt OFF") { 
    payload: "OFF";
     topic: ""; 
     }
else{ newMsg = null; }

return newMsg;```

Try this - I removed topic as you are not using it

var newMsg = { payload: msg.payload};

if (newMsg.payload == "Apt ON") {
    newMsg.payload = "ON";
}
else if (newMsg.payload == "Apt OFF") {
    newMsg.payload = "OFF";
}
else { newMsg = null; }

return newMsg;

Thanks!
I tried it and got the same results, nothing.

I made one change to the else line from

else{newMsg = null;}

to

else { newMsg.payload = null; }

The null gets passed this way.

Here's the entire code as it is now

var newMsg = { payload: msg.payload };

if (newMsg.payload == "Apt ON") {
    newMsg.payload = "ON";
}
else if (newMsg.payload == "Apt OFF") {
    newMsg.payload = "OFF";
}
else { newMsg.payload = null; }

return newMsg;

var newMsg = { payload: msg.payload };

This creates a clone, which: if it is on, overwrites itself and it will never validate to off.

Keep it simple.

2 examples:

Flow:

[{"id":"07ac74630fad9c64","type":"switch","z":"0519acea67608d4c","name":"APT ON or OFF ?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"APT ON","vt":"str"},{"t":"eq","v":"APT OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":410,"y":180,"wires":[["ce5cf6b363b23bf3"],["5b2a6844f0bf8afd"]]},{"id":"ce5cf6b363b23bf3","type":"change","z":"0519acea67608d4c","name":"ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":160,"wires":[["aa0e0cc1d32e5a18"]]},{"id":"5b2a6844f0bf8afd","type":"change","z":"0519acea67608d4c","name":"OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":200,"wires":[["aa0e0cc1d32e5a18"]]},{"id":"87827d3783337e27","type":"inject","z":"0519acea67608d4c","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"APT ON","payloadType":"str","x":180,"y":160,"wires":[["07ac74630fad9c64"]]},{"id":"a63f10c80a1baf23","type":"inject","z":"0519acea67608d4c","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"APT OFF","payloadType":"str","x":180,"y":200,"wires":[["07ac74630fad9c64"]]},{"id":"aa0e0cc1d32e5a18","type":"debug","z":"0519acea67608d4c","name":"debug 136","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":180,"wires":[]},{"id":"2d12e5c9455f2ec0","type":"change","z":"0519acea67608d4c","name":"Set ON/OFF ","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload = 'APT ON' ? 'ON' : 'OFF'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":300,"wires":[["ec24c956158e15be"]]},{"id":"ec24c956158e15be","type":"debug","z":"0519acea67608d4c","name":"debug 137","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":300,"wires":[]},{"id":"2d9c619efcc1174f","type":"inject","z":"0519acea67608d4c","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"APT ON","payloadType":"str","x":180,"y":280,"wires":[["2d12e5c9455f2ec0"]]},{"id":"7e28c772d7b878f2","type":"inject","z":"0519acea67608d4c","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"APT OFF","payloadType":"str","x":180,"y":320,"wires":[["2d12e5c9455f2ec0"]]}]

Note the checks are case-sensitive.

Tested and work fine for me ?

[{"id":"4a7d3131e8776f24","type":"inject","z":"2d30dc05e3a1d040","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Apt ON","payloadType":"str","x":695,"y":780,"wires":[["2ec02d6a0c11374e"]]},{"id":"2ec02d6a0c11374e","type":"function","z":"2d30dc05e3a1d040","name":"function 16","func":"var newMsg = { payload: msg.payload };\n\nif (newMsg.payload == \"Apt ON\") {\n    newMsg.payload = \"ON\";\n}\nelse if (newMsg.payload == \"Apt OFF\") {\n    newMsg.payload = \"OFF\";\n}\nelse { newMsg = null; }\n\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":895,"y":825,"wires":[["f93f425343c3eab6"]]},{"id":"f93f425343c3eab6","type":"debug","z":"2d30dc05e3a1d040","name":"debug 77","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1095,"y":825,"wires":[]},{"id":"a7e8a0b86907dca0","type":"inject","z":"2d30dc05e3a1d040","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Apt OFF","payloadType":"str","x":705,"y":825,"wires":[["2ec02d6a0c11374e"]]},{"id":"4decedb087d6e99c","type":"inject","z":"2d30dc05e3a1d040","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Other msg","payloadType":"str","x":705,"y":870,"wires":[["2ec02d6a0c11374e"]]}]
26/02/2023, 16:30:58node: debug 77
msg.payload : string[2]
"ON"
26/02/2023, 16:30:59node: debug 77
msg.payload : string[3]
"OFF"

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