# Need help with a simple function

**URL:** https://discourse.nodered.org/t/need-help-with-a-simple-function/3708
**Category:** General
**Created:** [5 October 2018 08:21 UTC](https://discourse.nodered.org/t/need-help-with-a-simple-function/3708 "2018-10-05T08:21:54Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![rednode](https://avatars.discourse-cdn.com/v4/letter/r/3d9bf3/32.png) [@rednode](https://discourse.nodered.org/u/rednode)
#### Post date: [5 October 2018 08:21 UTC](https://discourse.nodered.org/t/need-help-with-a-simple-function/3708/1 "2018-10-05T08:21:55Z")

</div>

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?

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

```

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [5 October 2018 08:28 UTC](https://discourse.nodered.org/t/need-help-with-a-simple-function/3708/2 "2018-10-05T08:28:30Z")

</div>

Hi @rednode,

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

```auto
   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.

---

<div class="post-metadata">

### Author: ![rednode](https://avatars.discourse-cdn.com/v4/letter/r/3d9bf3/32.png) [@rednode](https://discourse.nodered.org/u/rednode)
#### Post date: [5 October 2018 08:35 UTC](https://discourse.nodered.org/t/need-help-with-a-simple-function/3708/3 "2018-10-05T08:35:51Z")

</div>

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

```auto
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

```auto
if(msg.payload!==false){

```

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [5 October 2018 08:46 UTC](https://discourse.nodered.org/t/need-help-with-a-simple-function/3708/4 "2018-10-05T08:46:08Z")

</div>

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

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [5 October 2018 08:46 UTC](https://discourse.nodered.org/t/need-help-with-a-simple-function/3708/5 "2018-10-05T08:46:32Z")

</div>

See [this](https://discourse.nodered.org/t/and-confusion/3642/2?u=knolleary) 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:

```auto
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.

---

<div class="post-metadata">

### Author: ![rednode](https://avatars.discourse-cdn.com/v4/letter/r/3d9bf3/32.png) [@rednode](https://discourse.nodered.org/u/rednode)
#### Post date: [5 October 2018 08:55 UTC](https://discourse.nodered.org/t/need-help-with-a-simple-function/3708/6 "2018-10-05T08:55:48Z")

</div>

> [@knolleary](#):
>
> if(!msg.payload){ msg.payload="Online" } else { msg.payload="Offline" } return msg;

aah now i understand.  
thank you sir!

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [5 October 2018 09:19 UTC](https://discourse.nodered.org/t/need-help-with-a-simple-function/3708/7 "2018-10-05T09:19:25Z")

</div>

> [@knolleary](#):
>
> if(!msg.payload) {  
> msg.payload="Online"  
> }
> 
> ```auto
> 
> ```

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

```auto
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.
