# How to split mqtt tag values

**URL:** <https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794>\
**Category:** General\
**Created:** [3 May 2019 19:21 UTC](https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794 "2019-05-03T19:21:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![muharremsari](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/muharremsari/32/7996_2.png) [@muharremsari](https://discourse.nodered.org/u/muharremsari)\
**Post date:** [3 May 2019 19:21 UTC](https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794/1 "2019-05-03T19:21:44Z")

</div>

Have a two tags or more recived from mqtt broker. How to split tag from each other for seperated outputs?

Best regards

 ![node_red_two_tag](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7113577fe0e3cb46ed5a8604efc54151da08c60b.jpeg)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [3 May 2019 19:33 UTC](https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794/2 "2019-05-03T19:33:23Z")

</div>

msg.payload is a string to first thing is to convert it to an object. Try running it through the JSON node. See the cookbook [https://cookbook.nodered.org/](https://cookbook.nodered.org/)  
Then the page in the docs about working with messages will be worth reading

---

<div class="post-metadata">

**Author:** ![muharremsari](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/muharremsari/32/7996_2.png) [@muharremsari](https://discourse.nodered.org/u/muharremsari)\
**Post date:** [9 May 2019 15:03 UTC](https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794/3 "2019-05-09T15:03:51Z")

</div>

I was convert string to object. I try more method but still mixed. How i can do that ![mqtt_tag_object](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/87d8214f6d2ab442ff2b8b52b6f8146a94d89511.jpeg)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [9 May 2019 15:39 UTC](https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794/4 "2019-05-09T15:39:02Z")

</div>

I assume you want msg.payload.q ?

If so, what’s creating the MQTT message as it’s coming in the wrong way round ( value, key) rather than (key, value). If you can, it might be better to fix the sender.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 May 2019 15:49 UTC](https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794/5 "2019-05-09T15:49:18Z")

</div>

Unclear topic.

Do you want to split the payload, or do you want multiple outputs, 1 for each payload ?  
If you want multiple outputs, the input always needs to be the same.

---

<div class="post-metadata">

**Author:** ![muharremsari](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/muharremsari/32/7996_2.png) [@muharremsari](https://discourse.nodered.org/u/muharremsari)\
**Post date:** [10 May 2019 08:18 UTC](https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794/6 "2019-05-10T08:18:49Z")

</div>

I'm so sorry about mistaken words .

I don't want multiple outputs for 1 each payload. I would peer to peer.

I've get two message (maybe is up to "n" in future) from mqtt broker. I was spllit payload "v" for both message at the JSON but output value switching between two payload and it's randomly!

![node_red_switch_between](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c9f0c542ea64e8003dfd9d74db7151c48fe0890c.jpeg)

---

<div class="post-metadata">

**Author:** ![muharremsari](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/muharremsari/32/7996_2.png) [@muharremsari](https://discourse.nodered.org/u/muharremsari)\
**Post date:** [10 May 2019 08:25 UTC](https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794/7 "2019-05-10T08:25:36Z")

</div>

Broker Message template shown below

{  
"timestamp": |SERVERTIMESTAMP|,  
"values": [  
|#each VALUES|  
{"id": "|TAGNAME|", "v": |VALUE|, "q": |QUALITY|, "t": |TIMESTAMP| } |#unless @last|,|/unless|  
|/each|  
]  
}

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [10 May 2019 12:42 UTC](https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794/8 "2019-05-10T12:42:37Z")

</div>

Please do a `mqtt` -\> `debug` and post the output here.

---

<div class="post-metadata">

**Author:** ![muharremsari](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/muharremsari/32/7996_2.png) [@muharremsari](https://discourse.nodered.org/u/muharremsari)\
**Post date:** [11 May 2019 05:20 UTC](https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794/9 "2019-05-11T05:20:25Z")

</div>

iotgateway : msg : Object

object

topic: "iotgateway"

payload: "{"timestamp":1557554912495,"values":[{"id":"HSM1.EGD.SPR.SM\DC2000\ODAKUZ\_FB","v":21.6999989,"q":true,"t":1557554904136},{"id":"HSM1.EGD.SPR.SM\DC2000\ODAGUN\_FB","v":24.1999989,"q":true,"t":1557554904136},{"id":"HSM1.EGD.SPR.SM\DC2000\ODAGUN\_FB","v":24.0999985,"q":true,"t":1557554905136},{"id":"HSM1.EGD.SPR.SM\DC2000\ODAKUZ\_FB","v":21.3999996,"q":true,"t":1557554906136},{"id":"HSM1.EGD.SPR.SM\DC2000\ODAKUZ\_FB","v":21.5999985,"q":true,"t":1557554907136},{"id":"HSM1.EGD.SPR.SM\DC2000\ODAGUN\_FB","v":24.2999992,"q":true,"t":1557554908136},{"id":"HSM1.EGD.SPR.SM\DC2000\ODAGUN\_FB","v":24.0999985,"q":true,"t":1557554910136},{"id":"HSM1.EGD.SPR.SM\DC2000\ODAKUZ\_FB","v":21.4999981,"q":true,"t":1557554911136}]}"

qos: 1

retain: false

\_msgid: "b4b34316.41302"

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [11 May 2019 05:59 UTC](https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794/10 "2019-05-11T05:59:00Z")

</div>

The `\` is problematic, as it is an escape character, can those be changed to `/` ?

Try this flow (I have changed the backslashes to make it work with an inject node. Replace the inject node with your mqtt node and set the mqtt node to output json):

```auto
[{"id":"aee7d67a.411258","type":"inject","z":"f298621.db6eda","name":"","topic":"","payload":"{\"timestamp\":1557554912495,\"values\":[{\"id\":\"HSM1.EGD.SPR.SM/DC2000/ODAKUZ_FB\",\"v\":21.6999989,\"q\":true,\"t\":1557554904136},{\"id\":\"HSM1.EGD.SPR.SM/DC2000/ODAGUN_FB\",\"v\":24.1999989,\"q\":true,\"t\":1557554904136},{\"id\":\"HSM1.EGD.SPR.SM/DC2000/ODAGUN_FB\",\"v\":24.0999985,\"q\":true,\"t\":1557554905136},{\"id\":\"HSM1.EGD.SPR.SM/DC2000/ODAKUZ_FB\",\"v\":21.3999996,\"q\":true,\"t\":1557554906136},{\"id\":\"HSM1.EGD.SPR.SM/DC2000/ODAKUZ_FB\",\"v\":21.5999985,\"q\":true,\"t\":1557554907136},{\"id\":\"HSM1.EGD.SPR.SM/DC2000/ODAGUN_FB\",\"v\":24.2999992,\"q\":true,\"t\":1557554908136},{\"id\":\"HSM1.EGD.SPR.SM/DC2000/ODAGUN_FB\",\"v\":24.0999985,\"q\":true,\"t\":1557554910136},{\"id\":\"HSM1.EGD.SPR.SM/DC2000/ODAKUZ_FB\",\"v\":21.4999981,\"q\":true,\"t\":1557554911136}]}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":352,"wires":[["7657164f.129d38"]]},{"id":"7657164f.129d38","type":"function","z":"f298621.db6eda","name":"","func":"i = msg.payload.values\n\ni.forEach((e)=>{\n\n node.send({payload:{id:e.id, value:e.v, timestamp:e.t}})\n \n})\n \n\n\n\n","outputs":1,"noerr":0,"x":358,"y":352,"wires":[["6adbd054.c41878"]]},{"id":"6adbd054.c41878","type":"debug","z":"f298621.db6eda","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":532,"y":352,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![muharremsari](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/muharremsari/32/7996_2.png) [@muharremsari](https://discourse.nodered.org/u/muharremsari)\
**Post date:** [11 May 2019 07:20 UTC](https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794/11 "2019-05-11T07:20:10Z")

</div>

Thanks for express reply. Problem solved.

I was replace backslashes with slash at the broker side.

---

<div class="post-metadata">

**Author:** ![muharremsari](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/muharremsari/32/7996_2.png) [@muharremsari](https://discourse.nodered.org/u/muharremsari)\
**Post date:** [11 May 2019 10:31 UTC](https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794/12 "2019-05-11T10:31:59Z")

</div>

I have seen topics debug window with below code. How to separate topic for lot of output on the function node

for(var i in msg.payload.values)  
{  
var newmessage = msg.payload.values[i].v;  
var newtopic = msg.topic + '/' + msg.payload.values[i].id;  
node.send({ payload:newmessage, topic:newtopic });  
}

return msg;

#################################################################################

result like this;

iotgateway/HSM1.EGD.SPR.SM/DC2000/ODAGUN\_FB : msg : Object

object

payload: 24.0999985

topic: "iotgateway/HSM1.EGD.SPR.SM/DC2000/ODAGUN\_FB"

\_msgid: "4c36ac97.64fa74"

* * *

iotgateway/HSM1.EGD.SPR.SM/DC2000/ODAKUZ\_FB : msg : Object

object

payload: 21.4999981

topic: "iotgateway/HSM1.EGD.SPR.SM/DC2000/ODAKUZ\_FB"

\_msgid: "344a4e1b.626562"

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [11 May 2019 11:30 UTC](https://discourse.nodered.org/t/how-to-split-mqtt-tag-values/10794/13 "2019-05-11T11:30:19Z")

</div>

Yes, same method, i used a shorthand/ES6 version

You can use something like:  
node.send({topic:e.id, payload:e.v})
