How to split string, multiple values inside string to separate output.

10,436 views
Skip to first unread message

TridiumControls

unread,
Jan 20, 2018, 4:43:05 PM1/20/18
to Node-RED
Hi, I'm running into a puzzle here, 

I have a payload string that has 3 separate values, but they are scattered. Wondering how I go about filtering them out into separate outputs.

1st output: Connecting to 192.168.1.22:4025 is one string, 
2nd output: Connected
3rd output: Update: DISARMED CHIME Ready to Arm

Debug output:

Connecting to 192.168.1.22:4025
Connected
Update:  DISARMED CHIME   Ready to Arm 

Thanks, 
Screen Shot 2018-01-20 at 1.35.49 PM.png

steve rickus

unread,
Jan 20, 2018, 5:17:33 PM1/20/18
to Node-RED
So you have a single msg.payload that is a string with 3 lines... you can split them into separate lines using either the split node, or the js function:

var lines = msg.payload.split("\n");

But I don't see what good that will do for you. Perhaps you could explain a bit more what problem you are attempting to solve?
--
Steve

TridiumControls

unread,
Jan 20, 2018, 10:03:46 PM1/20/18
to Node-RED
Thanks Steve, 
What i'm looking to do is to send each line into a separate mqtt message. So I get this now in the debug. but I cannot figure out how to filter these message into a separate output so that I can send to a mqtt output node. I tried using the function code you provided instead and get the error below.

1/20/2018, 7:00:10 PMnode: 32225bd4.e3c0e4msg : Object
object
payload: "Connecting to 192.168.1.22:4025"
_msgid: "fe6623c5.1c10a"
parts: object
id: "fe6623c5.1c10a"
type: "string"
ch: "↵"
index: 0
count: 4
1/20/2018, 7:00:10 PMnode: 32225bd4.e3c0e4msg : Object
object
payload: "Connected"
_msgid: "fe6623c5.1c10a"
parts: object
id: "fe6623c5.1c10a"
type: "string"
ch: "↵"
index: 1
count: 4
1/20/2018, 7:00:10 PMnode: 32225bd4.e3c0e4msg : Object
object
payload: "Update: DISARMED CHIME Ready to Arm "
_msgid: "fe6623c5.1c10a"
parts: object
id: "fe6623c5.1c10a"
type: "string"
ch: "↵"
index: 2
count: 4

When I used 
var lines = msg.payload.split("\n");



1/20/2018, 7:02:09 PMnode: e95dc0df.af2d8function : (error)
"Function tried to send a message of type string"
1/20/2018, 7:02:09 PMnode: e95dc0df.af2d8function : (error)
"Function tried to send a message of type string"
1/20/2018, 7:02:09 PMnode: e95dc0df.af2d8function : (error)
"Function tried to send a message of type string"

Zenofmud

unread,
Jan 21, 2018, 5:41:04 AM1/21/18
to 'Milica Lekic' via Node-RED
You can’t return a string from a function, it must be a object - i.e. you can’t return ‘lines’ you need to move l\’lines to msg.payload and return msg
msg.payload = lines;

However, why not use a ‘split’ or ‘change’ node to break the messages apart. here is an example, the ‘split’ node will give you three messages, while the ‘change’ node (using jsonata) will give you one message with the msg.payload being an array

[{"id":"4d13a5ab.69d09c","type":"inject","z":"18a52596.b44c8a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":94,"y":226,"wires":[["ee4b3742.8cd7f8"]]},{"id":"3f6fb3cf.75c074","type":"split","z":"18a52596.b44c8a","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":366,"y":167,"wires":[["dc567d65.349188"]]},{"id":"dc567d65.349188","type":"debug","z":"18a52596.b44c8a","name":"","active":true,"console":"false","complete":"true","x":651,"y":169,"wires":[]},{"id":"ee4b3742.8cd7f8","type":"function","z":"18a52596.b44c8a","name":"","func":"msg.payload = 'Connected to 192.188.1.22:4025\\nConnected\\nUpdate: DISARED CHIME Ready to Arm';\nreturn msg;","outputs":1,"noerr":0,"x":231,"y":225,"wires":[["3f6fb3cf.75c074","f6d43f00.93294"]]},{"id":"ad63e9f4.94737","type":"debug","z":"18a52596.b44c8a","name":"","active":true,"console":"false","complete":"true","x":655,"y":273,"wires":[]},{"id":"f6d43f00.93294","type":"change","z":"18a52596.b44c8a","name":"create array with change node","rules":[{"t":"set","p":"payload","pt":"msg","to":"$split(payload,\"\\n\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":437,"y":273,"wires":[["ad63e9f4.94737"]]}]


AIOT MAKER

unread,
Jan 21, 2018, 11:18:53 AM1/21/18
to Node-RED
Using the split node as suggested by Paul will work perfectly and it is clean and neat.

However, if you decide using a function node then take into account that the function node can send multiple messages over the same output as long as you return an array of objects. Each object should have at least one property which is normally named payload (but in your case you may want to add a topic property to make easier to handle the downstream MQTT node). It will not work if the function return an array of strings. Perhaps the code below will help you understand this point about returning an array.



var lines = msg.payload.split("\n");
var outp = [];
outp[0] = {payload:lines[0]};
outp[1] = {payload:lines[1]};
outp[2] = {payload:lines[2]};
return [outp];






Flow:

[{"id":"481655af.3d508c","type":"inject","z":"b4b2d449.027278","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":100,"y":100,"wires":[["dee28a36.c1d688"]]},{"id":"2969b0df.681bf","type":"debug","z":"b4b2d449.027278","name":"","active":true,"console":"false","complete":"true","x":570,"y":100,"wires":[]},{"id":"4649d773.00e968","type":"function","z":"b4b2d449.027278","name":"","func":"var lines = msg.payload.split(\"\\n\");\nvar outp = [];\noutp[0] = {payload:lines[0]};\noutp[1] = {payload:lines[1]};\noutp[2] = {payload:lines[2]};\nreturn [outp];","outputs":1,"noerr":0,"x":410,"y":100,"wires":[["2969b0df.681bf"]]},{"id":"dee28a36.c1d688","type":"function","z":"b4b2d449.027278","name":"","func":"msg.payload = 'Connected to 192.188.1.22:4025\\nConnected\\nUpdate:  DISARED CHIME   Ready to Arm';\nreturn msg;","outputs":1,"noerr":0,"x":250,"y":100,"wires":[["4649d773.00e968"]]}]


Nick O'Leary

unread,
Jan 21, 2018, 1:22:29 PM1/21/18
to Node-RED Mailing List
Hi,

as you have already solved splitting the message into multiple messages, as shown by your screenshot of debug, thanks to the split node, your question was: how to filter these message into a separate output?

If you look closely at those messages, you'll see they all have a property called msg.parts.index - each with a unique value. The split node has split up your original message into a sequence of three messages - the msg.parts.index property shows its position in the sequence. You can make use of that property with the Switch node:

1. add a Switch node and set its property to msg.parts.index
2. add three rules to compare that value against 0, 1, 2

That will produce a node with three outputs, and each message will be sent to the corresponding output.

Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/7f0dea8e-604d-42f8-bb2f-0452870b2bb7%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

AIOT MAKER

unread,
Jan 21, 2018, 1:37:13 PM1/21/18
to Node-RED
Here is a couple of pictures and a link to illustrate what Nick explained:

TridiumControls

unread,
Jan 21, 2018, 9:29:31 PM1/21/18
to Node-RED
Thanks Nick, Aiot, and everyone else, learned a lot, appreciate all the help, works exactly like I wanted to. Awesome community to be a part of. 
Reply all
Reply to author
Forward
0 new messages