# Function node to keep same topic

**URL:** <https://discourse.nodered.org/t/function-node-to-keep-same-topic/28088>\
**Category:** General\
**Created:** [9 June 2020 23:16 UTC](https://discourse.nodered.org/t/function-node-to-keep-same-topic/28088 "2020-06-09T23:16:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giaitzoglou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/giaitzoglou/32/5947_2.png) [@Giaitzoglou](https://discourse.nodered.org/u/Giaitzoglou)\
**Post date:** [9 June 2020 23:16 UTC](https://discourse.nodered.org/t/function-node-to-keep-same-topic/28088/1 "2020-06-09T23:16:00Z")

</div>

hi so i want to do something relly basic change a string message that comes from mqtt from 0 to 1 and from 1 to 0 i tried to do it with a change node but i put both options inside but it only does one and not both

 ![2020-06-10 (2)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/dab951fe55c2d204d086794e55d7d251d7bc3575.png)  
so itried the funktion node and it works perfectly but i lose the topic which is really important ,ho do i keep the same topic?

```auto
if (msg.payload === "1") {
   msg = { payload:"0" };
} else {
   msg = { payload:"1" };
}
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:** [9 June 2020 23:18 UTC](https://discourse.nodered.org/t/function-node-to-keep-same-topic/28088/2 "2020-06-09T23:18:20Z")

</div>

In your code you are creating a whole new message and returning it.

It's a better habit to reuse the `msg` object you've been given - as that then preserves the other properties:

```auto
if (msg.payload === "1") {
   msg.payload = "0";
} else {
   msg.payload = "1";
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![Giaitzoglou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/giaitzoglou/32/5947_2.png) [@Giaitzoglou](https://discourse.nodered.org/u/Giaitzoglou)\
**Post date:** [9 June 2020 23:20 UTC](https://discourse.nodered.org/t/function-node-to-keep-same-topic/28088/3 "2020-06-09T23:20:03Z")

</div>

hi thanks for your fast response cause i am new with java script and learning now can show me what you mean ?  
thanks  
chris

---

<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:** [9 June 2020 23:22 UTC](https://discourse.nodered.org/t/function-node-to-keep-same-topic/28088/4 "2020-06-09T23:22:13Z")

</div>

Hi @Giaitzoglou

I provided exactly the code you could use in my reply.

Where you were doing:

```auto
msg = { payload:"0" };

```

I changed it to:

```auto
msg.payload = "0";

```

In your code, you were assigning `msg` to a new object - so it lost all of the other properties on the message.  
In my code, we keep the existing `msg` object and just update its `payload`.

---

<div class="post-metadata">

**Author:** ![Giaitzoglou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/giaitzoglou/32/5947_2.png) [@Giaitzoglou](https://discourse.nodered.org/u/Giaitzoglou)\
**Post date:** [9 June 2020 23:25 UTC](https://discourse.nodered.org/t/function-node-to-keep-same-topic/28088/5 "2020-06-09T23:25:13Z")

</div>

ohhh sorry i unsderstand now can i ask you why it didnt work in the change node ?

---

<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:** [9 June 2020 23:27 UTC](https://discourse.nodered.org/t/function-node-to-keep-same-topic/28088/6 "2020-06-09T23:27:47Z")

</div>

> [@Giaitzoglou](#):
>
> why it didnt work in the change node ?

The Change node applies each rule you set in the order you list them.

This means:

- if `payload` is `1`, the first rule does nothing, the second rule changes it to a `0`. That is good.
- if `payload` is `0`, the first rule will change it to a `1`. The second rule then finds that `1` and changes it back to a `0`. That is bad.

---

<div class="post-metadata">

**Author:** ![Giaitzoglou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/giaitzoglou/32/5947_2.png) [@Giaitzoglou](https://discourse.nodered.org/u/Giaitzoglou)\
**Post date:** [9 June 2020 23:40 UTC](https://discourse.nodered.org/t/function-node-to-keep-same-topic/28088/7 "2020-06-09T23:40:10Z")

</div>

> [@knolleary](#):
>
> ```auto
> if (msg.payload === "1") {
> msg.payload = "0";
> } else {
> msg.payload = "1";
> }
> return msg;
> 
> ```

thanks a lot for your help

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [9 June 2020 23:46 UTC](https://discourse.nodered.org/t/function-node-to-keep-same-topic/28088/8 "2020-06-09T23:46:26Z")

</div>

Try using a intermediary value in the change node;

![change](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f62b95db4182b908ab6b905365b78c547960479a.jpeg)

![example](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/c/4c2bb96731acabe59f6e1527e44a1e2e2491b6a5.jpeg)

```auto
[{"id":"3c3eae5.ea3d352","type":"change","z":"c53060.842a0fa","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"str","to":"3","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"str","to":"0","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"3","fromt":"str","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":1120,"wires":[["f6be0d0a.725f2"]]},{"id":"9fd5b2ee.1fa62","type":"inject","z":"c53060.842a0fa","name":"","topic":"","payload":"0","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":1090,"wires":[["3c3eae5.ea3d352"]]},{"id":"f6be0d0a.725f2","type":"debug","z":"c53060.842a0fa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":1120,"wires":[]},{"id":"977b5120.e3654","type":"inject","z":"c53060.842a0fa","name":"","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":1140,"wires":[["3c3eae5.ea3d352"]]}]

```

---

<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:** [10 June 2020 08:16 UTC](https://discourse.nodered.org/t/function-node-to-keep-same-topic/28088/9 "2020-06-10T08:16:05Z")

</div>

This is one of the few cases where I would use JSONata

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/d/8d784bd4c1d8fb22f45edab8b655e91bcfef445c.png)

---

<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:** [24 June 2020 08:16 UTC](https://discourse.nodered.org/t/function-node-to-keep-same-topic/28088/10 "2020-06-24T08:16:18Z")

</div>

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