Usage of statistics

I have been trying for hours to set up an average over the last 24h humidity sensor, but couldn’t figure it out!

I added:

sensor:
  - platform: statistics
    entity_id: sensor.multisensor_bathroom_humidity
    max_age:
      seconds: 20

to my configuration but how can I extract a value from this to use in an automation or on lovelace? Something that says: tha avg humidity in the last 24h was x.

I guess if you change the max age to 24 hours you will get the average of 24 hours.
But I have never tried it to be honest.
I have only used shorter timespans so I’m not sure it works with 24 hours, but I doubt it’s limited.

Still I don’t get how to use it in lovelace for exaple. It does not create a new sensor.

Yeah it does. Have you restarted since creating it?

Also if you give it a name you will be able to find it more easily.

e.g.

sensor:  # <--- this should only appear once in your configuration
  - platform: statistics
    name: Bathroom Humidity Stats
    entity_id: sensor.multisensor_bathroom_humidity
    max_age:
      hours: 24
    sampling_size: 2880 # depending on how often your sensor updates you may need to change this

e.g. The sampling size for a sensor that updates every 30 seconds should be 60*60*24/30 = 2880 + a bit for safety You can check this by looking at the samples attribute for your statistics sensor in developer tools / states and making sure it never counts past 2880.

Having said all of that, this is a better average sensor as it take into account the time the state stays at each value, rather than just averaging all the values:

e.g. If you used the statistics sensor and it stayed at 50% all day except the last value for the day was 0% the average would be 25%. Obviously this is not the correct average for the 24hours . The custom average sensor does not have this issue. Also you don’t need to calculate the max samples for this custom sensor.

2 Likes

Thanks so much this was super exhaustive!

1 Like

You mean if it was at 50% it does not add 50+50+50+50… It just has two values 50 and 0.
That is not good. I had no idea about this.

Correct. It only adds a sample to the average when the state changes, and it takes no account of how long that state existed for.

Yeah not good at all.

So not good that Limych made a better one and kindly shared it.

EDIT: Note that this has been fixed in 2021.12

2 Likes

Hi Folks!

I wanna make an avarage sensor which can examine the last 14 days temperature but in a current time.
So i do not want a simple 14d avarage but the 14d avarage at 10:00, 10:05, 10:10 …etc for all days.

With this sensor i can see not the daily avarage but the avarage for every five minutes back to 14 days.
I hope i am clear, hard to describe what i mean.
I simply do not know what kind of statistic shoud i use.
Please give me an advice.