# Persistence calculation of energy usage

**URL:** <https://community.openhab.org/t/persistence-calculation-of-energy-usage/88181>\
**Category:** Scripts & Rules\
**Created:** [December 21, 2019, 7:10pm UTC](https://community.openhab.org/t/persistence-calculation-of-energy-usage/88181 "2019-12-21T19:10:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![derSchweiger](https://community.openhab.org/user_avatar/community.openhab.org/derschweiger/32/80097_2.png) [@derSchweiger](https://community.openhab.org/u/derSchweiger)\
**Post date:** [December 21, 2019, 7:10pm UTC](https://community.openhab.org/t/persistence-calculation-of-energy-usage/88181/1 "2019-12-21T19:10:55Z")

</div>

- Platform information:

- Issue of the topic: I try to calculcate the hourly average power consumption of my HS110 socket. Therefore I save every 15 minutes the current power usage into my MariaDB database. So far so good. Now I try to calculate the hourly average consumption with a rule that looks like this:

\*_In reality the rule is a little bit longer. For reasons of comprehension I removed all unnecessary parts._

Let’s have a look on the tracked 15 minutes results:  
2019-12-21 08:45:00.902 - 0 Watt  
2019-12-21 09:00:01.029 - 0 Watt   
2019-12-21 09:15:00.211 - 0 Watt  
2019-12-21 09:30:00.339 - 0 Watt  
2019-12-21 09:45:00.481 - 207.266 Watt  
2019-12-21 10:00:00.593 - 211.374 Watt

And the result of the rule looks like:  
2019-12-21 10:00:00.413 - (-131.0962113148697)  
How is that possible? The rule should have calculcated (0+0+0+207,266)/4= **51,82**

Do I get something wrong? Is there an obvious mistake somewhere?  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [December 21, 2019, 7:40pm UTC](https://community.openhab.org/t/persistence-calculation-of-energy-usage/88181/2 "2019-12-21T19:40:28Z")

</div>

> [@derSchweiger](#):
>
> And the result of the rule looks like:  
> 2019-12-21 10:00:00.413 - (-131.0962113148697)

10:00:00:413 is before …

> [@derSchweiger](#):
>
> 2019-12-21 10:00:00.593 - 211.374 Watt

You need to choose your rule trigger with care, and may need to allow some time for persistence to do its work before you can read it back.

---

<div class="post-metadata">

**Author:** ![derSchweiger](https://community.openhab.org/user_avatar/community.openhab.org/derschweiger/32/80097_2.png) [@derSchweiger](https://community.openhab.org/u/derSchweiger)\
**Post date:** [December 21, 2019, 7:55pm UTC](https://community.openhab.org/t/persistence-calculation-of-energy-usage/88181/3 "2019-12-21T19:55:49Z")

</div>

Hey,

yeah - my rule triggered before the variable for 10am was written to the database. But that should be no problem?  
So i assume that my rule triggered around 10:00:00:00-10:00:00:412 am. Therefore it should have been used the variables within the last 60 minutes:  
2019-12-21 09:00:01.029 - 0 Watt  
2019-12-21 09:15:00.211 - 0 Watt  
2019-12-21 09:30:00.339 - 0 Watt  
2019-12-21 09:45:00.481 - 207.266 Watt

And the result should be (0+0+0+207,266)/4= **51,82**

But the actual result is completely wrong -131.0962113148697. How is it possible that it calculated a negative average?

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [December 21, 2019, 8:25pm UTC](https://community.openhab.org/t/persistence-calculation-of-energy-usage/88181/4 "2019-12-21T20:25:33Z")

</div>

Confident that your averageSince is addressing the correct database?

This thread contains some interesting considerations

> [@\[Solved\] Weird result of averageSince](https://community.openhab.org/t/solved-weird-result-of-averagesince/75152/9):
>
> I’m wondering if this is a timezone issue. The calculation itself is ok, depends on timestamps (since it is in fact a weighted average depending on the time-difference of the stored values). As last value the current item’s state is included with the current timestamp. If this value has another timezone than the stores valued and that is not properly considered, negative averages could be calculated (because now-first\<0).

---

<div class="post-metadata">

**Author:** ![derSchweiger](https://community.openhab.org/user_avatar/community.openhab.org/derschweiger/32/80097_2.png) [@derSchweiger](https://community.openhab.org/u/derSchweiger)\
**Post date:** [May 27, 2020, 6:01am UTC](https://community.openhab.org/t/persistence-calculation-of-energy-usage/88181/5 "2020-05-27T06:01:53Z")

</div>

Thank you! After some long time I came back to this problem and of course it was a timezone issue. I’m running openhab on a Synology Docker and changed the timezone to CET. Since that change it’s working without any problems 🙂

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [May 27, 2020, 10:23am UTC](https://community.openhab.org/t/persistence-calculation-of-energy-usage/88181/6 "2020-05-27T10:23:42Z")

</div>

Can you expand a little, for interest? Your MariaDB was operating in a different timezone to openHAB, I guess?

---

<div class="post-metadata">

**Author:** ![derSchweiger](https://community.openhab.org/user_avatar/community.openhab.org/derschweiger/32/80097_2.png) [@derSchweiger](https://community.openhab.org/u/derSchweiger)\
**Post date:** [August 2, 2020, 5:38pm UTC](https://community.openhab.org/t/persistence-calculation-of-energy-usage/88181/7 "2020-08-02T17:38:58Z")

</div>

Sorry for the late response. I am operating openhab in a docker container on my Synology DS. Therefore, I cannot set the timezone as I’m used to it in a “normal” linux system. So at the beginning it was too complicated for me to change the timezone within the docker container to Standard European Time (CEST).  
Fortunately there is an environment variable you can set to change it. I have written a little how-to for this case (in german). For other who have the same problem, you can have a look to this: [openHAB 2: Synology Docker Timezone ändern – SchweigersTechBlog](https://schweigerstechblog.de/openhab-2-synology-docker-timezone-aendern/)  
If it’s not allowed to post a link in this forum, just delete it please.
