# How to add msg.topic to function

**URL:** <https://discourse.nodered.org/t/how-to-add-msg-topic-to-function/48401>\
**Category:** General\
**Created:** [14 July 2021 11:11 UTC](https://discourse.nodered.org/t/how-to-add-msg-topic-to-function/48401 "2021-07-14T11:11:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![edstobi](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@edstobi](https://discourse.nodered.org/u/edstobi)\
**Post date:** [14 July 2021 11:11 UTC](https://discourse.nodered.org/t/how-to-add-msg-topic-to-function/48401/1 "2021-07-14T11:11:20Z")

</div>

Hi  
how do I add the msg.topic here.  
My attempts always end with the error message that the function tries to send a message of type string.

so it works, but logically without msg.topic

```auto
if(msg.payload =="on"){
    msg.topic="s7.1.DBs.DB1.Lüfter_an";
  return [msg1,null];
}
if(msg.payload =="off"){
    msg.topic="s7.1.DBs.DB1.Lüfter_aus";
  return [null,msg1]
}

```

Thx for helping

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [14 July 2021 11:21 UTC](https://discourse.nodered.org/t/how-to-add-msg-topic-to-function/48401/2 "2021-07-14T11:21:22Z")

</div>

hi .. the topic assignment looks fine .. the problem is that you are trying to return `msg1` which doesnt exist. Rename it to `msg`

**Test flow**

```auto
[{"id":"af8adf79fb8227e0","type":"inject","z":"4895ea10b4ee9ead","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":270,"y":900,"wires":[["cfb9a2f47cfc443a"]]},{"id":"cfb9a2f47cfc443a","type":"function","z":"4895ea10b4ee9ead","name":"","func":"if (msg.payload == \"on\") {\n msg.topic = \"s7.1.DBs.DB1.Lüfter_an\";\n return [msg, null];\n}\nif (msg.payload == \"off\") {\n msg.topic = \"s7.1.DBs.DB1.Lüfter_aus\";\n return [null, msg]\n}","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":940,"wires":[["17379cbce11f7c46"],["629eebe8d418a008"]]},{"id":"8a2507e4f47d623a","type":"inject","z":"4895ea10b4ee9ead","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":270,"y":1000,"wires":[["cfb9a2f47cfc443a"]]},{"id":"17379cbce11f7c46","type":"debug","z":"4895ea10b4ee9ead","name":"on","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":900,"wires":[]},{"id":"629eebe8d418a008","type":"debug","z":"4895ea10b4ee9ead","name":"off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":980,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)\
**Post date:** [14 July 2021 11:26 UTC](https://discourse.nodered.org/t/how-to-add-msg-topic-to-function/48401/3 "2021-07-14T11:26:08Z")

</div>

Or with one out put but two different topics.

```auto
[{"id":"1f9de5c0.4c944a","type":"tab","label":"Flow 11","disabled":false,"info":""},{"id":"375e9797.eb03b8","type":"inject","z":"1f9de5c0.4c944a","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":260,"y":180,"wires":[["28d5d8eb.556088"]]},{"id":"adc5a123.ee186","type":"inject","z":"1f9de5c0.4c944a","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":270,"y":300,"wires":[["28d5d8eb.556088"]]},{"id":"28d5d8eb.556088","type":"function","z":"1f9de5c0.4c944a","name":"","func":"if(msg.payload ==\"on\"){\n msg.topic=\"s7.1.DBs.DB1.Lüfter_an\"\n return msg;;\n}else if (msg.payload ==\"off\"){\n msg.topic=\"s7.1.DBs.DB1.Lüfter_aus\"\n return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":240,"wires":[["fdd2c86b.658808"]]},{"id":"fdd2c86b.658808","type":"debug","z":"1f9de5c0.4c944a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":840,"y":240,"wires":[]}]

```

---

<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:** [12 September 2021 11:26 UTC](https://discourse.nodered.org/t/how-to-add-msg-topic-to-function/48401/4 "2021-09-12T11:26:44Z")

</div>

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