Howto: Calculate average energy over last 5 minutes?

I like to calculate the avarage energy level (W) over the last 5 minutes.

What is my goal?
I have solar panels and I want to switch on/off a device when the panels start to produce energy. But when they start the amount is not so high and if someone is in the house and switches stuff on and off then it can make large steps from negative energy to positive and back. T prevent to much flipping i use a timer now, but a much better approch is to use average data over a certain period, to prevent to much switching my device on and off.
Also due to clouds the energy levels can very a lot.

I found the thread how to calculate the average humidity, but that’s a continues average and not an average over the last 5 minuts.

What I thought but cannot find: can I use the history of Insights in a flow card, to calculate an average, max, min, sum, etc. over the last 1, 5, 10, 15, 30, 60 minutes and maybe even hours and day’s? The numbers are there, but how to use them in a flow?

Numbers for example (in W):
Normal use: 200, no one at home (NAS, Homey, cameras, etc).
CV 65
Washing machine 2200
Fohn / vacume cleaner, dishwasher, etc…

Solar full sun: -2400
Solar with cloud -10 till -1200

I did find the Aggregate Insights App in the app store, but that seems not to work with Homey v2.1.1.

https://apps.athom.com/app/com.insights.aggregate

Why you want to calculate an average for the last 5 minutes? Why not the current energy level measured every 5 minutes?

Then the insight graph is also every 5 minutes.

Now seriously, why the average and not the current value?

@Osorkon because the sun could be hidden behind clouds for 4 minutes and 55 seconds, and then, just when you’re using the current value, the sun breaks through to the almost overcast sky… :wink:

@M_a_r_c_o did you find a solution already? I’m looking for the same formula, but to switch the lights on/off with the lux sensor of the Hue motion sensor. I don’t want the current value, because if someone walks by, the lux drops for a second and isn’t reliable. I’d like an average for let’s say a minute or so.

You can catch your first scenario with a countdown (countdown app) and 3 flows.

  1. If energy lowers below a threshold, start timer for 5 minutes (300 seconds).
  2. If energy goes higher again, stop countdown.
  3. If countdown is empty, do what ever

And the other way around is ofcourse also possible.

1 Like

@TKroon No, not yet found it.
I did try the < < group> > app, but for a group of energy only total exist and no selectable period. Maybe for Lux the average exist in the sensors.

To do it manually, I think I need 2 count downs, one for every 15 or 30 seconds, a second one for 5 minutes, make it a loop and use betterlogic to calculate average over an array of numbers (if that is possible).
Another flow to update every 15 seconds the oldest updated value in the array.
Or use homeyscript may be.

@Caseda thanks, that sounds like an acceptable workaround. Still, for example when the threshold is 10, and I’ve got lots of 3’s, and then one spike of 11, the timer resets. But it’s better than nothing.

Well, mathematicaly its not that hard. You could make a moving average. That is:
Variable += (new_value - Variable) * constant;

The constant will be the “avering factor”, in electronics that would be called the RC-time.

Just try it out!