Statistics max_age and sampling_size

I’m trying to build a sensor that will report the max power value from an esphome connected outlet over the past 24 hours.

I started with this:

- platform: statistics
  name: "MasterTub Power Max"
  entity_id: sensor.mastertub_power
  state_characteristic: value_max
    hours: 24

Unfortunately, it did not hold the 24 hour max value of the sensor.mastertub_power. For some reason it was well under 24 hours. In the statistics documentation it says:

sampling_size integer (optional, default: 20)
Maximum number of source sensor measurements stored. Be sure to choose a reasonably high number if the limit should be driven by max_age instead.

So I added, “sampling_size: 240” to the sensor. I have no idea what the sampling_size is in relation to time. I was assuming that it might be related to max_age, so I set it to sample 10 times an hour. This is what it looked like today:

So the power spike (caused by the jets being cleaned) started at 3:00 PM the previous day, but then the statistics sensor tossed this max value at around 7:30 AM the next day, obviously well inside a 24 hour window.

Am I wrong in thinking that finding a max and min over a set time period is unnecessarily convoluted here? In any case, it doesn’t work right, and no reading of the statistics documentation clears this up for me.

How often does your sensor update?

i.e. how may times an hour?

Multiply that by 24, that is the minimum number of sample you need for a 24 hour period.

There is an attribute of the statistics sensor called buffer_usage_ratio. You want this to be close to 1 without ever reaching 1. If it reaches 1 you need to keep more samples.

I don’t have a clue how many times an hour it updates. Any easy way to find that, or should I just stab around increasing the sampling_size until it works?

Look at the history graph for a short period of time. You should then be able to see the update interval if it is polled, or take a stab at estimating it and use the buffer fill attribute to adjust up or down.

1 Like