Trying to remove spaces from value

2,401 views
Skip to first unread message

Phil Grant

unread,
May 18, 2015, 6:31:31 PM5/18/15
to node...@googlegroups.com
Hi,
I'm new to node-red and have got my RPi running node red a passing values from my serial port to Grovestreams OK but I have a problem that when the temp. valus falls below 10.0 degrees the value doesn't get accepted by GS.

I'm sure this is because the data has a leading space in it when the '1' doesn't appear.

I'm trying to remove any leading space with the str.trim() in a function node but I'm obviously doing it wrong.

My function strips out the label from my data and leaves me with just a value in line one, that works OK but when I try to trim it it breaks the flow.

var newMsg = { payload: msg.payload.split(":")[1]};
var newMsg2 = newMsg.trim();
return newMsg2;


any pointers on how to do it right would be much appreciated, it's probably obvious but I've not really done much Javascript.

Andrew Lindsay

unread,
May 19, 2015, 1:25:33 AM5/19/15
to node...@googlegroups.com
You are trying to trim the whole newMsg json object and not the value you want in payload. Try

var pl = msg.payload.split(":")[1].trim();
var newMsg = { payload: pl}
return newMsg2;


Andrew

Andrew Lindsay

unread,
May 19, 2015, 1:26:52 AM5/19/15
to node...@googlegroups.com
Should be a return newMsg; at the end not newMsg2.

Phil Grant

unread,
May 19, 2015, 8:35:21 AM5/19/15
to node...@googlegroups.com
Thanks Andrew I'll give it a try tonight.

Although I thought that this line 

var newMsg = { payload: msg.payload.split(":")[1]};

meant that newMsg was now equal to the second part of msg.payload, split at the first ':' and not the whole Json object.

I think I will start to read up on javascript programming for dummies!

Cheers.

Nicholas O'Leary

unread,
May 19, 2015, 9:07:11 AM5/19/15
to Node-RED Mailing LIst
Phil, when you say it breaks the flow, is there a particular error message displayed anywhere?

--
http://nodered.org
---
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.
For more options, visit https://groups.google.com/d/optout.

Phil Grant

unread,
May 19, 2015, 9:40:05 AM5/19/15
to node...@googlegroups.com
Nicholas,
I will have a look tonight when I get home but if I remember it just stopped updating the debug window, I've also being getting some strange error messages in the RPi terminal window about address errors or something like.

I have been getting lockups and SD card corruption when trying to add the pushbullet node and DS18B20 node via npm so I have a base img file on the PC with node-red installed and I keep writing the image to an SD card and trying again.

Cheers

Julian Knight

unread,
May 19, 2015, 10:33:42 AM5/19/15
to node...@googlegroups.com
That sounds odd. I wonder if your SD card is going - that can be a fairly regular occurrence with cheap cards.

Antoine Aflalo

unread,
May 19, 2015, 10:49:53 AM5/19/15
to node...@googlegroups.com
 If you're using the SD card for anything else than booting the pi, keep in mind you shouldn't overclock it even with the configuration tool given.
Personally I'm using an hard drive plugged to a power hub to avoid any possible sdcard corruption and still overclock.

Phil Grant

unread,
May 19, 2015, 11:10:00 AM5/19/15
to node...@googlegroups.com
I thought the same and have 2 8GB SD cards on order from Ebuyer but also I might have been pulling the power out too soon after the shutdown command....we'll see.

Phil Grant

unread,
May 19, 2015, 11:31:26 AM5/19/15
to node...@googlegroups.com
Julian,
I suspect it maybe as a result of trying to add another node, I'm not even certain I'm doing it right but it did work for the Grovestreams node, maybe I'm running out of SD card space, time for an 8GB.
The whole RPi did freeze completely while trying to add the pushbullet node.

On Tuesday, 19 May 2015 15:33:42 UTC+1, Julian Knight wrote:

Phil Grant

unread,
May 19, 2015, 2:31:33 PM5/19/15
to node...@googlegroups.com
Andrew,
I tried your code and I get this error
STATUS: 200
HEADERS
: {"date":"Tue, 19 May 2015 18:04:38
GMT"
,"server":"Apache","accept-ranges":"bytes","vary":"Accept-Charset,Accept-Encoding,Accept-Language,Accept","access-control-allow-origin":"*","access-control-allow-methods":"GET,
PUT, POST, DELETE, OPTIONS"
,"access-control-allow-headers":"accept,
origin, x-requested-with,
content-type"
,"access-control-max-age":"86400","content-length":"0","access-control-allow-credentials":"true","keep-alive":"timeout=15,
max=1000"
,"connection":"Keep-Alive","content-type":"application/json;charset=UTF-8"}

??

Nicholas O'Leary

unread,
May 19, 2015, 2:41:39 PM5/19/15
to Node-RED Mailing LIst
Phil,

were exactly did you get that error? It isn't something node-red would print out if there was an issue with your function.

Nick

--

Nicholas O'Leary

unread,
May 19, 2015, 2:42:32 PM5/19/15
to Node-RED Mailing LIst
Oh, and Status 200 isn't an error - that means whatever HTTP request that is the response to was successful.... but without knowing where you saw that output, it is hard to say what is happening.

Phil Grant

unread,
May 19, 2015, 2:51:39 PM5/19/15
to node...@googlegroups.com
OK,
Sorry about the lack of detail, the whole story.

Serial data from Arduino via USB "Outside Temp:xx.x" (xx.x = 10.1) -> into node-red via serial node -> into function node to remove label (Split on ':') this leaves a value 10.1, this then gets sent to Grovestream via GS node. All works fine until temp. falls below 10.0 degrees then i think the dtostrf function adds a space into the value where the 1 used to be and then I get an error code returned from GS (not the one above).

So I wanted to strip the leading space in the function node but I cannot get it to work, I have confirmed that without a space it works by splitting on ': ' when the value is below 10.0 and it works fine.

If the payload into the function node ="Outside Temp: 9.8" and I want out the other end to be "9.8" no space.

I hope this explains it a bit better.

Thanks for the help.

Phil Grant

unread,
May 19, 2015, 2:53:48 PM5/19/15
to node...@googlegroups.com
Nicholas,
Yes I realise that this is a response from GS but I didn't know what it meant. :-)


On Tuesday, 19 May 2015 19:42:32 UTC+1, Nicholas O'Leary wrote:

Nicholas O'Leary

unread,
May 19, 2015, 2:59:28 PM5/19/15
to Node-RED Mailing LIst
Here's the code for a function node that will extract any number from the end of the payload, and replace the payload with it. It uses a regular expression to identify and extract the number. It assumes there is a space immediately before the number.

var m = / (\d+(\.\d+)?)$/.exec(msg.payload);

if (m) {
   msg.payload = m[1];
} else {
   return null;
}
return msg;


If the payload doesn't end with a number, the flow halts (return null).

Phil Grant

unread,
May 19, 2015, 3:17:40 PM5/19/15
to node...@googlegroups.com
Nicholas,
Absolutely brilliant, works a treat thanks very much.

Phil

Phil Grant

unread,
May 19, 2015, 6:08:50 PM5/19/15
to node...@googlegroups.com
Oops! spoke too soon, the errors stopped and the debug data was looking good but when I checked the GS data later there was none... it looks like the function doesn't send a value out, I added a debug node and confirmed it, there's no data in the output from the function.

Anyway I'm off to bed, enough for one day.

Dave C-J

unread,
May 19, 2015, 6:55:41 PM5/19/15
to node...@googlegroups.com

Nicks regex assumes there is always a space before a number... If it's 10 or more is that true ? (Or is it only true under 10 ?)

Mark Setrem

unread,
May 19, 2015, 7:09:50 PM5/19/15
to node...@googlegroups.com
Just a thought, but it might be easier if you pasted an example of the output of the serial node?

Phil Grant

unread,
May 20, 2015, 1:48:11 AM5/20/15
to node...@googlegroups.com
Dave,
There is only a space if the temperature is less than 10.00 so there may or may not be a space, last night when testing the temp. Was below 10.

Mark,
The packet from the serial node = "Outside Temp:10.1" the number is a temperature so varies, the problem occurs when it falls below 10 degrees and then it becomes 9 Point something or lower preceeded by a space.

I will try injecting some values directly into the function to make sure it's not something strange with my serial data.

I appreciate the help.

Dave C-J

unread,
May 20, 2015, 2:30:59 AM5/20/15
to node...@googlegroups.com

You could try
msg.payload = parseFloat(msg.payload.split(":")[1]);
return msg;

Julian Knight

unread,
May 20, 2015, 3:56:39 AM5/20/15
to node...@googlegroups.com
A regex:

/\:\s*(.*)\s*$/

Should pick up anything after a colon but will strip out the whitespace. Of course, it picks non-whitespace text not just numbers.

Julian Knight

unread,
May 20, 2015, 3:57:30 AM5/20/15
to node...@googlegroups.com
Dave, you should probably trim your suggestion to ensure no whitespaces get included.


On Wednesday, 20 May 2015 07:30:59 UTC+1, Dave C-J wrote:

Nicholas O'Leary

unread,
May 20, 2015, 4:00:09 AM5/20/15
to Node-RED Mailing LIst
Phil,

sorry about that - missed the bit that two-digit numbers wouldn't have the space. Here is a fixed version of my regex:

var m = /[: ](\d+(\.\d+)?)$/.exec(msg.payload);

if (m) {
   msg.payload = m[1];
} else {
   return null;
}
return msg;

It now looks for either a space or a colon before the digits start.

Nick

--

Dave C-J

unread,
May 20, 2015, 4:02:14 AM5/20/15
to node...@googlegroups.com

Julian,
ParseFloat tries to look for numbers and will handle whitespace just fine. No need to trim.

Julian Knight

unread,
May 20, 2015, 7:31:25 AM5/20/15
to node...@googlegroups.com
Doh, should have spotted that sorry. Does it fail for blank strings though?

Phil Grant

unread,
May 20, 2015, 4:02:04 PM5/20/15
to node...@googlegroups.com
Thanks for all your suggestions Guys.

Dave's parse float solution works great.

A good night, I even got Pushbullet setup and working, just don't receive a message from you phone and send it to your phone it got stuck in a  loop and I got 30 messages before I broke the connection.....

Cheers,
Phil


On Wednesday, 20 May 2015 07:30:59 UTC+1, Dave C-J wrote:

Dave C-J

unread,
May 20, 2015, 5:05:28 PM5/20/15
to node...@googlegroups.com

+1. We've all been there !

Reply all
Reply to author
Forward
0 new messages