Help| how to split string from mqtt

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

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

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


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

this is the output of splitted and separated with "," and not splitted nodes:

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: ", "

1 Like

yes it does split as shown above!

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

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

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

1 Like

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

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

the mqtt msg as below:

and the switch setting as below

how do i refer to x y and z

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

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

Opera Snapshot_2021-07-26_164435_192.168.1.2


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

1 Like

it shows failed to parse JSON string

alright i have change to contains

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

1 Like

i change the output to string and it work

do i need to use json now or leave it

also i manage to save

if you are happy with your result, leave it.

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

how ever the graph is empty

any suggestion regarding this issue

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

[{"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}]
1 Like

I wanted to try but unsure how since im a beginner

:sweat_smile: :sweat_smile:

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

1 Like

woah OK i get this

and this

i will try to redo again

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

the data obtained

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.

1 Like