Trouble with javascript

Hi, trying to change msg.payload from true to on and false to off from the alexa node. Used a function node with the following but it doesnt work, can anyone tell me why.??

if (msg.payload == "true"){
msg.payload = "on";
}
else if (msg.payload == "false"){
msg.payload = "off";
}
return msg;

if its a typeof boolean true and false you need to do the comparison without the “”,so:

if (msg.payload == true) {

otherwise your looking for the literal strings of true and false.
You can see which it is when you connect a debug to the previous node and check in the debug tab for the format of this message property.
Johannes
Ps: and a happy forum anniversary :tada:

or

msg.payload = (msg.payload == true) ? "on" : "off";
2 Likes

or even

msg.payload = msg.payload ? "on" : "off"

You don't need to test msg.payload == true as it is itself true (or false).

That all depends on whether you wish ON when the payload is {}.

Problem solved. Thank you to everyone who helped, very much appreciated.

If you want to test for exactly true rather than just truthy then you should use ===. With == you will see a yellow warning triangle in the editor.

As i said it depends what you require.

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