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?