Combining 2 parts of a msg.payload to form a Topic/Subtopic for publishing to MQTT

Hello. I'm not sure if this is even possible.... I am trying to take 2 separate parts of a msg.payload and convert them into an MQTT formatted topic/subtopic.

For example, I want msg.topic to equal "msg.payload.parameter1 / msg.payload.parameter2" of the incoming node so when the message is then fed into an MQTT publish node, the topic seen at the broker will be parameter1/parameter2.

I can currently get the topic to set itself to parameter1 alone by using a change node but don't quite know the syntax for combining 2 parameters into a single topic separated by a slash for compatibility with MQTT.

I think the join node can solve this.

Here is an example using JSONata in a change node, you could also use javascript in a function node.

[{"id":"7ec23cce.84de5c","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"param1\":\"param1\",\"param2\":\"param2\"}","payloadType":"json","x":140,"y":4400,"wires":[["beba9359.6c44f8"]]},{"id":"beba9359.6c44f8","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload.param1 & \"/\" & payload.param2","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":4420,"wires":[["e40ac006.709d5"]]},{"id":"e40ac006.709d5","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":4400,"wires":[]}]
1 Like

This did it! Thank you!

Oops, I did not get the question, sorry :no_mouth:

Mostly correct, you could split and join.

[{"id":"5c069ba0.5d188c","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"param1\":\"param1\",\"param2\":\"param2\"}","payloadType":"json","x":170,"y":4440,"wires":[["c53201a1.c945f"]]},{"id":"c53201a1.c945f","type":"split","z":"c791cbc0.84f648","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":330,"y":4360,"wires":[["97f47398.fe9e1"]]},{"id":"97f47398.fe9e1","type":"join","z":"c791cbc0.84f648","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"/","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":450,"y":4360,"wires":[["2dc9bff.e0ff24"]]},{"id":"2dc9bff.e0ff24","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":4360,"wires":[["ccc79603.d75458"]]},{"id":"ccc79603.d75458","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":4440,"wires":[]}]

For completeness you can also use a template

[{"id":"4a1983d9.a7083c","type":"template","z":"c791cbc0.84f648","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.param1}}/{{payload.param2}}","output":"str","x":380,"y":4280,"wires":[["e40ac006.709d5"]]},{"id":"7ec23cce.84de5c","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"param1\":\"param1\",\"param2\":\"param2\"}","payloadType":"json","x":190,"y":4280,"wires":[["4a1983d9.a7083c"]]},{"id":"e40ac006.709d5","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":4300,"wires":[]}]

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