# 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:** 2

<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:** [27 July 2021 16:16 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/21 "2021-07-27T16:16:15Z")

</div>

No the data is from application that i developed which use built-in accelerometer sensor in smartphone to send data in real-time to MQTT,the average data send is about 3000 per minute 🥴

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/6/6681ab51f1280e0e0abf17b6b1a1f4a092b13956.png)  
Sorry for late reply, i was restricted

---

<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:** [27 July 2021 16:35 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/22 "2021-07-27T16:35:56Z")

</div>

If you developed the app then do you not have control of the format? If instead of publishing the strings you posted, you published JSON strings, for example  
`{"X-AXIS": 0.153, "Y-AXIS": 0.114, "Z-AXIS": 9.807}`  
then in node-red you could tell the MQTT In node to interpret the string as JSON and it would automatically convert it to a javascript object for you, eliminating all the string manipulation in node-red.

---

<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:** [27 July 2021 16:45 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/23 "2021-07-27T16:45:03Z")

</div>

since i am new im quite clueless to be honest,,,however the application is developed via MIT app inventor using code-block and sends as string so the data recieve as below

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/0639d7d48779b6bd3076452b2954ffe453e06673.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:** [27 July 2021 17:00 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/24 "2021-07-27T17:00:13Z")

</div>

Are you not able to change the format of the string to be as I suggested?

---

<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:** [27 July 2021 17:10 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/25 "2021-07-27T17:10:42Z")

</div>

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

i am able to change, and do i change MQTT in to json string now?

---

<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:** [27 July 2021 17:13 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/26 "2021-07-27T17:13:01Z")

</div>

You have the quotes in the wrong place, check my post more carefully.  
Then set the MQTT In output field to Parsed JSON, feed it into a debug node and see what you get.

---

<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:** [27 July 2021 17:16 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/27 "2021-07-27T17:16:41Z")

</div>

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

this happen 😅 ,

---

<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:** [27 July 2021 17:18 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/28 "2021-07-27T17:18:53Z")

</div>

Is that after you have fixed the quotes, which you originally had in the wrong place as I posted a few minutes ago. If yes then set the output back to string and see what you get in the debug.

---

<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:** [27 July 2021 17:21 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/29 "2021-07-27T17:21:08Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/db15fae02fe520f10324f8485291295ad06c704b.png)  
i did get this result using output as string

---

<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:** [27 July 2021 17:22 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/30 "2021-07-27T17:22:30Z")

</div>

As I said in my post a few minutes ago, you still have the quotes in the wrong place. Go back and check my posts more carefully.

---

<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:** [27 July 2021 17:25 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/31 "2021-07-27T17:25:59Z")

</div>

yes i notice it now 😬 😬 i will fix it and try again, will post here the result

---

<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:** [27 July 2021 17:36 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/32 "2021-07-27T17:36:51Z")

</div>

after fixing

in MQTT

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

in node red(using JSON string)

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

it works, but how do i split it into individual csv of X-axis Y-axis and Z-axis alongside time and date for each data

---

<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:** [27 July 2021 17:42 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/33 "2021-07-27T17:42:25Z")

</div>

you would just need to remove the function node, the flow should then work as it was.

---

<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:** [27 July 2021 17:54 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/34 "2021-07-27T17:54:03Z")

</div>

i did remove the function node

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

but error occured

---

<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:** [27 July 2021 17:55 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/35 "2021-07-27T17:55:22Z")

</div>

And what exactly is the JSONata expression? you need to supply more info

---

<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:** [27 July 2021 18:00 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/36 "2021-07-27T18:00:13Z")

</div>

how do i send like this, 😅

if im able to send like this it will be easier to check each parts

---

<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:** [27 July 2021 18:03 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/37 "2021-07-27T18:03:35Z")

</div>

To export a flow highlight all the nodes (you can use shift or ctrl to highlight multipe) then press Ctrl e, and copy to clipboard.  
Then when you post it press the \</\> button and paste flow json between the backticks  
e.g.  
```  
code  
```

---

<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:** [27 July 2021 18:06 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/38 "2021-07-27T18:06:40Z")

</div>

```auto
[{"id":"fade33b0.31dc2","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"64915f5c.0a7238","type":"change","z":"fade33b0.31dc2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\t\"X-AXIS,\" & $$.payload.X-AXIS & $moment().tz(\"Asia/Tokyo\").format(\",DD/MM/YYYY,h:mm:ss a\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":280,"wires":[["55a1aba7.7cb53c","8258bfd2.d739a"]]},{"id":"85bcb7f0.7b1cc8","type":"change","z":"fade33b0.31dc2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\t\"Y-AXIS,\" & $$.payload.Y-AXIS & $moment().tz(\"Asia/Tokyo\").format(\",DD/MM/YYYY,h:mm:ss a\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":340,"wires":[["8258bfd2.d739a","a2609af6.5d05b"]]},{"id":"258a1d49.c7bce2","type":"change","z":"fade33b0.31dc2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\t\"Z-AXIS,\" & $$.payload.Z-AXIS & $moment().tz(\"Asia/Tokyo\").format(\",DD/MM/YYYY,h:mm:ss a\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":400,"wires":[["8258bfd2.d739a","90b1a43d.e74fa"]]},{"id":"b539e42d.73bd48","type":"ui_gauge","z":"fade33b0.31dc2","name":"","group":"8b5cde76.edd58","order":5,"width":0,"height":0,"gtype":"gage","title":"X-AXIS","label":"m/s2","format":"{{msg.payload[\"X-AXIS\"]}}","min":"-10","max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":520,"y":520,"wires":[]},{"id":"70d60df4.60ab3c","type":"ui_gauge","z":"fade33b0.31dc2","name":"","group":"8b5cde76.edd58","order":5,"width":0,"height":0,"gtype":"gage","title":"Y-AXIS","label":"m/s2","format":"{{msg.payload[\"Y-AXIS\"]}}","min":"-10","max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":520,"y":580,"wires":[]},{"id":"685e5157.f187b8","type":"ui_gauge","z":"fade33b0.31dc2","name":"","group":"8b5cde76.edd58","order":5,"width":0,"height":0,"gtype":"gage","title":"Z-AXIS","label":"m/s2","format":"{{msg.payload[\"Z-AXIS\"]}}","min":"-10","max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":520,"y":640,"wires":[]},{"id":"8258bfd2.d739a","type":"debug","z":"fade33b0.31dc2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":220,"wires":[]},{"id":"55a1aba7.7cb53c","type":"file","z":"fade33b0.31dc2","name":"","filename":"C:\\Users\\User\\Desktop\\x-axis.csv","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"utf8","x":860,"y":280,"wires":[[]]},{"id":"a2609af6.5d05b","type":"file","z":"fade33b0.31dc2","name":"","filename":"C:\\Users\\User\\Desktop\\y-axis.csv","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"utf8","x":860,"y":340,"wires":[[]]},{"id":"90b1a43d.e74fa","type":"file","z":"fade33b0.31dc2","name":"","filename":"C:\\Users\\User\\Desktop\\z-axis.csv","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"utf8","x":860,"y":400,"wires":[[]]},{"id":"3124b708.80021","type":"mqtt in","z":"fade33b0.31dc2","name":"","topic":"SENSE","qos":"0","datatype":"json","broker":"277379c9.ca83a6","nl":false,"rap":true,"rh":0,"x":110,"y":500,"wires":[["64915f5c.0a7238","85bcb7f0.7b1cc8","258a1d49.c7bce2","b539e42d.73bd48","70d60df4.60ab3c","685e5157.f187b8"]]},{"id":"8b5cde76.edd58","type":"ui_group","name":"","tab":"8f03e639.85956","order":1,"disp":true,"width":"12","collapse":false},{"id":"277379c9.ca83a6","type":"mqtt-broker","name":"","broker":"broker.hivemq.com","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"8f03e639.85956","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<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:** [27 July 2021 18:11 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/39 "2021-07-27T18:11:44Z")

</div>

Ok the issue is when you edited the expression you put

```auto
"X-AXIS," & $$.payload.X-AXIS & $moment().tz("Asia/Bangkok").format(",DD/MM/YYYY,h:mm:ss a")

```

while it should be

```auto
"X-AXIS," & $$.payload.`X-AXIS` & $moment().tz("Asia/Bangkok").format(",DD/MM/YYYY,h:mm:ss a")

```

notice the backticks around `X-AXIS` that is needed otherwise `-` would be taken as a minus character and JSONata tries to preform a calculation with the strings either side. This causes the error

[edit] the issue was cause by the forum rendering of the code i posted as it contained `. I have corrected the post so others will not be caught out

---

<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:** [27 July 2021 18:18 UTC](https://discourse.nodered.org/t/help-how-to-split-string-from-mqtt/48931/40 "2021-07-27T18:18:31Z")

</div>

YES it did work perfectly!

ok now i notice that difference between `X-AXIS` and X-AXIS

the flow work well and the data in csv is great

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

thanks alot

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

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