Split a three value message, and create 3 messages with the count as part of the topic

I'm receiving a message like this from mqtt -

transformer/temperature_PL6X10 33
transformer/temperature_PL6X12 [50, 40, 43]
transformer/temperature_PL6X13 [59, 48, 49]

I want to output:

transformer/temperature_PL6X10, 33
transformer/temperature_PL6X12_1, 50
transformer/temperature_PL6X12_2, 40
transformer/temperature_PL6X12_3, 43
transformer/temperature_PL6X13_1, 59
transformer/temperature_PL6X13_2, 48
transformer/temperature_PL6X13_3, 49

split gives me 3 messages with the same topic, I need to increment the topic # if there are multiple values.

Ended up splitting the messages in python before publishing to the broker. Now node-red has an easier time inserting into influx.

I'm not understanding the incoming messages.

Is that three examples of messages or the continuation of the original message?

What do you mean increment the topic #?

What is the topic you want it to be?

From this:

transformer/temperature_PL6X12 [50, 40, 43]

I want this:

transformer/temperature_PL6X12_1 50
transformer/temperature_PL6X12_2 40
transformer/temperature_PL6X12_3 43

Arrays can be iterated using the split node.

Then you update the topic of each message emitted from the split node to append "_n" to the topic (where n is the index + 1)

NOTE: The split node adds msg.parts to each message, which contains the index. (see built in help of split node) (add a debug node to the output of the split node (set to show complete message) to this for your own understanding)

Demo:

[{"id":"c61dd404de610e73","type":"inject","z":"06a2836c9f2ae124","name":"transformer/temperature_PL6X12  [50, 40, 43]","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"transformer/temperature_PL6X12","payload":" [50, 40, 43]","payloadType":"json","x":1410,"y":460,"wires":[["efac7f31e3da5f23"]]},{"id":"cefe06e6888df2bf","type":"split","z":"06a2836c9f2ae124","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","property":"payload","x":1370,"y":400,"wires":[["6fdd372739f2be43"]]},{"id":"6fdd372739f2be43","type":"change","z":"06a2836c9f2ae124","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"topic & \"_\" & (parts.index+1)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1510,"y":400,"wires":[["8ee933d648549309"]]},{"id":"8ee933d648549309","type":"debug","z":"06a2836c9f2ae124","name":"debug 14","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1820,"y":400,"wires":[]},{"id":"efac7f31e3da5f23","type":"switch","z":"06a2836c9f2ae124","name":"","property":"payload","propertyType":"msg","rules":[{"t":"istype","v":"array","vt":"array"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1670,"y":460,"wires":[["cefe06e6888df2bf"],["c9904fc2b7f3ee69"]]},{"id":"c9904fc2b7f3ee69","type":"debug","z":"06a2836c9f2ae124","name":"debug 15","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1820,"y":480,"wires":[]},{"id":"89333c91338bce96","type":"inject","z":"06a2836c9f2ae124","name":"transformer/temperature_PL6X13 55","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"transformer/temperature_PL6X13","payload":"55","payloadType":"num","x":1380,"y":520,"wires":[["efac7f31e3da5f23"]]},{"id":"58ddd82492454565","type":"inject","z":"06a2836c9f2ae124","name":"transformer/temperature_PL6X14 \"some text\"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"transformer/temperature_PL6X14","payload":"some text","payloadType":"str","x":1410,"y":580,"wires":[["efac7f31e3da5f23"]]}]

2 Likes

Thank you!

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