Statistics Sensor not displaying correct values

Good day All,

I am hoping someone could help with some unexpected results from 2 statistics sensors I have created.

I am pretty confident that the configuration is correct as per documentation, but results are incorrect.

I have 2 x sensors that I want to use to display the Maximum value for the past 1 hour and for the past 24 hours.

The Lovelace dashboard does display values, but not what I am expecting. Been running for a few days already and recorder settings are all default.

Config and screenshots are below.

From the graphs, you can clearly see that Max values, but the dashboard does not display as per graph.

# Solar Inverter 1 Load Power Stats Max Last Hour
- platform: statistics
  name: solar_inverter_inverter_1_load_power_stats_max_last_hour
  entity_id: sensor.solar_inverter_inverter_1_load_power
  state_characteristic: value_max
  max_age:
    minutes: 60

# Solar Inverter 1 Load Power Stats Max Last Day
- platform: statistics
  name: solar_inverter_inverter_1_load_power_stats_max_last_day
  entity_id: sensor.solar_inverter_inverter_1_load_power
  state_characteristic: value_max
  max_age:
    hours: 24



Here is a graph of the source.

Have you tried setting

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.

I would think you would want hours set to 60 at least and day set to 1440 to get a once a minute max.

That’s not quite how it works.

The sampling size must be grater than the max age divided by the sampling interval of the sensor.

So if you have a sensor that updates every 30 seconds and want an average for the last hour:

1x60x60 / 30 = 120 samples minimum.

Or for a full day

24x60x60 / 30 = 2880 samples minimum

The statistics sensor has an attribute buffer_usage_ratio. Setting the sampling_size so that this is close to 1 but never equals 1 (buffer full) will give the best results with most efficient use of memory.

In short you need to know how often sensor.solar_inverter_inverter_1_load_power updates so you can set the sampling_size value.

For a buffer fill of 90%:

sampling_size = 1.1 * max_age in seconds / source sensor sampling interval in seconds

Thank you so much J Gent and tom_I

You when correct, I needed to add the sampling_size.

Not sure why, but I though all samples within the max_age would be used, but this certainly makes a lot of sense.

Also explains why the values for 1 hour and full day where the same because default is 20 samples.

Thank you so very much for the time taken to assist me. Really appreciate it.

Very kind of you both.

Kind Regards
Jason