Moving average for energy dashboard

Hi there,

I would like to request a very nice feature that not many know but can be very useful for indicating if you use more or less energy over time. I think it will not be very complex to add to HA.

In home assistant we have an energy dashboard with insights in daily power consumption. Nice but it can be much more useful with a simple addition.

The feature i would like to request is moving average. Electricity and natural gas consumption is influenced by weather/seasonal effects. These effects can be filterered out off the data by using moving average.

For example:
I download my smart meter monthly consumption values from time to time from slimmemeterportal. From this data i create for every month the average consumption of that month including the 11 months before. I plot these values in a graph. Every plotted point had thus data from a whole year (divided by 12). And you can thus directly see if usage is increasing or decreasing. See the attached image as an example.

You can do this with montly data but also with daily data. The benefit of doing this with daily data is that changes can be seen much faster and you do not have to wait for the month to pass.

These insights give a huge potential for energy savings and users can see the effects of adjustments within a couple of days/weeks.

How is this a feature request

(a) when you have clearly already implemented it; and
(b) there is this Filter - Home Assistant

Well, the graph i showed was from excel.

I looked at the filter. Will try this approach. Not very fond of doing this in the configuration file. Hoped that is would be a little more elegant. But maybe it is not that complicated, i will see.

If I understand the post correctly, this is a moving average over a year. the filter entity mentioned uses recorder, which typically only holds the data for little over a week, and should not be set to keep years. Also there is a warning there for long periods of time, and setting up a sensow would take a year to get a full year cycle. So currently this is more something that advanced graphing tools would do. I assume Grafana could do, but not standard Home Assistant.

Personally this would be of little use to me on account of the solar panels unpredictable behavior and canceling out some of the energy use, and that of charging my electric car. People with a heat pump would also have very much weather influence on account of cold and warm years. But if you don’t have any of those, it might be useful.

This is exactly why you want a moving avarage over a year and not over a week or a month.

All months are included, and also spring, winter etc. Sunny days, rainy days and holidays.

So extremes are more or less canceld out.

If the number is dropping you did something good for your energy bill, if it rising you did something bad

No, that isn’t true. You should compare months with similar months, the way the energy dashboard does. Because a moving average over a year will never account for the fact that summer is less lighting, more airco if you have one, winter is more lighting, maybe an electric heater…, vacation is mostly everything off, … You cannot compare those to the average of one year. Because it will claim you are doing bad in energy intensive months, while that month may be less than the same month last year. And it will always say you are doing great in energy low months, because you compare with averages that include the heavy months.

So, you switch the energy dashboard to year view, ask to compare with previous years: and there you have all you need to compare with last year. A month my month comparison. For me the comparison is hard, because we bought an electric car and solar panels, but that would not be anything different with a rolling average, it would probably be even less insightful when averaged. Because I know when I bought the car, I know the difference it makes. If that car is 33% inside the rolling average, I’m at a loss…
:

There is a huge difference between a moving avarage or moving total and comparing calender years.

I’m not interested in comparing 2022 with 2023 or 2024. That way I only know after a full calender year what has happend this year.

I want to compare the total ammount used between Feb 17 2022 to Feb 16 2023 with the total ammount between Feb 18 2022 to Feb 17 2023 with the total ammount used between Feb 19 2022 to Feb 18 2023 and so on.

In principal this number is a constant. Because in every range every season is included. (yes I know not every year is the same, but seasonal influences should average out more ore less.)

If this number is rising, I am using more energy, when it is declining I am using less energy.

I am totaly not interested in what I used in august or last week. Those numbers can vary a lot. A couple of clouds or more wind wil change everything.

An other thing is that my contract with the energy company is not in sync with calender years.

And comparing every day by hand selecting dates is a pita.

What I want is a possibillty to use the statistical functions that are already in HA on the energy data.

1 Like

I totally agree with your last post Dolleman! This is exactly what i want!

Love this request. Would love to see how much I’ve used the past 12 months.

I would also like this feature built in to Home Assistant.

Copying some notes here in case this helps anyone.
I tried a few different approaches but kept running into the issue that recorder only keeps 10 days of data by default (recommended), and long-term statistics aren’t accessible for other sensors or templating.

E.g. I was hoping this would work (you could combine with a template sensor that divides this value by 365 for a moving average daily cost, but I decided I liked a moving total instead):

- platform: statistics
  name: Total energy cost yearly
  entity_id: sensor.total_energy_cost
  state_characteristic: change
  max_age:
    days: 365

But not only did recorder limit the data but there’s an issue with statistics resetting to 0 and requiring reloading which felt hacky and still prone to error.
For short duration moving total/averages this works well.

If you just want to show a moving total in your dashboard, you can use the statistics card. But this can’t be graphed or used in template sensors:

type: statistic
entity: sensor.total_energy_cost
period:
  rolling_window:
    duration:
      days: 365
stat_type: change

Another potential option I was looking at was using a daily utility_meter, and creating a template sensor that calculates its state based on the utility meter’s last_period attribute. Was looking to be a bit messy implementing deque or similar in a template AND ensuring the values persisted across restarts etc.

Cutting to the chase, I’ve found a stable solution that didn’t end up being that hard:

  1. Install the InfluxDB add-on and configure to record the sensors of interest for an appropriate time period (i.e. 1 year in this case).
  2. Add an InfluxDB sensor:
- platform: influxdb
  host: a0d7b954-influxdb
  username: home-assistant
  password: !secret influx_password
  queries:
    - name: Total energy cost yearly
      value_template: "{{ (states('sensor.total_energy_cost')|float - value|float)|round(2) }}"
      where: '"entity_id" = ''total_energy_cost'' AND time >= now() - 365d'
      group_function: first
      measurement: '"$"'
      unit_of_measurement: $/year

Again, you could just divide by 365 in the above value_template to get a daily moving average instead of total.

Here’s a screenshot of the resulting dashboard I set up (note that some of the sensors have only been available recently so the long-term values aren’t correct until that time has passed):

This is obviously way more work than should be required for something so simple as a long-term moving average sensor. I think building this into Home Assistant in general would be great, not just for the energy dashboard. Until then, if anyone finds a simpler method than the above then please share!

1 Like

Completely agree, something build in would be nice…