Multiple mqtt data to single id


I need your help, I´m new to node red.

How can I filter/seperate multiple mqtt input data to single ids ?

I tried with this code, but it didn't work as I want it to do:
(function node)
paths = msg.topic.split("/");
msg.loc = paths[1];
msg.sensor = paths[2];
msgsid = ("2180fdb1.8c8842");
return msg;

Best Regards

MQTT messages are a bit tricky. First thing I suggest you do is put the message through a JSON node and decode it back to what is should be.

Going through MQTT alters things in the message.
The only TWO things to go through the MQTT pipe are: msg.payload and msg.topic.

So before you send a message through MQTT, make sure the data is encompassed within msg.payload, or you are going to have no luck at all with what you want to do.

To help others, copy the message you are wanting to send (just before it goes into the MQTT node)
and/or forget MQTT (for now) and show what you want to do with the message.

That way it would be a bit clearer what you are wanting to do.

I'm not quite sure what you are asking. A mqtt msg is received with a topic and a payload. The toopic can have multiple 'levels' like node41/livingroom/bme280/temperature. Are you trying to split that up?

if you want to split up the parts of the topic, what you are doing will work fine. If you want to create seperate msgs, one for each part of the topic, I have to ask 'Why? What are you trying to do?


Thank you.

If you look on this data from the mqtt input, I want to separate the ids in single nodes for make single sensor data, hope I explain my self good enough .

Best Regards.

Sorry: which ID's?

I'm guessing the payload is an example..... 1234..... But....

That is a message which has come from what ever and you want to send it (via MQTT) else where.

So what is it you want to send?
(Got the out/in the wrong way around)

This is what you are receiving... Ok. Again: No problem.

Though it is still unclear what you want to do with the message.

I want to take the "_msgid:xxxxxxxx" and separate/split it into single node, because there are about 30 sensor inputs I want to extract and send to home assistant.

If you want those two msgs to go down different flow paths, use a switch node testing msg.topic and use the 'contains' option to look for 'period' and 'start' and the msgs will go different ways.

If you have ... about 30 sensors..... They would be on different topics. Sorry: should be.

That would make it a bit easier. (Just a thought.)

Okay that could maybe be the solution...
Sorry I´m new to node-red so its little hard for me to explain what I want.

Is it also possible to take topic names and split them into single paths ?

Should I use switch node to split the topic in single data ?

Sorry, I don't understand the question.

switch and split are very different things.

In his original function he was using which might be problematic, I think. I also understand using long topics and id's but that also makes typo errors easier to make. As @Trying_to_learn pointed out every sensor should be uniquely identified assuming you have control of that aspect and you may want to consider something easier to type
you aren't trying to receive every mqtt message in a single node are you? The easy way (easier in my opinion) would be a receive node for every sensor, it might be he's receiving every message on a single node and is trying to parse it all out to send to different functions

As you have seen from the responses, there are multiple ways to do what you want.

  • use the switch node to check for a word contained in the topic
  • have multiple mqtt-in nodes with different topics (I wouldn't do this, I like to have one spot for the msgs to come into NR to make maintenance easier six months down the line.)

In any case, do you control the device creating the MQTT message?
What do the parts of the topic represent?

You have a topic
pellmon/timer_hotwater_period and
If that repredsents a unit(pellmon) a sensor (timer a device (hotwater) and an action (period or start_1) you could dfine your topic as /unit(?)/sensor/device/action which would result in the msg.topics being:
pellmon/timer/hotwater/period and

This would make it much easier to split the topic when it gets to NR. In eithor case here is a sample showing the two ideas:

[{"id":"1ec8d46.1e2feac","type":"group","z":"b02613e9.ed1bd","name":"Example using `switch` node","style":{"fill":"#ffefbf","label":true,"color":"#000000"},"nodes":["9186f60e.d98688","42d5cb10.0b9124","2aaa06d1.1ddcea","5ee57486.6cfa04"],"x":54,"y":359,"w":532,"h":162},{"id":"9186f60e.d98688","type":"mqtt in","z":"b02613e9.ed1bd","g":"1ec8d46.1e2feac","name":"","topic":"pellmon/#","qos":"2","datatype":"auto","broker":"8d5cfad5.2fc08","x":140,"y":440,"wires":[["5ee57486.6cfa04"]]},{"id":"42d5cb10.0b9124","type":"debug","z":"b02613e9.ed1bd","g":"1ec8d46.1e2feac","name":"period (switch)","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":460,"y":400,"wires":[]},{"id":"2aaa06d1.1ddcea","type":"debug","z":"b02613e9.ed1bd","g":"1ec8d46.1e2feac","name":"start (switch)","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":450,"y":480,"wires":[]},{"id":"5ee57486.6cfa04","type":"switch","z":"b02613e9.ed1bd","g":"1ec8d46.1e2feac","name":"","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"period","vt":"str"},{"t":"cont","v":"start","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":270,"y":440,"wires":[["42d5cb10.0b9124"],["2aaa06d1.1ddcea"]]},{"id":"8d5cfad5.2fc08","type":"mqtt-broker","name":"","broker":"mqttpizw.local","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""},{"id":"3df1483a.874ab","type":"group","z":"b02613e9.ed1bd","name":"Simulate data creation","style":{"fill":"#ffefbf","label":true,"color":"#000000"},"nodes":["b5fd4031.138768","640c83fb.1c3e94","fb79eebc.5454f"],"x":54,"y":19,"w":352,"h":122},{"id":"b5fd4031.138768","type":"inject","z":"b02613e9.ed1bd","g":"3df1483a.874ab","name":"period","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"pellmon/timer_hotwater_period","payload":"1234","payloadType":"num","x":150,"y":60,"wires":[["640c83fb.1c3e94"]]},{"id":"640c83fb.1c3e94","type":"mqtt out","z":"b02613e9.ed1bd","g":"3df1483a.874ab","name":"","topic":"","qos":"","retain":"","broker":"8d5cfad5.2fc08","x":330,"y":80,"wires":[]},{"id":"fb79eebc.5454f","type":"inject","z":"b02613e9.ed1bd","g":"3df1483a.874ab","name":"start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"pellmon/timer_hotwater_start_1","payload":"1234","payloadType":"num","x":150,"y":100,"wires":[["640c83fb.1c3e94"]]},{"id":"e1ced345.a4e928","type":"group","z":"b02613e9.ed1bd","name":"Example using two `mqtt-in` nodes","style":{"label":true,"fill":"#ffefbf","color":"#000000"},"nodes":["61c4ee8f.9c5be8","505d81c3.1e","a97c8fcd.91c6b8","c7ec295b.ef7bc8"],"x":54,"y":159,"w":532,"h":162},{"id":"61c4ee8f.9c5be8","type":"mqtt in","z":"b02613e9.ed1bd","g":"e1ced345.a4e928","name":"","topic":"pellmon/timer_hotwater_period","qos":"2","datatype":"auto","broker":"8d5cfad5.2fc08","x":210,"y":200,"wires":[["505d81c3.1e"]]},{"id":"505d81c3.1e","type":"debug","z":"b02613e9.ed1bd","g":"e1ced345.a4e928","name":"period","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":200,"wires":[]},{"id":"a97c8fcd.91c6b8","type":"mqtt in","z":"b02613e9.ed1bd","g":"e1ced345.a4e928","name":"","topic":"pellmon/timer_hotwater_start_1","qos":"2","datatype":"auto","broker":"8d5cfad5.2fc08","x":210,"y":280,"wires":[["c7ec295b.ef7bc8"]]},{"id":"c7ec295b.ef7bc8","type":"debug","z":"b02613e9.ed1bd","g":"e1ced345.a4e928","name":"start","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":280,"wires":[]}]
1 Like

Thank you very much that was exactly the solution what I need.

Best Regards

Yes you have right, I was receiving all data in one note but needed to split it into parts so all multi sensor data would go to be single sensor.

What I wanted was, when node red receive mqtt data from the device node red should be able to distinguish between the ids names and divide the data into each their box so it is not a big box alone.

I've abused the msg.topic to send meta data along with the jpg image msg.payload to do what the OP seems to want to do with a switch node on msg.topic. The key is these are QOS 0 messages without retain, otherwise the mqtt server could have storage space issues as "unique" topics accumulate with no other subscribers. Been running 24/7/365 for well over a year now.

I also use multiple topic specific mqtt in nodes, I think it make the flow easier to understand and modify. YMMV.

(I'll bite: what is YMMV?)

YMMV your mileage might vary

1 Like

I should have guessed it, but we use metric.... :stuck_out_tongue_winking_eye:

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