# Building the average of the last 5 msg and send it?

**URL:** <https://discourse.nodered.org/t/building-the-average-of-the-last-5-msg-and-send-it/58156>\
**Category:** General\
**Tags:** function-node\
**Created:** [11 February 2022 14:44 UTC](https://discourse.nodered.org/t/building-the-average-of-the-last-5-msg-and-send-it/58156 "2022-02-11T14:44:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nodenoob](https://avatars.discourse-cdn.com/v4/letter/n/b5a626/32.png) [@nodenoob](https://discourse.nodered.org/u/nodenoob)\
**Post date:** [11 February 2022 14:44 UTC](https://discourse.nodered.org/t/building-the-average-of-the-last-5-msg-and-send-it/58156/1 "2022-02-11T14:44:31Z")

</div>

So I tried programming a function node which builds the average of the last 5 sent values and then sends it away.(to a database, because of that I save the first timestamp)  
But it just wont send away anything. Did I use the context wrong? I dont know what my mistake is.  
Here is the code:

```auto

var data = context.get(['count','time','spannung','strom'])||0;

var count = data[0];
var time=data[1];
var spannung=data[2];
var strom=data[3];

count+=1;
spannung += msg.payload.Spannung;
strom += msg.payload.Strom;
    
if(time==0){
    time= Date.now();
}

if(count==5){

    spannung = spannung/5;
    strom = strom/5;

    msg.payload = [{
        Spannung: spannung,
        Strom: strom,
        time: time
    },
    {
        Geraet: "WR1"
    }];
    
    context.set(['count','time','spannung','strom'],[0,0,0,0]);
    return msg;
    
} else{
    context.set(['count','time','spannung','strom'],[count,time,spannung,strom]);
    return null;
}

```

Does anyone of you know what my mistake is?

---

<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:** [11 February 2022 16:02 UTC](https://discourse.nodered.org/t/building-the-average-of-the-last-5-msg-and-send-it/58156/2 "2022-02-11T16:02:50Z")

</div>

I think your context.get is the issue as when undefined it's default does not work.

But if you want the running average of last 5 readings i would store them in an array and limit it to 5 items, then average the array each run e.g.

```auto
[{"id":"7ec23cce.84de5c","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"Spannung\":5,\"Strom\":5}","payloadType":"json","x":140,"y":4400,"wires":[["cc19f93d.a9fd4"]]},{"id":"cc19f93d.a9fd4","type":"function","z":"c791cbc0.84f648","name":"","func":"let data = []\ndata[0] = context.get('spannung') || [];\ndata[1] = context.get('strom') || [];\n\nlet spannung=data[0];\nlet strom=data[1]\n\nspannung.unshift(msg.payload.Spannung);\nstrom.unshift(msg.payload.Strom);\n\nspannung = spannung.slice(0,5); \nstrom = strom.slice(0,5);\n\nmsg.payload = [{\n Spannung: spannung.reduce((a,e) => a+e)/spannung.length,\n Strom: strom.reduce((a,e) => a+e)/strom.length,\n time: Date.now()\n },\n {\n Geraet: \"WR1\"\n }];\n \ncontext.set(['spannung','strom'],[spannung, strom]);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":4400,"wires":[["e40ac006.709d5"]]},{"id":"e40ac006.709d5","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":4400,"wires":[]}]

```

---

<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:** [11 February 2022 16:29 UTC](https://discourse.nodered.org/t/building-the-average-of-the-last-5-msg-and-send-it/58156/3 "2022-02-11T16:29:28Z")

</div>

The [smooth node](https://flows.nodered.org/node/node-red-node-smooth) can do this for you, unless you are treating this as a learning exercise in using javascript in node-red.

---

<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:** [12 April 2022 16:29 UTC](https://discourse.nodered.org/t/building-the-average-of-the-last-5-msg-and-send-it/58156/4 "2022-04-12T16:29:37Z")

</div>

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