Averaging power in Node-Red

 


I spent WAY more time trying to figure out how to average values a little for my solar array in node red than I should have.   I blame how utterly shitty documentation is today in open source and programming projects.   Note,  this is not JUST the fault of OSS...  multi billion dollar corporations with multi billion dollar software projects also have the worlds shittiest documentation.   I blame CS professors not whipping students and failing them for not doing proper comments and documentation.   Hey programmers,  you SUCK at what you do if you don't comment your code and write documentation.  Yep called all you out.  Come at me Bro!      I also blame that many projects as they get older they make all kinds of breaking changes and never update docs,  or the only docs that are written by someone like me that got all pissed off at how shitty the docs were, wrote how to do something and its now out of date because the project was modified so much it should have been forked, renamed, and released as something different.  But they dont, they call it the same thing.

I LOVE NODE RED,  let's get that out here.   It's honestly the single best, home automation software on the planet and it makes the other open source ones look like low grade dog food.  So dont think this is a bitch-fest hating on node red.   It's not.   It's hating on the lazy devs that dont document things well.

Ok so what am I up to?    my solar array uses  power meters that go over MODBUS to an ESP32 that then runs Tasmota to take that data and shovel it at my MQTT broker.   then node red I use make that data into something readable.    so far so good.     problem is it's all over the place as clouds float by, a bee farts in front of the array, etc...  and I want the data smoothed a little.   Just a little,  like an average of the last 3 reported values.    First I need to store them, so trying to figure that crap out was a nightmare of contradiction.  every link I went to was completely different process than before. meaning everything did not work.   I finally stumbled on the answer.  Below I get the last 3 values,  rotate them and then slap the new value in and then store it.   Then I add them together and divide by 3 to get my moving average.    stupid simple except for the how to keep data alive in Node Red functions.  Now this all resets if you restart node red.  and that is fine.  you can write to disk instead but thats only good if you hate your SSD drive.  


var val1 = context.get('val1')||0;  

var val2 = context.get('val2')||0;

var val3 = context.get('val3')||0;


val1=val2;

val2=val3;

val3=msg.payload;


context.set('val1',val1);

context.set('val2',val2);

context.set('val3',val3);

//node.warn("val1="+val1+" val2="+val2+" val3="+val3);


var sum = (val1 + val2 + val3) / 3;

var newMsg = { payload: sum }


return newMsg;

Comments

Popular Posts