# Help| how to split string from mqtt

**URL:** <https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931>\
**Category:** General\
**Created:** [26 July 2021 14:52 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931 "2021-07-26T14:52:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![puteramelaka](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/puteramelaka/32/45520_2.png) [@puteramelaka](https://discourse.nodered.org/u/puteramelaka)\
**Post date:** [26 July 2021 14:52 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/1 "2021-07-26T14:52:24Z")

</div>

i have send reading of accelerometer from MQTT to node-red in three axis (x -axis,y-axis and z-axis).  
however the message recieved is in the form of string. i have been watching youtube and reading google etc.i found that by using split node we are able to split the data from line that conisist xyz into a own line x , y and z .after split the data i want to save as csv and build graph from the reading.Since i am very new its quite confusing and im quite lost.i will show image detail below to make it clear and easier to help me.

msg receieved as below (had to remove image as new user can send 2 image only)  
" X-AXIS :0.076, Y-AXIS :0.134, Z-AXIS :9.768"

the view of current node-red

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/f/1fbb10124406c3c44dec5a56fec975d45bd486dd.png)

i cannot connect since the data is not split yet..

what i imagine is as below...but i believe there are many ways to make it,please suggest the best way

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/218eeeba0ed03a3cbd0f65f54d804fff681bcdad.png)

is this possible? **how do i code the split** so i can save data acceleration of X,acceleration of Y and acceleration of Z in 3 different csv file and the show the data in graf

THANKS

---

<div class="post-metadata">

**Author:** ![sfedo](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfedo](https://discourse.nodered.org/u/sfedo)\
**Post date:** [26 July 2021 15:09 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/2 "2021-07-26T15:09:32Z")

</div>

if its an exactly string as you've send, than you can use your "," as separator.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/7/0757fe197d6fbe395605116efd4da981d65808f4.png)

this is the output of splitted and separated with "," and not splitted nodes:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/240c3e610f9ab669b3ad5a35e5f1fc9e153f4b66.png)

PS: if you use "," and 1 space, you remove that one space on second and third value that I had in my screenshot. like this: ", "

---

<div class="post-metadata">

**Author:** ![puteramelaka](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/puteramelaka/32/45520_2.png) [@puteramelaka](https://discourse.nodered.org/u/puteramelaka)\
**Post date:** [26 July 2021 15:20 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/3 "2021-07-26T15:20:29Z")

</div>

yes it does split as shown above!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/f/1f943c07683f7968a5ae5338ef8dd4d15b94a35d.png)

**how do i save** x , y and z **into csv** ,, **how do i refer to the splited line** of x-axis, y-axis and z-axis

---

<div class="post-metadata">

**Author:** ![sfedo](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfedo](https://discourse.nodered.org/u/sfedo)\
**Post date:** [26 July 2021 15:24 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/4 "2021-07-26T15:24:50Z")

</div>

you can use switch node now:  
its maybe not the best solution, but works 😉

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/f/cf9f9a7185d5894be1c4105290615f5f62f71a42.png)

I cant help you save to csv, didn't worked with csv so far.

---

<div class="post-metadata">

**Author:** ![puteramelaka](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/puteramelaka/32/45520_2.png) [@puteramelaka](https://discourse.nodered.org/u/puteramelaka)\
**Post date:** [26 July 2021 15:40 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/5 "2021-07-26T15:40:49Z")

</div>

i did do the switch-node but it did not work.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/2/621cb28f0f7fadce8d29d4a9c6d9e4986205a0bc.png)

but when i connect to the old payload it work,,,

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/3889a805a35dbb06fc62fa73aee4ca8b4a742d15.png)

---

<div class="post-metadata">

**Author:** ![puteramelaka](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/puteramelaka/32/45520_2.png) [@puteramelaka](https://discourse.nodered.org/u/puteramelaka)\
**Post date:** [26 July 2021 15:41 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/6 "2021-07-26T15:41:27Z")

</div>

the mqtt msg as below:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/498239e5d46d1898a8b19f8c435edd92d42cde65.png)

and the switch setting as below

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/cab2b36c6dc32ec0fd7fab2928c6ce9c81112bf2.png)

how do i refer to x y and z

(had to reply twice since new user can only send 2 media in 1 reply)

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [26 July 2021 15:45 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/7 "2021-07-26T15:45:52Z")

</div>

Set the MQTT in node to output a parsed JSON Object:

![Opera Snapshot_2021-07-26_164435_192.168.1.2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/34d558ab1c059100472339a9f22e9a5530f5c7ec.png)

---

<div class="post-metadata">

**Author:** ![sfedo](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfedo](https://discourse.nodered.org/u/sfedo)\
**Post date:** [26 July 2021 15:49 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/8 "2021-07-26T15:49:19Z")

</div>

yes hold a minute, why you even use string output? use parsed json object, much easy to work with, I forgot about this. Thanks @ghayne

you cannot use "==" operator there. Choose "contains". The == need the exact string, but since your numbers change, you cannot use this

---

<div class="post-metadata">

**Author:** ![puteramelaka](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/puteramelaka/32/45520_2.png) [@puteramelaka](https://discourse.nodered.org/u/puteramelaka)\
**Post date:** [26 July 2021 15:50 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/9 "2021-07-26T15:50:52Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6ec45da75298caf075bf469fc2e19f6699e442c9.png)

it shows failed to parse JSON string

---

<div class="post-metadata">

**Author:** ![puteramelaka](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/puteramelaka/32/45520_2.png) [@puteramelaka](https://discourse.nodered.org/u/puteramelaka)\
**Post date:** [26 July 2021 15:52 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/10 "2021-07-26T15:52:09Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/06ce5eee7c5d89093743a8e4853e01cf1c708953.png)

alright i have change to contains

---

<div class="post-metadata">

**Author:** ![sfedo](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfedo](https://discourse.nodered.org/u/sfedo)\
**Post date:** [26 July 2021 15:58 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/11 "2021-07-26T15:58:08Z")

</div>

I guess it says "failed to parse" because you have split node for strings.  
use your mqtt with parsed json object to debug node, without split and show the output

---

<div class="post-metadata">

**Author:** ![puteramelaka](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/puteramelaka/32/45520_2.png) [@puteramelaka](https://discourse.nodered.org/u/puteramelaka)\
**Post date:** [26 July 2021 16:00 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/12 "2021-07-26T16:00:11Z")

</div>

i change the output to string and it work

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/4/64c250dddf3f90bd9312c1ecd0fb2c97e007c063.png)

do i need to use json now or leave it

also i manage to save

 ![resultt x y z](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/5/a59eac4d82d3bbd53ba81a9f3815661edef644c5.png)

---

<div class="post-metadata">

**Author:** ![sfedo](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfedo](https://discourse.nodered.org/u/sfedo)\
**Post date:** [26 July 2021 16:01 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/13 "2021-07-26T16:01:58Z")

</div>

if you are happy with your result, leave it.

---

<div class="post-metadata">

**Author:** ![puteramelaka](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/puteramelaka/32/45520_2.png) [@puteramelaka](https://discourse.nodered.org/u/puteramelaka)\
**Post date:** [26 July 2021 16:26 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/14 "2021-07-26T16:26:55Z")

</div>

im happy with the result and will leave it, thanks alot to @sfedo and @ghayne i really appreciate it

for now ,im trying to visualize the data recieved in graph, the flow as below

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/3442be0fa20d9e62648dcb3827f540d23fe8c0ac.png)

how ever the graph is empty

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/5409653a5a1a53dda56daf5782561a32133d4193.png)

any suggestion regarding this issue

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 July 2021 16:52 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/15 "2021-07-26T16:52:45Z")

</div>

Try this example it splits the string and then asigns them to an object

```auto
[{"id":"e077981e.bba8b8","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":" X-AXIS :0.076, Y-AXIS :0.134, Z-AXIS :9.768","payloadType":"str","x":130,"y":2220,"wires":[["369b48bf.a5189"]]},{"id":"369b48bf.a5189","type":"function","z":"c74669a0.6a34f8","name":"","func":"let parts = msg.payload.split(\",\");\nmsg.payload={};\nfor(let i = 0; i < parts.length; i++){\n let bits = parts[i].split(\":\");\n msg.payload[bits[0].trim()] = bits[1].trim()\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":2220,"wires":[["64915f5c.0a7238","85bcb7f0.7b1cc8","258a1d49.c7bce2","b539e42d.73bd48","70d60df4.60ab3c","685e5157.f187b8"]]},{"id":"64915f5c.0a7238","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"X-AXIS,\" & $$.payload.`X-AXIS`","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":2220,"wires":[["8258bfd2.d739a","55a1aba7.7cb53c"]]},{"id":"85bcb7f0.7b1cc8","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"Y-AXIS,\" & $$.payload.`Y-AXIS`","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":2260,"wires":[["8258bfd2.d739a","a2609af6.5d05b"]]},{"id":"258a1d49.c7bce2","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"Z-AXIS,\" & $$.payload.`Z-AXIS`","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":2320,"wires":[["8258bfd2.d739a","90b1a43d.e74fa"]]},{"id":"b539e42d.73bd48","type":"ui_gauge","z":"c74669a0.6a34f8","name":"","group":"8b5cde76.edd58","order":5,"width":0,"height":0,"gtype":"gage","title":"X-AXIS","label":"units","format":"{{msg.payload[\"X-AXIS\"]}}","min":0,"max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":360,"y":2420,"wires":[]},{"id":"70d60df4.60ab3c","type":"ui_gauge","z":"c74669a0.6a34f8","name":"","group":"8b5cde76.edd58","order":5,"width":0,"height":0,"gtype":"gage","title":"Y-AXIS","label":"units","format":"{{msg.payload[\"Y-AXIS\"]}}","min":0,"max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":360,"y":2460,"wires":[]},{"id":"685e5157.f187b8","type":"ui_gauge","z":"c74669a0.6a34f8","name":"","group":"8b5cde76.edd58","order":5,"width":0,"height":0,"gtype":"gage","title":"Z-AXIS","label":"units","format":"{{msg.payload[\"Z-AXIS\"]}}","min":0,"max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":360,"y":2500,"wires":[]},{"id":"8258bfd2.d739a","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":2160,"wires":[]},{"id":"55a1aba7.7cb53c","type":"file","z":"c74669a0.6a34f8","name":"","filename":"x-axis.csv","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"utf8","x":740,"y":2220,"wires":[[]]},{"id":"a2609af6.5d05b","type":"file","z":"c74669a0.6a34f8","name":"","filename":"y-axis.csv","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"utf8","x":730,"y":2260,"wires":[[]]},{"id":"90b1a43d.e74fa","type":"file","z":"c74669a0.6a34f8","name":"","filename":"z-axis.csv","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"utf8","x":730,"y":2320,"wires":[[]]},{"id":"8b5cde76.edd58","type":"ui_group","name":"","tab":"8f03e639.85956","order":1,"disp":true,"width":"12","collapse":false},{"id":"8f03e639.85956","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![puteramelaka](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/puteramelaka/32/45520_2.png) [@puteramelaka](https://discourse.nodered.org/u/puteramelaka)\
**Post date:** [26 July 2021 17:01 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/16 "2021-07-26T17:01:01Z")

</div>

I wanted to try but unsure how since im a beginner

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/e/2e2ed0a684fc534e74c074aa22d900a988abf3d3.png)  
😅 😅

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 July 2021 17:15 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/17 "2021-07-26T17:15:39Z")

</div>

copy the flow json i provided.  
then in the node-red editor press control i, then paste in the json and press import.

 ![Screenshot_20210726-181617](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0b76954fcf9e1c56b1d3774947b94b5075b9b590.png)

---

<div class="post-metadata">

**Author:** ![puteramelaka](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/puteramelaka/32/45520_2.png) [@puteramelaka](https://discourse.nodered.org/u/puteramelaka)\
**Post date:** [26 July 2021 17:25 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/18 "2021-07-26T17:25:41Z")

</div>

woah OK i get this

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cd18b7e2646e9959bb71380a5a6476e8e038d493.png)

and this

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/a/aa22a79a53ed5c52c5131511a7d21c041047d868.png)

i will try to redo again

---

<div class="post-metadata">

**Author:** ![puteramelaka](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/puteramelaka/32/45520_2.png) [@puteramelaka](https://discourse.nodered.org/u/puteramelaka)\
**Post date:** [26 July 2021 17:36 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/19 "2021-07-26T17:36:00Z")

</div>

well i import my mqqt in node and edit the location of csv file saved ,it works as intended , thank you so much

the flow of node-red

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/30dfb378d0062e5ba7b616b9befd7f4229948353.png)

the data obtained

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/5/157cd7aab2cc1106269ab256c05a32e654d1f585.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 July 2021 18:01 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/20 "2021-07-26T18:01:27Z")

</div>

Are you writing the data to MQTT yourself? If so there is a much better way to do the whole thing which will save you messing about with strings. You should write the data as a JSON string then the MQTT node will parse it for you.

[Next page](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931.md?page=2)
