I need help with the code from a function node

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:

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?

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

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.

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.

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

Try this if it works for you

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

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.

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;

}
2 Likes

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!

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