# 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â€¦

@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!

Just ran into this app â€śInsight Trendsâ€ť, sounds promising:

@M_a_r_c_o did you find a good solution for your avarage energy level?

Well, I did not investigate further or implemented this, the waf is to unpredicatable

But I did something which looks a bit like this. Using the apps Insight Trends Reloaded and the app Temporary Variables. And results in 3 new Insight graphs (numeric logics).
Danger is with 5 minutes that you can have a flow which disables often.

Flow (every day at 23:57 executed):

Info of the Insight reloaded card

Input graph:

Result graphs