Statistics sensor sample once per day, max value

I have a sensor value, daily accumulated power consumption, that grows during the day.

I would like to sample the max value per day (e.g. the last just before midnight) and calculate the average over a period of days.

I am trying to use the statistics sensor for this but I don’t really grasp from the docs how I can sample just once.

Anyone got any ideas?

same here. Did you find a solution?



I did a template sensor to trigger like this:

  - trigger:
      - platform: time
        at: "23:59:55"
      - name: "Electricity Consumption (max)"
        unique_id: electricity_consumption_max
        state: >
          {{ states('sensor.accumulated_consumption_XYZ') }}
        state_class: measurement
        unit_of_measurement: "kWh"

Then I used a statistics sensor to calculate mean value of the above sensor:

  - platform: statistics
    name: "Electricity Consumption (mean 7-days)"
    unique_id: electricity_consumption_mean
    entity_id: sensor.electricity_consumption_max
    state_characteristic: mean
    sampling_size: 7
      days: 7

Not very elegant, but it seems to work.

Thanks for sharing, Henrik! I’m trying to do something similar by recording a rolling 7 day sum of the rainfall in my area based on a sensor I have that tells me yesterday’s rainfall.

I set up the template sensor that updates once per day, and the statistics sensor that does a sum of the previous 7 days’ data. However, I found that the template sensor updates every time I restart HA or reload the configuration, artificially inflating the statistics sensor.

I was wondering if you ran into this problem and had a fix? I’m guessing it’s less obvious in an average of max values, but presumably you’re also getting multiple entries due to restarts.

Does it update during restart even though it should trigger at 23:59:55?

Yes. Even though I have the time trigger set for the template, the value updates on restart not just at the stated time:

  - trigger:
      - platform: time
        at: "5:57:55"
      - name: "Precipitation yesterday"
        unique_id: precipitation_yesterday
        state: >
          {{ states('sensor.weatherflow_precipitation_yesterday_rain_checked') }}
        state_class: measurement
        unit_of_measurement: "in"

@solstyce9 i have just installed a rain meter and I stumbled upon using Utility Meter integration. I have it set up to produce hourly, daily weekly and monthly sum of rain events. Still testing but it looks good. I guess you can then run statistics from there?

Edit: ddoooohhhh and to your answer the original problem my numbers survive HA reboot.

@JulianDH oh great I will also try this one ASAP.

Interesting, thanks for the pointer! I took a look and it looks like it will do fixed period sums. Unfortunately, I’m looking for a rolling 7 day window - if there hasn’t been enough rain the last 7 days it’s time to water - so that won’t work for my use case.

But a very cool integration. That’s the best part about Home Assistant - it’s constantly evolving and improving so there’s always more to learn!

@solstyce9 Can you run a statistic on each proceeding day. The data is there but it needs to be calculated. So much easier in Excel! At some point, for me, this would be goood to solve

I went with a command line approach and storing data in a text file.

It’s a hack, but it works.

1 Like