Pass parameter to a node

Hi,

I'm currently working on the Statistics Node, but I think my question is not Statistics-specific.

To make the node work, the node has a property called 'dataSetSize'. As long as I set this value directly on the node everything is working properly but, I need to set this property from my flow. How can i do this?

Statistics

Which node exactly is this? What module did you install to get it?

If the node supports you providing that value via a message property then it's sidebar help should tell you.

Node is named "Statistics" and I find it a bit hard to control :slight_smile:

But what module did you install to get that node? Probably node-red-contrib-SOMETHING

Oh, sorry
node-red-contrib-statistics (node)

I don't see any suggestion in the node description that you can pass that property in a message.

Neither can I Colin.

My goal is to calculate the Mean and the StandardDeviation of the incoming payload where the array-length can by anything. Maybe there is a better way to do this in javascript? Any suggestions?

I haven't used that node, but I think the description says that if you specify clear when you pass the array then it will clear any existing data, add in the new array of values, and calculate the answer for you.

Yes, I checked that msg.topic = clear will celar the register. But msg.topic is also necessary to specify the kind of math you are doing, for example standardDeviation. Then things gets messy....

Well,
It is probably easier to do it all in Javascript. Something like this:

var v = 0;
var diffarr = ;
var mean;
var diffmean;

//mean value
for (i=0; i < msg.valuearr.length;i++) {
v = v + msg.valuearr[i];
}

mean = v / msg.valuearr.length;

//Standard deviation
for (i=0; i < msg.valuearr.length;i++) {
v= Math.pow(msg.valuearr[i] - mean,2);
diffarr.push(v);
}

v=0;
for (i=0; i < diffarr.length;i++) {
v = v + diffarr[i];
}

diffmean = v / diffarr.length;

msg.stdev = Math.sqrt(diffmean);
msg.mean = mean;
return msg

I just installed that node and it seems to work fine.

I understood that the whole input array will be used in the calculation when the parameter Data set size is left as zero.

maximum number of data elements to store. 0 for unlimited.

The trick (as you already mentioned) is to set the topic of the message to the function to be applied, in your case "mean" or "standardDeviation"

a-01

Edit: After playing a while with the node I understand what you mean. Indeed, it is not intuitive controlling the behaviour of the node. Still trying to get my head round.

An alternative is to install the underline statistics library from NPM and use it inside a function node like explained on Node-RED documentation.

var ss = global.get('simple_statistics');
msg.mean = ss.mean(msg.payload);
msg.max = ss.max(msg.payload);
msg.standardDeviation = ss.standardDeviation(msg.payload);
return msg;

ss-01

Following line has been added to settings.js under functionGlobalContext property:

simple_statistics:require("simple-statistics"),

1 Like

Thank you for your support guys!