# I need help with the code from a function node

**URL:** <https://discourse.nodered.org/t/i-need-help-with-the-code-from-a-function-node/58160>\
**Category:** General\
**Tags:** function-node\
**Created:** [11 February 2022 15:39 UTC](https://discourse.nodered.org/t/i-need-help-with-the-code-from-a-function-node/58160 "2022-02-11T15:39:48Z")\
**Posts on this page:** 8\
**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 15:39 UTC](https://discourse.nodered.org/t/i-need-help-with-the-code-from-a-function-node/58160/1 "2022-02-11T15:39:49Z")

</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:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [11 February 2022 16:25 UTC](https://discourse.nodered.org/t/i-need-help-with-the-code-from-a-function-node/58160/2 "2022-02-11T16:25:58Z")

</div>

`var data = context.get(['count','time','spannung','strom'])||0;`  
Possibly you shouldnt assign 0 to `data` if context.get is not truthy but an array `|| [0,0,0,0]`

`if(count==5){` better check for `if (count >= 5){` just in case

_untested_

---

<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:** [12 February 2022 09:47 UTC](https://discourse.nodered.org/t/i-need-help-with-the-code-from-a-function-node/58160/3 "2022-02-12T09:47:21Z")

</div>

> [@UnborN](#):
>
> Possibly you shouldnt assign 0 to `data` if context.get is not truthy but an array `|| [0,0,0,0]`

Unfortunately this did also not work.

I tried just using context.get and context.set ones for every variable and now it does work!

But I really wonder what I did wrong.

---

<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:** [12 February 2022 10:02 UTC](https://discourse.nodered.org/t/i-need-help-with-the-code-from-a-function-node/58160/4 "2022-02-12T10:02:58Z")

</div>

> [@nodenoob](#):
>
> But I really wonder what I did wrong.

You can sprinkle `node.warn()` statements, showing appropriate data, through your function so you can follow the flow and see what values variables are set to as it goes through.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [12 February 2022 10:26 UTC](https://discourse.nodered.org/t/i-need-help-with-the-code-from-a-function-node/58160/5 "2022-02-12T10:26:18Z")

</div>

Me too i would make each get set seperate to read it more easily

Try this if it works for you

```auto
[{"id":"4b1aa83c30695bb7","type":"function","z":"54efb553244c241f","name":"Average","func":"var count = context.get('count') || 0;\nvar spannung = context.get('spannung') || 0;\nvar strom = context.get('strom') || 0;\n\n// add\ncount += 1;\nspannung += msg.payload.Spannung;\nstrom += msg.payload.Strom;\n\n// set\ncontext.set('count', count);\ncontext.set('spannung', spannung);\ncontext.set('strom', strom);\n\nif (count >= 5) {\n\n spannung = spannung / 5;\n strom = strom / 5;\n\n // prepare msg.payload\n msg.payload = {\n Spannung: spannung,\n Strom: strom,\n time: Date.now(),\n Geraet: \"WR1\"\n }\n\n //reset\n context.set('count', 0);\n context.set('spannung', 0);\n context.set('strom', 0);\n\n return msg;\n\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":1000,"wires":[["b4ecf2c64f90cbc4"]]},{"id":"7778c2f4c97316b2","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":1000,"wires":[["a678437527317254"]]},{"id":"a678437527317254","type":"function","z":"54efb553244c241f","name":"Random data","func":"msg.payload = {\n Spannung: Math.random() * 20 + 220,\n Strom: Math.random() * 10 + 3 \n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":1000,"wires":[["4b1aa83c30695bb7","9d16310eb6981937"]]},{"id":"9d16310eb6981937","type":"debug","z":"54efb553244c241f","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":520,"y":920,"wires":[]},{"id":"b4ecf2c64f90cbc4","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":860,"y":1000,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [14 February 2022 15:23 UTC](https://discourse.nodered.org/t/i-need-help-with-the-code-from-a-function-node/58160/6 "2022-02-14T15:23:53Z")

</div>

You are creating an array of strings not numbers with your `context.get`.

Your output results in msg.payload[0].Spannung etc., is this the same as the input? If not, and assuming that you meant the output to be an array.

```auto
let [count, time, spannung, strom] = context.get("data") || [0, 0, 0, 0]

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('data',[0, 0, 0, 0]);

    return msg;

} else{
    context.set('data',[count, time, spannung, strom]);

    return null;

}

```

---

<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:** [19 February 2022 09:08 UTC](https://discourse.nodered.org/t/i-need-help-with-the-code-from-a-function-node/58160/7 "2022-02-19T09:08:38Z")

</div>

Sorry for the late answer, I was busy in the last week.  
Thanks that was the mistake, in hindsight it is totally clear why it did not work.  
Thanks!

---

<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:** [5 March 2022 09:09 UTC](https://discourse.nodered.org/t/i-need-help-with-the-code-from-a-function-node/58160/8 "2022-03-05T09:09:31Z")

</div>

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