How to add msg.topic to function

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

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

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

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

Or with one out put but two different topics.

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

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