Array values median

Hi there,
From fiew days i'm trying to make a function witch
collect value in a array, from a sensore comming every hour
and to find the middle value everytime when new number is aplied
when the counter reach 24 to start from 0, replacing the old values in the array

Sorry for the nube question, i'v red many of the posts here in the forum but i couldn't adapt any. please can you help me.

Do you need exactly that. The built-in smooth node will do a rolling average of the last 24 samples, or a low pass filter, which for process averaging would normally be considered a better approach. If you only want to get an output every 24 samples then you could use a Delay node in Rate Limit mode, after the smooth node, to effectively achieve that.

Hi Colin,
Thankyou for your replay, yes i need axactly this and i tryed with the smooth node as you said and its work good. Sorry for this i dont know good all the node functions, but i'm learning. Can you ask you to provide mi just a simple example using array in function just to set some numbers according to the qounter, this will help me to understand how to manage array variables in the functuion node.
thank you.

If the smooth node works well, as you said then I don't understand what you want.

yes its work good, i wanted to learn how to work with array variables, but its not important i'll manage it.

W3schools is a good site for javascript help. JavaScript Tutorial

1 Like

If you really want median - then I would push a new value to an array every hour - then use an array sort - after 24 - take the 12th (or 13th) one - and reset the array.

Yes i tryed exactly this but i faced some problems, like after i zeroed the counter i couldnt make the array to start from 0 and replace the old values. I'm verry new in node red and java script. If you can provide me simple sketch it will help me how to manage array variables in node red.

Here are two examples, one using function node, and one using change node and JSONata. They both store the last 24 readings and output the median value.

[{"id":"d914b791.136a4","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$ceil($random()*100)","payloadType":"jsonata","x":120,"y":2580,"wires":[["c2187bd9.e074e8","9b77f2bf.9941a"]]},{"id":"c2187bd9.e074e8","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"medArray","pt":"flow","to":"$append([payload],$flowContext(\"medArray\"))[[0..23]]","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$sort(\t   $flowContext(\"medArray\"),\t   function($l, $r) {\t  $l > $r\t}\t)[$floor($count($flowContext(\"medArray\"))/2)]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":2560,"wires":[["30cdf41d.35342c"]]},{"id":"30cdf41d.35342c","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":2520,"wires":[]},{"id":"3b163090.9f8058","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":2580,"wires":[]},{"id":"9b77f2bf.9941a","type":"function","z":"5a245aa1.510164","name":"","func":"let fMedArray = (flow.get(\"fMedArray\") || []);\nfMedArray.unshift(msg.payload)\nfMedArray= fMedArray.slice(0,24);\nflow.set(\"fMedArray\", fMedArray.concat([]));\nmsg.payload = fMedArray.sort((a, b) => a - b)[Math.floor(fMedArray.length/2)];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":280,"y":2620,"wires":[["3b163090.9f8058"]]}]

Do you really want the median, not the mean?

Thankyou, its a good example!

You gave me the answer in the first post, so i used it and it work (smoot node). I continued the post cos i wanted to learn how its work with arrays in node red. So i cheked the site wich you gave me and i will take some of the courses. And for now i will examin the example from



So you don't want the median? You want the mean, which is what the smooth node gives.

sorry Colin im not verry inside of the programing terminology for me

average, middle, medium, mid, mean, median

are same. You can explane me what is the differenc in the programist side if you want. I resolved the case with your answer and i neade one working skech using array how to adres the different levels and how to use it in general in node red.

Median and Mean are not programming terms, they are statistics and are not the same thing, though in the case of sensor noise there is probably very little difference.

1 Like

i will chek

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.