Splitting a string

9,361 views
Skip to first unread message

Finn Janssens

unread,
Nov 28, 2016, 7:14:50 AM11/28/16
to Node-RED
Hello, I have a arduino sending 2 sensor values as a string (with a space inbetween) to a raspberry pi running node-red. I now want to write a script to split the sensor values, turn them back into integers and then send them to 2 charts. With my current code (http://pastebin.com/zKdBL1jy) it doesnt do much.. when i connect a debug node to one of the 2 outputs of the function node (wich contains my code) it just gives me the result "(undefined)". Could anyone help?

Nicholas O'Leary

unread,
Nov 28, 2016, 7:49:51 AM11/28/16
to Node-RED Mailing List
Hi Finn,

a Function node should return msg objects, not just raw values:


var output = msg.payload.split(" ");
 
var temp = parseInt(output[0]);
var heart = parseInt(output[1]);

var msg1 = {payload:temp};
var msg2 = {payload:heart}

return [msg1,msg2];



Regards,
Nick


On 28 November 2016 at 12:14, Finn Janssens <finnjan...@gmail.com> wrote:
Hello, I have a arduino sending 2 sensor values as a string (with a space inbetween) to a raspberry pi running node-red. I now want to write a script to split the sensor values, turn them back into integers and then send them to 2 charts. With my current code (http://pastebin.com/zKdBL1jy) it doesnt do much.. when i connect a debug node to one of the 2 outputs of the function node (wich contains my code) it just gives me the result "(undefined)". Could anyone help?

--
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/5a2d6b4a-90ba-4fea-b2ae-9e29beda5df7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Zenofmud

unread,
Nov 28, 2016, 7:53:34 AM11/28/16
to node...@googlegroups.com
Nick, you beat me by moments - I was going to suggest:

var output = msg.payload.split(" ");

temp = {payload:parseInt(output[0])};
heart = {payload:parseInt(output[1])};

return [temp, heart];

To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Finn Janssens

unread,
Nov 28, 2016, 8:11:44 AM11/28/16
to Node-RED
Thanks so much! it works perfectly now!


On Monday, November 28, 2016 at 1:49:51 PM UTC+1, Nick O'Leary wrote:
Hi Finn,

a Function node should return msg objects, not just raw values:


var output = msg.payload.split(" ");
 
var temp = parseInt(output[0]);
var heart = parseInt(output[1]);

var msg1 = {payload:temp};
var msg2 = {payload:heart}

return [msg1,msg2];



Regards,
Nick

On 28 November 2016 at 12:14, Finn Janssens <finnjan...@gmail.com> wrote:
Hello, I have a arduino sending 2 sensor values as a string (with a space inbetween) to a raspberry pi running node-red. I now want to write a script to split the sensor values, turn them back into integers and then send them to 2 charts. With my current code (http://pastebin.com/zKdBL1jy) it doesnt do much.. when i connect a debug node to one of the 2 outputs of the function node (wich contains my code) it just gives me the result "(undefined)". Could anyone help?

--
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+u...@googlegroups.com.

Shane Kevin

unread,
Feb 14, 2018, 1:32:32 PM2/14/18
to Node-RED
Hi nike, sorry for bothering you, but what if the payload outputs a string like this: `35,22,47` and I insert the function node with exactly like this code, I only get the payload `35`, how can I get a seperate json object to a `35` and a `22`? Thanks


On Monday, November 28, 2016 at 8:49:51 PM UTC+8, Nick O'Leary wrote:
Hi Finn,

a Function node should return msg objects, not just raw values:


var output = msg.payload.split(" ");
 
var temp = parseInt(output[0]);
var heart = parseInt(output[1]);

var msg1 = {payload:temp};
var msg2 = {payload:heart}

return [msg1,msg2];



Regards,
Nick

On 28 November 2016 at 12:14, Finn Janssens <finnjan...@gmail.com> wrote:
Hello, I have a arduino sending 2 sensor values as a string (with a space inbetween) to a raspberry pi running node-red. I now want to write a script to split the sensor values, turn them back into integers and then send them to 2 charts. With my current code (http://pastebin.com/zKdBL1jy) it doesnt do much.. when i connect a debug node to one of the 2 outputs of the function node (wich contains my code) it just gives me the result "(undefined)". Could anyone help?

--
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+u...@googlegroups.com.

Nick O'Leary

unread,
Feb 14, 2018, 1:35:52 PM2/14/18
to Node-RED Mailing List
Hi,

it depends what exactly you want the payload to look like.

Do you want a separate message for each value? Or do you want one message with all the values arranged in some object?

var values = msg.payload.split(" ");

will give you an array (values)  containing the individual values. What you do next really depends on exactly what you want.

Nick



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.

Shane Kevin

unread,
Feb 14, 2018, 1:54:32 PM2/14/18
to Node-RED
Hi, sorry I am totally a noob in node red
I just simply drop an inject node with string 35,22,47
and then what I care about is the first two digis, so I would like to extract them to two single payload seperately, each msg payload will returns number 35 and number 22 but without a comma "," in the middle, and the 47 is simply drop to trash :)
I've already try the json node, I thought node red can auto-transfer this string to a json object, but I just don't know how, sorry for a very nooob question sir~~

Nick O'Leary

unread,
Feb 14, 2018, 2:14:43 PM2/14/18
to node...@googlegroups.com
No problem. Everyone has to start somewhere.

    msg.payload = msg.payload.split(" ");
    return msg;

This will give you back a single message whose payload is an array or the individual values.

So to get the first two values you would use msg.payload[0] and msg.payload[1].

They will still be String types. If you want them as Numbers (so you can use them in calculations etc) then you could do:

msg.payload = msg.payload.split(" ").map(function(v) { return parseInt(v)});
return msg;

Which ensures each value is parsed to its Number type.

Hope that helps.
Nick

Shane Kevin

unread,
Feb 14, 2018, 2:15:03 PM2/14/18
to Node-RED
hey sir, I think I figure it out how to do this
var test = msg.payload.substr(3, 2);

msg.payload = test;

return msg;

 and I got the number 22 now

can I ask you what is the meaning for (0, 2), does it means at the first string is always=0? so 0=3, 2 means first 2 letters? so it returns 35? does it looks correct to you?

Shane Kevin

unread,
Feb 14, 2018, 2:24:28 PM2/14/18
to Node-RED
That's even awesomer!! Thanks man! You really helped me a lot!

steve rickus

unread,
Feb 14, 2018, 3:52:19 PM2/14/18
to Node-RED
Shane, to be successful with node-red, it certainly helps to be familiar with Javascript syntax and functions -- I like the W3 Schools site and refer to it myself often.

But, if you are new to both Node-RED and Javascript, I suggest getting familiar with the core nodes first. Things like splitting strings can be done without any coding using the "split" and "change" nodes. Since they are single-purpose chunks of logic, you can wire them together to do most of what you need -- then for those special purpose tasks, drop in a function node and code the logic in javascript. Really the best of both worlds, but if you start by trying to code everything inside function nodes, you will miss out on the fun of reusable graphic "flow-gramming".

And if you haven't looked at the flow library yet, there are thousands of community contributed specialty nodes and example flows you can search. It's not often that something I need to do cannot be accomplished using the pre-built core and contrib nodes.
--
Steve

Colin Law

unread,
Feb 14, 2018, 3:58:08 PM2/14/18
to node...@googlegroups.com
The w3schools site is excellent for javascript and other web stuff reference. It is mostly well written. For example looking at the link below should answer those questions.

But I agree with Steve, most stuff like this can be handled by the builtin nodes so it is worth getting to grips with those.

Colin

 

--
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.
Reply all
Reply to author
Forward
0 new messages