Send message to another topic

Hey guys!

I'm new to node-red so I'm not getting a solution to my problem.

So I get via mqtt a message that I need to extract some data, and then I need to send two separate messages, one stop each topic, but I'm not able to do that:

image

My "Extract Info" node has the following code:

var topic = msg.topic;
var payload = msg.payload;
var product = payload.gtin;
var description=payload.items[0].name;
var price = payload.items[0].price;
var thumbnail = payload.thumbnail;
var id = payload.items[0].id;
var msgid = {payload: id};

msg.payload = 

description + "\r\n" + 
"Preço : R$ " + price  + "\r\n"; 

msg.topic = "esp32/product_return/"+topic.substr(19);

return [msg,msgid];

I would like to send the content of msgid to a topic other than msg.topic. How can I do this?

Thanks!

Depends how you want to set it... but you could just do

var msgid = {payload: id , topic: "myNewTopic"};

You can create an array of payloads and topics and then they will be sent as two seperate mssages to the same output

 [[{"payload":1,"topic":"aaa/bbb"},
{"payload":2,"topic":"fff/ggg"}]]
[{"id":"f70ec784.46d18","type":"inject","z":"190c8195.72b3be","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"somegggggggggg/topic/blahblahblah","payload":"","payloadType":"date","x":480,"y":740,"wires":[["39ccfe1.914be82"]]},{"id":"39ccfe1.914be82","type":"function","z":"190c8195.72b3be","name":"","func":"var topic = msg.topic;\nvar payload = msg.payload;\nvar product = \"gggg\";\nvar description=\"fffff\";\nvar price = 123;\nvar thumbnail = \"mmmm\";\nvar id = 123;\nvar msgid = {payload: id, topic:\"anothe/topc\"};\n\nfirst = {payload: description + \"\\r\\n\" + \"Preço : R$ \" + price  + \"\\r\\n\",\ntopic: \"esp32/product_return/\"+topic.substr(19)};\nmsg =[[first, msgid]]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":690,"y":740,"wires":[["6182f651.7b026","7f92c9b6.d14948"]]},{"id":"6182f651.7b026","type":"mqtt out","z":"190c8195.72b3be","name":"","topic":"","qos":"","retain":"","broker":"d675b749.04b9c8","x":840,"y":840,"wires":[]},{"id":"7f92c9b6.d14948","type":"debug","z":"190c8195.72b3be","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":740,"wires":[]},{"id":"d675b749.04b9c8","type":"mqtt-broker","name":"Localhost","broker":"localhost","port":"1883","clientid":"","usetls":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

You don't have to use two outputs from your function node. Draw two wires from the same output and in one of them insert a change node.!
The message along each wire will have the same id. At least, it did in my experiment!

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