Splitting Strings, banging my head against a wall.

181 views
Skip to first unread message

TridiumControls

unread,
Apr 21, 2017, 1:14:45 AM4/21/17
to Node-RED
Hi, 

I recently started playing with node-red and bought a sonoff TH16 switch for temp/humidity monitoring, but want to be able to separate the temperature and humidity from the string, so it to be available as a raw number so that I can send to a dashboard gauge. 

I want to be able to split this message and extractly only the temperature and humdity from it. This is from the debug window.

4/20/2017, 10:11:06 PMnode: 3995ab08.7b23c4tele/sonoff/SENSOR : msg.payload : string[78]
"{"Time":"2017-04-21T06:11:05", "AM2301":{"Temperature":21.5, "Humidity":52.8}}"

Is there a function that I could use to achieve this? Been at this for hours looking up different methods on the web and can't find anything that will work.

Thanks in advance.

Dave C-J

unread,
Apr 21, 2017, 2:41:03 AM4/21/17
to node...@googlegroups.com
Hi, rather than splitting the string, you need to convert it into an object and then you will have access to the parts directly using standard JavaScript for notation. So first feed it through a JSON node, then into a function.

The function will require 2 outputs so set that bottom left.
Then we need to create two output messages from the incoming one.

var m1 = {topic:"Temperature", payload: msg.payload.AM2301.Temperature};
var m2 = {topic:"Humidity", payload: msg.payload.AM2301.Humidity};
return [m1, m2];

Which you can then feed to two ui widgets.

TridiumControls

unread,
Apr 21, 2017, 3:03:59 AM4/21/17
to Node-RED
You are the man.

Exactly what I needed.

Thanks

Dave Harrison

unread,
Apr 28, 2017, 12:27:00 PM4/28/17
to Node-RED
Hi Dave,

I found this useful too - thanks!
Reply all
Reply to author
Forward
0 new messages