# Array values median

**URL:** <https://discourse.nodered.org/t/array-values-median/42957>\
**Category:** General\
**Created:** [21 March 2021 13:03 UTC](https://discourse.nodered.org/t/array-values-median/42957 "2021-03-21T13:03:05Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgolemanov](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mgolemanov](https://discourse.nodered.org/u/mgolemanov)\
**Post date:** [21 March 2021 13:03 UTC](https://discourse.nodered.org/t/array-values-median/42957/1 "2021-03-21T13:03:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 March 2021 13:33 UTC](https://discourse.nodered.org/t/array-values-median/42957/2 "2021-03-21T13:33:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mgolemanov](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mgolemanov](https://discourse.nodered.org/u/mgolemanov)\
**Post date:** [21 March 2021 13:58 UTC](https://discourse.nodered.org/t/array-values-median/42957/3 "2021-03-21T13:58:55Z")

</div>

> [@Colin](#):
>
> smooth

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 March 2021 14:11 UTC](https://discourse.nodered.org/t/array-values-median/42957/4 "2021-03-21T14:11:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mgolemanov](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mgolemanov](https://discourse.nodered.org/u/mgolemanov)\
**Post date:** [21 March 2021 14:18 UTC](https://discourse.nodered.org/t/array-values-median/42957/5 "2021-03-21T14:18:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 March 2021 14:26 UTC](https://discourse.nodered.org/t/array-values-median/42957/6 "2021-03-21T14:26:10Z")

</div>

W3schools is a good site for javascript help. [JavaScript Tutorial](https://www.w3schools.com/js/default.asp)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [21 March 2021 15:16 UTC](https://discourse.nodered.org/t/array-values-median/42957/7 "2021-03-21T15:16:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mgolemanov](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mgolemanov](https://discourse.nodered.org/u/mgolemanov)\
**Post date:** [21 March 2021 16:47 UTC](https://discourse.nodered.org/t/array-values-median/42957/8 "2021-03-21T16:47:07Z")

</div>

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.  
Thankyou

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 March 2021 17:06 UTC](https://discourse.nodered.org/t/array-values-median/42957/9 "2021-03-21T17:06:48Z")

</div>

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.

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 March 2021 17:20 UTC](https://discourse.nodered.org/t/array-values-median/42957/10 "2021-03-21T17:20:58Z")

</div>

Do you really want the median, not the mean?

---

<div class="post-metadata">

**Author:** ![mgolemanov](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mgolemanov](https://discourse.nodered.org/u/mgolemanov)\
**Post date:** [21 March 2021 17:24 UTC](https://discourse.nodered.org/t/array-values-median/42957/11 "2021-03-21T17:24:43Z")

</div>

Thankyou, its a good example!

---

<div class="post-metadata">

**Author:** ![mgolemanov](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mgolemanov](https://discourse.nodered.org/u/mgolemanov)\
**Post date:** [21 March 2021 17:29 UTC](https://discourse.nodered.org/t/array-values-median/42957/12 "2021-03-21T17:29:53Z")

</div>

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

> [E1cid](https://discourse.nodered.org/u/E1cid)

Thankyou

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 March 2021 17:31 UTC](https://discourse.nodered.org/t/array-values-median/42957/13 "2021-03-21T17:31:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mgolemanov](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mgolemanov](https://discourse.nodered.org/u/mgolemanov)\
**Post date:** [21 March 2021 17:41 UTC](https://discourse.nodered.org/t/array-values-median/42957/14 "2021-03-21T17:41:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 March 2021 17:45 UTC](https://discourse.nodered.org/t/array-values-median/42957/15 "2021-03-21T17:45:57Z")

</div>

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.  
[https://www.answerminer.com/blog/mean-vs-median](https://www.answerminer.com/blog/mean-vs-median)

---

<div class="post-metadata">

**Author:** ![mgolemanov](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mgolemanov](https://discourse.nodered.org/u/mgolemanov)\
**Post date:** [21 March 2021 17:57 UTC](https://discourse.nodered.org/t/array-values-median/42957/16 "2021-03-21T17:57:32Z")

</div>

i will chek  
thankyou

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 April 2021 17:58 UTC](https://discourse.nodered.org/t/array-values-median/42957/17 "2021-04-04T17:58:12Z")

</div>

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