# Multiple mqtt data to single id

**URL:** <https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062>\
**Category:** General\
**Created:** [27 December 2020 01:14 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062 "2020-12-27T01:14:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lindberg87](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lindberg87/32/34286_2.png) [@lindberg87](https://discourse.nodered.org/u/lindberg87)\
**Post date:** [27 December 2020 01:14 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/1 "2020-12-27T01:14:49Z")

</div>

Hey

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  
Lindberg87

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [27 December 2020 01:43 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/2 "2020-12-27T01:43:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [27 December 2020 01:50 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/3 "2020-12-27T01:50:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lindberg87](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lindberg87/32/34286_2.png) [@lindberg87](https://discourse.nodered.org/u/lindberg87)\
**Post date:** [27 December 2020 01:52 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/4 "2020-12-27T01:52:28Z")

</div>

![nodered-pellmon1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6dcf0ea0baaa3c0c03b77a48d7e8053ce9d05783.png)

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [27 December 2020 01:55 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/5 "2020-12-27T01:55:37Z")

</div>

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_.

---

<div class="post-metadata">

**Author:** ![lindberg87](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lindberg87/32/34286_2.png) [@lindberg87](https://discourse.nodered.org/u/lindberg87)\
**Post date:** [27 December 2020 01:59 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/6 "2020-12-27T01:59:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [27 December 2020 02:00 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/7 "2020-12-27T02:00:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [27 December 2020 02:03 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/8 "2020-12-27T02:03:13Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![lindberg87](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lindberg87/32/34286_2.png) [@lindberg87](https://discourse.nodered.org/u/lindberg87)\
**Post date:** [27 December 2020 02:05 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/9 "2020-12-27T02:05:32Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![lindberg87](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lindberg87/32/34286_2.png) [@lindberg87](https://discourse.nodered.org/u/lindberg87)\
**Post date:** [27 December 2020 02:14 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/10 "2020-12-27T02:14:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [27 December 2020 02:39 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/11 "2020-12-27T02:39:05Z")

</div>

Sorry, I don't understand the question.

`switch` and `split` are very different things.

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [27 December 2020 03:29 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/12 "2020-12-27T03:29:05Z")

</div>

In his original function he was using msg.id 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  
[EDIT]  
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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [27 December 2020 09:45 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/13 "2020-12-27T09:45:21Z")

</div>

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  
`pellmon/timer_hotwater_start_1`  
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  
`pellmon/timer/hotwater/start_1`

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:

 ![Screen Shot 2020-12-27 at 4.43.57 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f1daf87fb92695f2764329114b78470814be0e8.png)

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

```

---

<div class="post-metadata">

**Author:** ![lindberg87](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lindberg87/32/34286_2.png) [@lindberg87](https://discourse.nodered.org/u/lindberg87)\
**Post date:** [27 December 2020 09:59 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/14 "2020-12-27T09:59:11Z")

</div>

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

Best Regards

---

<div class="post-metadata">

**Author:** ![lindberg87](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lindberg87/32/34286_2.png) [@lindberg87](https://discourse.nodered.org/u/lindberg87)\
**Post date:** [27 December 2020 10:05 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/15 "2020-12-27T10:05:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wb666greene](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wb666greene/32/6534_2.png) [@wb666greene](https://discourse.nodered.org/u/wb666greene)\
**Post date:** [27 December 2020 15:12 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/16 "2020-12-27T15:12:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [27 December 2020 21:51 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/17 "2020-12-27T21:51:43Z")

</div>

(I'll bite: what is YMMV?)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [27 December 2020 23:59 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/18 "2020-12-27T23:59:07Z")

</div>

YMMV your mileage might vary

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 December 2020 00:01 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/19 "2020-12-28T00:01:34Z")

</div>

I should have guessed it, but we use metric.... 😜

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [11 January 2021 00:01 UTC](https://discourse.nodered.org/t/multiple-mqtt-data-to-single-id/38062/20 "2021-01-11T00:01:42Z")

</div>

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