# Howto: Calculate average energy over last 5 minutes?

**URL:** <https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750>\
**Category:** Questions & Help\
**Tags:** energy\
**Created:** [April 12, 2019, 9:28am UTC](https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750 "2019-04-12T09:28:30Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_a\_r\_c\_o](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/m_a_r_c_o/32/146_2.png) [@M\_a\_r\_c\_o](https://community.homey.app/u/M_a_r_c_o)\
**Post date:** [April 12, 2019, 9:28am UTC](https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750/1 "2019-04-12T09:28:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![M\_a\_r\_c\_o](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/m_a_r_c_o/32/146_2.png) [@M\_a\_r\_c\_o](https://community.homey.app/u/M_a_r_c_o)\
**Post date:** [April 12, 2019, 4:13pm UTC](https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750/2 "2019-04-12T16:13:49Z")

</div>

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](https://apps.athom.com/app/com.insights.aggregate)

---

<div class="post-metadata">

**Author:** ![Osorkon](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/osorkon/32/43026_2.png) [@Osorkon](https://community.homey.app/u/Osorkon)\
**Post date:** [April 12, 2019, 7:13pm UTC](https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750/3 "2019-04-12T19:13:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![M\_a\_r\_c\_o](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/m_a_r_c_o/32/146_2.png) [@M\_a\_r\_c\_o](https://community.homey.app/u/M_a_r_c_o)\
**Post date:** [April 12, 2019, 8:24pm UTC](https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750/4 "2019-04-12T20:24:53Z")

</div>

Then the insight graph is also every 5 minutes.

---

<div class="post-metadata">

**Author:** ![Osorkon](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/osorkon/32/43026_2.png) [@Osorkon](https://community.homey.app/u/Osorkon)\
**Post date:** [April 13, 2019, 6:58am UTC](https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750/5 "2019-04-13T06:58:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TKroon](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tkroon/32/9504_2.png) [@TKroon](https://community.homey.app/u/TKroon)\
**Post date:** [April 24, 2019, 9:37am UTC](https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750/6 "2019-04-24T09:37:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Caseda](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/caseda/32/138695_2.png) [@Caseda](https://community.homey.app/u/Caseda)\
**Post date:** [April 24, 2019, 10:58am UTC](https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750/7 "2019-04-24T10:58:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![M\_a\_r\_c\_o](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/m_a_r_c_o/32/146_2.png) [@M\_a\_r\_c\_o](https://community.homey.app/u/M_a_r_c_o)\
**Post date:** [April 24, 2019, 11:50am UTC](https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750/8 "2019-04-24T11:50:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TKroon](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tkroon/32/9504_2.png) [@TKroon](https://community.homey.app/u/TKroon)\
**Post date:** [April 24, 2019, 2:18pm UTC](https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750/9 "2019-04-24T14:18:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tjar](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tjar/32/61177_2.png) [@Tjar](https://community.homey.app/u/Tjar)\
**Post date:** [July 5, 2023, 6:55am UTC](https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750/10 "2023-07-05T06:55:48Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![FKey](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/fkey/32/14793_2.png) [@FKey](https://community.homey.app/u/FKey)\
**Post date:** [June 30, 2024, 11:09am UTC](https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750/11 "2024-06-30T11:09:59Z")

</div>

Just ran into this app [“Insight Trends”](https://homey.app/en-us/app/com.spkes.insightTrendsReloaded/Insight-Trends-Reloaded/), sounds promising:

> [@](#):
>
> With the help of Homey’s insightful features, you can view all sensor measurements and their historical evolution. Take it a step further and do more than just observe the insights; act accordingly or even glimpse the future a little.
> 
> You can choose an insight (a device and a capacity, such as the outside temperature), as well as a timeframe to analyze.

---

<div class="post-metadata">

**Author:** ![FKey](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/fkey/32/14793_2.png) [@FKey](https://community.homey.app/u/FKey)\
**Post date:** [July 27, 2024, 7:03pm UTC](https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750/12 "2024-07-27T19:03:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![M\_a\_r\_c\_o](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/m_a_r_c_o/32/146_2.png) [@M\_a\_r\_c\_o](https://community.homey.app/u/M_a_r_c_o)\
**Post date:** [July 27, 2024, 7:47pm UTC](https://community.homey.app/t/howto-calculate-average-energy-over-last-5-minutes/11750/13 "2024-07-27T19:47:32Z")

</div>

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):

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/a/c/ac7086babb36fa2fdef7473308ddbaab72a58e0b.jpeg)

Info of the Insight reloaded card

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/a/a/aa2a360e8ba57b97f7154c1693050c380dd002fb.jpeg)

Input graph:

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/4/f/4f36ce159ba568517911c07c9cc2935fbc11c75c.jpeg)

Result graphs

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/6/b/6bdb64e3eae5cc9ed6499cb361040b4cc4a26269.jpeg)
