Problem SPLITting and SWITCHing data from a Serial Port stream

Hi all,

I have a problem which I just cannot understand and therefore, haven't managed to solve. I'm pretty new to Node Red but have done programming in the past

I am reading data in from a serial connection to my Solar Tubes water heating store. Data comes once per minute. I am looking to extract the temperature readings for the Top, Middle and Bottom of the storage tank

The stream comes in as follows:
2021-04-03 13:03:40;52;27;37;50;17;35;7;1;1;0;5;1;22;18136;32767;EU;2;TR0603mc;Err;1;0;; ending in a CR (0xa)

So I wrote a simple first function

msg.payload = msg.payload.split(';');
msg.payload = "Top:" + msg.payload[4] + ",Middle:" + msg.payload[3] + ",Bottom:" + msg.payload[2];
return msg;  

which gives me the new array string "Top:xx,Middle:xx,Bottom:xx". I then use the SPLIT node, splitting on a Comma, and a Fixed Length of 1, and then sending it into a SWITCH with 3 options created using contains Top, Middle or Bottom.

The problem is that the output is correct the first time around, but then goes haywire - see the Debug window

Here again is some more of the debug

4/4/2021, 1:29:04 PM[node: Whole Array](http://192.168.1.4:1880/#)msg.payload : string[26]
"Top:56,Middle:54,Bottom:39"
4/4/2021, 1:29:04 PM[node: Top](http://192.168.1.4:1880/#)msg.payload : string[15]
"Bottom:39Top:56"
4/4/2021, 1:29:05 PM[node: Bottom](http://192.168.1.4:1880/#)msg.payload : string[15]
"Bottom:39Top:56"
4/4/2021, 1:29:06 PM[node: Middle](http://192.168.1.4:1880/#)msg.payload : string[9]
"Middle:54"
4/4/2021, 1:30:04 PM[node: Whole Array](http://192.168.1.4:1880/#)msg.payload : string[26]
"Top:56,Middle:54,Bottom:39"
4/4/2021, 1:30:04 PM[node: Top](http://192.168.1.4:1880/#)msg.payload : string[15]
"Bottom:39Top:56"
4/4/2021, 1:30:05 PM[node: Bottom](http://192.168.1.4:1880/#)msg.payload : string[15]
"Bottom:39Top:56"
4/4/2021, 1:30:06 PM[node: Middle](http://192.168.1.4:1880/#)msg.payload : string[9]
"Middle:54"

Probably due to how you have the serial node setup. Can you show the config of your serial node.
Also, show us the raw data coming out of the serial node.

Question: what send the serial data? If you are in control of the data then I suggest formating the data to JSON and then converstion back to usable data (in the node-red side) would be much easier.

If your intention is to have separate values, why do you bother concatenating them then splitting? You could build 4 messages in one go (no joining or splitting required)

Thanks @Steve-Mcl

The data comes from a controller unit (Steca TR0603mc), out of an RS232 interface, connected via a USB interface to an RPi, and comes out like this once a minute
2021-04-03 13:03:40;52;27;37;50;17;35;7;1;1;0;5;1;22;18136;32767;EU;2;TR0603mc;Err;1;0;;

So this is my starting point - this is the data that comes into /dev/usbTTY0

So I am looking to extract the 3rd, 4th and 5th items of the string - so "27;37;50". So I could pass these straight into a SPLIT, separated by semi-colons ";", but I am unsure how to setup the SWITCH - or what other method ?

If you are certain you capture the serial data perfectly each time, then the splitting of that data is quite simple...

Here is 3 ways of splitting it up without the joining and (re)spliting...

Demo flow...

[{"id":"a9dafda9.08fe9","type":"inject","z":"83a23089.dc697","name":"fake comm data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2021-04-03 13:03:40;52;27;37;50;17;35;7;1;1;0;5;1;22;18136;32767;EU;2;TR0603mc;Err;1;0;;","payloadType":"str","x":800,"y":560,"wires":[["16d49792.947ca8","a6d83226.76e24","243f62ee.cbfafe"]]},{"id":"16d49792.947ca8","type":"function","z":"83a23089.dc697","name":"one msg","func":"var parts = msg.payload.split(\";\");\nmsg.payload = {\n    \"Top\": parts[2],\n    \"Middle\": parts[3],\n    \"Bottom\": parts[4],\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1000,"y":560,"wires":[["afd7dc59.ed2d6"]]},{"id":"a6d83226.76e24","type":"function","z":"83a23089.dc697","name":"3 messages","func":"var parts = msg.payload.split(\";\");\nvar msg1 = { topic: \"Top\", payload: parts[2] };\nvar msg2 = { topic: \"Middle\", payload: parts[3] };\nvar msg3 = { topic: \"Bottom\", payload: parts[4] };\n\nreturn [[msg1, msg2, msg3]];","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1010,"y":620,"wires":[["2e224f47.ad104"]]},{"id":"243f62ee.cbfafe","type":"function","z":"83a23089.dc697","name":"individuals","func":"var parts = msg.payload.split(\";\");\nvar msg1 = { topic: \"Top\", payload: parts[2] };\nvar msg2 = { topic: \"Middle\", payload: parts[3] };\nvar msg3 = { topic: \"Bottom\", payload: parts[4] };\n\n\nreturn [msg1, msg2, msg3];","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1010,"y":740,"wires":[["6eba899a.c67d48"],["4941090a.b846f8"],["939cad38.9641f"]]},{"id":"afd7dc59.ed2d6","type":"debug","z":"83a23089.dc697","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1190,"y":560,"wires":[]},{"id":"2e224f47.ad104","type":"debug","z":"83a23089.dc697","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1190,"y":620,"wires":[]},{"id":"6eba899a.c67d48","type":"debug","z":"83a23089.dc697","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1190,"y":680,"wires":[]},{"id":"939cad38.9641f","type":"debug","z":"83a23089.dc697","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1190,"y":800,"wires":[]},{"id":"4941090a.b846f8","type":"debug","z":"83a23089.dc697","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1190,"y":740,"wires":[]}]

HOWEVER: I still suspect the comm data is being split at wrong points. Put a debug on the end of the serial node & verify the data coming in is ALWAYS as expected

Here's a screenshot of the screen and debug

and the debug

4/4/2021, 3:35:14 PM[node: Whole Array](http://192.168.1.4:1880/#)msg.payload : string[89]

"2021-04-04 16:16:37;49;39;68;73;19;33;7;1;0;0;5;1;19;18180;32767;EU;2;TR0603mc;Err;1;0;;↵"

4/4/2021, 3:36:14 PM[node: Whole Array](http://192.168.1.4:1880/#)msg.payload : string[89]

"2021-04-04 16:17:37;49;39;68;73;19;33;7;1;0;0;5;1;19;18180;32767;EU;2;TR0603mc;Err;1;0;;↵"

4/4/2021, 3:37:14 PM[node: Whole Array](http://192.168.1.4:1880/#)msg.payload : string[89]

"2021-04-04 16:18:37;49;39;68;73;19;33;7;1;0;0;5;1;19;18180;32767;EU;2;TR0603mc;Err;1;0;;↵"

4/4/2021, 3:38:15 PM[node: Whole Array](http://192.168.1.4:1880/#)msg.payload : string[89]

"2021-04-04 16:19:37;49;40;68;73;19;33;7;1;0;0;5;1;19;18180;32767;EU;2;TR0603mc;Err;1;0;;↵"

Your example has field names and then a colon and then the data. My items have no field names, just the data, semi colon delimited. So I'm not sure how I would put this through a SWITCH node

ok, so it looks like the comm data is stable.

So did you try attaching the serial node to the functions I posted?

you can import my flow and simply connect a wire from the serial node to the 3 functions (just like the fake comm data inject is)

Ah yes, I never realised that you actually posted the node - I thought that you only just posted a diagram.

That worked absolutely spot on - thank you soooo much. I will use the "individuals" one because I will send all outputs into MQTT OUT node (to my MQTT server)

From a learning perspective, have you any idea as to why my original effort did not work ?

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