# Function node not working

**URL:** <https://discourse.nodered.org/t/function-node-not-working/41891>\
**Category:** General\
**Created:** [2 March 2021 11:43 UTC](https://discourse.nodered.org/t/function-node-not-working/41891 "2021-03-02T11:43:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sensors](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Sensors](https://discourse.nodered.org/u/Sensors)\
**Post date:** [2 March 2021 11:43 UTC](https://discourse.nodered.org/t/function-node-not-working/41891/1 "2021-03-02T11:43:23Z")

</div>

I have no idea what I'm doing wrong with this function node. It's probably something really silly, but can someone help?

I'm just trying to alter a msg.payload with a simple if statement, but no matter the input the output is aways "close".

```auto
msg = {};
msg.topic = "control";

if(msg.payload == "0"){
    msg.payload = "open";
}else{
    msg.payload = "close";
}

return msg;

```

```auto
[{"id":"efa8eb67.7449f8","type":"debug","z":"4ffa72da.fb000c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1730,"y":2500,"wires":[]},{"id":"e352c18c.d678","type":"function","z":"4ffa72da.fb000c","name":"","func":"msg = {};\nmsg.topic = \"control\";\n\n\nif(msg.payload == \"0\"){\n msg.payload = \"open\";\n}else{\n msg.payload = \"close\";\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1720,"y":2440,"wires":[["5eaf9670.9d9128"]]},{"id":"5eaf9670.9d9128","type":"debug","z":"4ffa72da.fb000c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1890,"y":2440,"wires":[]},{"id":"40d2d495.c7a8fc","type":"inject","z":"4ffa72da.fb000c","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":1510,"y":2460,"wires":[["e352c18c.d678","efa8eb67.7449f8"]]},{"id":"94f610c2.6fba1","type":"inject","z":"4ffa72da.fb000c","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":1510,"y":2420,"wires":[["e352c18c.d678","efa8eb67.7449f8"]]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 March 2021 11:46 UTC](https://discourse.nodered.org/t/function-node-not-working/41891/2 "2021-03-02T11:46:11Z")

</div>

> [@Sensors](#):
>
> I'm just trying to alter a msg.payload with a simple if statement, but no matter the input the output is aways "close".

`msg = {};` \<\< this crates a brand new empty msg & `msg.payload` will ALWAYS be empty (undefined)

what happens if you delete that first line?

```auto
//msg = {};
msg.topic = "control";

if(msg.payload == "0"){
    msg.payload = "open";
}else{
    msg.payload = "close";
}

return msg;

```

---

<div class="post-metadata">

**Author:** ![Sensors](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Sensors](https://discourse.nodered.org/u/Sensors)\
**Post date:** [2 March 2021 11:49 UTC](https://discourse.nodered.org/t/function-node-not-working/41891/3 "2021-03-02T11:49:09Z")

</div>

Ahhh of course. Thanks very much Steve, I knew it was something silly!

I was trying to clear out the msg because it had got a little messy before that node. I've rearranged now:

```auto
var result;

if(msg.payload == "0"){
    result = "open";
}else{
    result = "close";
}

msg = {};
msg.topic = "control";
msg.payload = result;

return msg;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 March 2021 11:53 UTC](https://discourse.nodered.org/t/function-node-not-working/41891/4 "2021-03-02T11:53:29Z")

</div>

or...

```auto
return {
  topic: "control",
  payload: msg.payload == "0" ? "open" : "close";
};

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 March 2021 11:54 UTC](https://discourse.nodered.org/t/function-node-not-working/41891/5 "2021-03-16T11:54:22Z")

</div>

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