#Get a 7 Day Average of Energy Consumption from a Sensor that Resets?

1 messages · Page 1 of 1 (latest)

latent pivot
#

I decided to make a statistics page after discovering my new plugs have energy monitoring.

Here's what I've set up:
• Gauge that shows how much power it is currently drawing at any given time.
• Gauge that shows how much power has been drawn that day.
• Graph that shows the power draw history of the 5 most recent days.

All this is pictured in my attachment.

What I'd like to set up

• Gauge that measures the day total of the last 7 days. I.e. if the total power draw in kWh for the last 7 days are .375, .375, .395, .390, .355, .395, and .425, the gague would read .387 1kWh.

What I've tried:
I discovered the statistics helper, but I didn't understand the options first time around. (The documentation that the ? took me to wasn't super helpful.) And on my first attempt at getting the change from 7 days ago till now, I was getting a negative number.

Images:
What I've set up (left) and what I want (right)


Solved

For anyone finding this thread in the future, I put the solution at the very end.

arctic ember
#

For starter, have a read on power and energy and stop mixing the two 🙂

And do you use an energy sensor that is ever increasing? That case you could use a statistics sensor of type change and set the max age to 7 days. That would give you the total energy over the 7 days. Then you would only need to divide it by 7 with a template.

latent pivot
# arctic ember For starter, have a read on power and energy and stop mixing the two 🙂 And do ...

I use plugs from Kasa, and in the Kasa app I can see current power, today's total consumtion, ... god dammit. I'm looking and seeing that the app provides a 7 day daily average, as well as a total consumption from the past 7 days. Each of these stats is a different entity in Home Assistant. I probably just have to use the right entity. (if that's the case, I'm gonna be mad that I didn't just see that beforehand.)

arctic ember
#

just go to the device view and look at all the entities then 😄

latent pivot
#

Weirdly enough, there's a sensor for the total energy consumption for the month, but not the past 7 days.

arctic ember
#

I see a total consumption. It that never resets, you have make the statistics sensor I mention above

latent pivot
arctic ember
#

yeah, thing is that average linear over the day sensor would not work. As it would everage over every sample during the day, not just the end result (= actual energy consumption of the day)

#

but if you calculate the difference (change) between now and 7 days back, you know the total energy consumed in those 7 days. Dividing it by 7 would give the average dayly consumption

latent pivot
#

Ah, that makes sense.

latent pivot
#

Took a break to get some yard work done.

I tried it and I ended up getting a negative number (before deviding by 7) or a number that simply cannot be correct. I thought maybe there's just not enough data, but the entities for it were created on July 11th (14 days ago).

Another weird thing is that I went to developer tools > states > and pulled up the states for all the entities, and this month's consumption is showing a higher number than total consumption. Only possible explanation I could think of is somehow the state got reset or maybe the plug was reset while being set up again? I don't remember setting up any of my plugs more than once, though. I'll wait it out and see if the issue fixes itself.


I set it up as if I wasn't having errors. I set up the helper to take the change in total power from now and 168 hours ago. (Days isn't an option, so 24 hours a day multiplied by 7 days is 168 hours.) Then I set up the template sensor like this:

`- sensor:

  • name: "Drink Fridge Plug Average"
    unique_id: drinkfridgeplugaverage
    icon: mdi:house-lightning-bolt
    state: >-
    {% if ( states('sensor.drink_fridge_consumption_over_7_days') | float ) <= 0 %}
    0
    {% else %}
    {{ (( states('sensor.drink_fridge_consumption_over_7_days') | float ) / 7 ) | round(3) }}
    {% endif %}
    attributes:
    friendly_name: "Drink Fridge Plug Average"`

Used if because seeing the negative numbers was bothering me. I'm hoping that this all fixes itself over the next few days.

latent pivot
#

I guess the big question here is: why would the statistics helper using the change characteristic for a total that only goes up result in a negative number?

untold hill
#

Share your data if you'd like someone to help analyze it.

latent pivot
#

Here's something interesting. I know the second dip lines up with a power outage, but I was away for the first dip, so I can't verify if that was a power outage. Still, why would it reset during a power outage? And is there a way to prevent a sudden shutdown from the data being reset?

untold hill
#

Many energy sensors periodically reset their value.

#

Utility meter helpers can mitigate that.

latent pivot
#

I'll look into it

arctic ember
#

yeah, if the sensor from the device resets, then you can feed it into a utility meter that will never reset. From that you can grab the 7 day delta.

latent pivot
#

I figured it out!

The actual problem: If your plug resets to 0 for any reason, subtracting its total from 7 days ago from its current toal yields a negative number, so we need to set it up so that the total goes up no matter what.

Step 1: Set up a Utility Meter. This helper can keep a total count of a measurement that keeps going up even if what it's tracking resets. In our case, I set up a Utility Meter Helper

• I name it Drink Fridge Total Consumption Meter using Drink Fridge Total Consumption as the input sensor.
• I leave Meter Reset Cycle as No Cycle because I don't want the Utility Meter resetting to 0.
Meter Reset Offset is kept at 0. It's irrelevant because I don't want the Utility Meter resetting to 0.
Supported Tariffs are irrelevant as well.
• I leave Net Consumption off as we never want this Utility Meter Helper to go down.
Delta Values remains off as well because the input sensor is giving a total, not a delta. Periodically Resetting set to On as a power outage or unplugging the plug can reset the input sensor to 0.
• I don't think Source Always Visible matters since nothing will be added when the input sensor is not available either way.

#

Step 2: Set up a Statistics Helper: This helper, to my understanding, does some math the history of another sensor or entity with a numeric state and gives us a result based on what we want.

• For our example, I name it Drink Fridge Consumption Over 7 Days and use the Utility Meter Helper we just made, Drink Fridge Total Consumption Meter, as the input sensor.

#

• Make sure you change the Statistical Characteristic from "Average Linear" to "Change". It doesn't let you change this later, so if you mess up, just cancel and start from the beginning of Step 2.
(Change takes the oldest sample, and subtracts is from the newest sample. (New - Old = Change)

#

If you leave this alone, this will also just keep going up and up as it will subtract the absolute oldest sample.

• Set Max Age to 168 hours. (24 hours x 7 days = 168 hours)
Keep last sample seems to yield the same results whether it's on or off.
Percentile is irrelevant since it won't be used for calculating Change.
Precision is personal preference, but I set mine to 3.

#

Step 3: Make a Template Sensor. We will use this to get the current reading of the Statistics Helper and divide it by 7.

For extra credit, I did use an if statement to prevent a negative number showing up. Simply put, "If Statistic Helper >= 0, return 0. Else, return (Statistics Helper / 7)"

For anyone who prefers to copy and paste, Here's what I put so you can simply change what you need to change.

`- sensor:

  • name: "Drink Fridge Plug Average"
    unique_id: drinkfridgeplugaverage
    icon: mdi:home-lightning-bolt
    state: >-
    {% if ( states('sensor.drink_fridge_consumption_over_7_days') | float ) <= 0 %}
    0
    {% else %}
    {{ (( states('sensor.drink_fridge_consumption_over_7_days') | float ) / 7 ) | round(3) }}
    {% endif %}
    attributes:
    friendly_name: "Drink Fridge Plug Average"
    unit_of_measurement: kWh`
#

(I had to add the "| float" parts because for some reason, the sensor was being interpreted as a string.)

latent pivot
#

Extra Credit: Placing it in your Dashboard

My visual example shows it being part of an overall item, but you can make just the singular gauge with the gauge card. My example uses a combination of gauge, grid, custom:stack-in-card, statistics graph, and card-mod to pull off the exact look I want. There are excellent guides on stack-in card on YouTube. If you just want the gauge, pick the gauge card from the card selector and choose your new template sensor as the entity. Thanks @arctic ember and @untold hill for helping.