How do I spliy Array of numeric values as separate outputs?

13,625 views
Skip to first unread message

TridiumControls

unread,
Oct 7, 2016, 2:11:12 PM10/7/16
to Node-RED
Hi,

I'm trying to separate an array with values, and create a separate outpout to connect to a dashboard gauge node in node-red. So for each value in the array a separate output to connect to a node-red gauge node.

Here's the array.

10/7/2016, 11:07:46 AM549b508e.efd198msg.payload : array [12][
 "0.34423017501831055",
 
"246.26158142089844",
 
"123.63079071044922",
 
"123.5",
 
"0.015658054500818253",
 
"0.0",
 
"2.2778106537599901E-41",
 
"0.99609375",
 
"2.2574918260272803E-41",
 
"0.4653584361076355",
 
"2.2431985816911672E-41",
 
"0.26609039306640625"
]

Thanks.

Julian Knight

unread,
Oct 7, 2016, 2:50:30 PM10/7/16
to Node-RED
Not entirely certain what you are asking for.

If you want each of those to produce a separate message downstream, a simple function node with something like:

msg.payload.forEach(function(element) {
    node
.send({"payload": element});
});

Should do it. You could also create an array of output messages and send them all with "return [[outArray]];" but I suspect the former is a little more memory efficient.

TridiumControls

unread,
Oct 7, 2016, 3:46:39 PM10/7/16
to Node-RED
Thx for the reply, I found a node on flows.node-red.com that accomplishes to split the array into separate messages, your method works too. Now that I have the separate messages, how do I send the separate message into a separate output.

Inside the Function node, towards the bottom there is a "Outputs" area that you can specify how many gray nibs get created.

See attached screen shot. Essentially Im trying to get the separated messages, into its own gauge dashboard.

steve rickus

unread,
Oct 7, 2016, 4:27:38 PM10/7/16
to Node-RED

It sounded to me like the input array of 12 numbers represent different measurements, each of which needs to be displayed on its own gauge...

If the number of input numbers in the array is fixed, you can use a split node to put each number in its own msg, and then use a switch node to route it by the original index value (using the Property msg.parts.index):


Alternatively, you can create a special function node with 12 outputs, passing an array of 12 msg objects. In this case, the array of nus is directly mapped to the "array" of outputs, index 0 -> output 1, index 1 -> output 2, etc.


If this is what you are looking for, here is some code that can do it:

var msg12 = msg.payload.map(function (p) {
    return {payload: p};
});
return msg12;


--
Steve
Reply all
Reply to author
Forward
0 new messages