Need help with a simple function

#1

Hi,
as a nodered /json noob i need some assistance from the crowd.

i used the ping node to check if my devices are on or offline. the node sends ping results or if ping failes, a false result.
now i want to rewrite the msg.payload to Online or Offline. For that i write a function but i still got the ping times on the output. so something goes wrong here. can someone explain me ho to correct the code?

if(msg.payload!==false){
    msg.payload==="Online"
}
else
{
    msg.payload==="Offline"
}
return msg
#2

Hi @rednode,

The == and === operators are used to test the value. To assign a value, you use =:

   msg.payload = "Online"

By the way, when sharing code on the forum please try to format it properly. The easiest way to do it is to add three back-tick characters - ``` - on a new line before and after your code block.

#3

thanks for your help.
so ive changed my code to this

if(msg.payload!=="false"){
    msg.payload="Online"
}
else
{
    msg.payload="Offline"
}
return msg;

but now, there icomes no data on the output site
same with

if(msg.payload!==false){
#4

OK lets step back a step.

I assume you have a debug node attached to the output?
is false written in blue? Then its a boolean and you would need msg.payload!==false
or "false"? Then it is a string and you would need msg.payload!=="false"

Its worth understanding javascript variable types as they are fundamental in Node-RED

[{"id":"1b8567c6.f72d9","type":"inject","z":"8cb64888.462c2","name":"Boolean false","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":160,"wires":[["6946248b.0b7ee4","dcf0068a.ed8c58"]]},{"id":"6946248b.0b7ee4","type":"function","z":"8cb64888.462c2","name":"","func":"if(msg.payload!==\"false\"){\n msg.payload=\"Online\"\n}\nelse\n{\n msg.payload=\"Offline\"\n}\nreturn msg;","outputs":1,"noerr":0,"x":380,"y":160,"wires":[["698291d5.35be58"]]},{"id":"7686a59c.1258f4","type":"inject","z":"8cb64888.462c2","name":"String false","topic":"","payload":"false","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":220,"wires":[["6946248b.0b7ee4","dcf0068a.ed8c58"]]},{"id":"698291d5.35be58","type":"debug","z":"8cb64888.462c2","name":"OUTPUT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":570,"y":160,"wires":[]},{"id":"dcf0068a.ed8c58","type":"debug","z":"8cb64888.462c2","name":"INPUT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":350,"y":80,"wires":[]}]

#5

See this recent thread about the differences between == and ===.

It all depends on the precise value and type of msg.payload you are testing. If it is a proper boolean value (false) rather than a String ("false") then you will be able to do:

if(!msg.payload){
    msg.payload="Online"
}
else
{
    msg.payload="Offline"
}
return msg;

But it does all hinge on what the incoming msg.payload looks like - pass it to a Debug node and share the output.

#6

aah now i understand.
thank you sir!

#7

I think I have to nit-pick slightly with this, though in practice it will probably be ok. The issue is that the test will not only be satisfied if the payload is false, but also if it is 0. Now since the Ping node returns a number if the ping succeeds, or false if it fails then I think it would be better to use

if (msg.payload === false) {
    msg.payload="Offline"
}
else
{
    msg.payload="Online"
}

In practice the successful payload will probably never be 0, but the documentation of the node does not say that it cannot be zero so better not to rely on that.

[Edit]
I have amended my code above, as actually @knolleary had the test the wrong way round I think.