Statistics Sensor

I tried using the min/max/mean/median helper to detect minimum temperature from a sensor. After some investigation, it seems that this particular sensor does not work for a single sensor entity. ???
I am completely baffled as to what minimum it is reporting. Does it report the instantaneous minimum value among the sensors involved? I guess that could be useful if you were trying to find the coolest/warmest place in the house by using multiple sensors.
Instead of this helper sensor I have opted to use a sensor in the statistics platform. This does not seem to work as expected either. The sensor is present in the entities list but the value is seemingly random. The value is not the minimum value over the last 24 hours as programmed it is also not the current value of the underlying sensor.


# Minimum overnight inside temperature
  - platform: statistics
    name: "Inside Temp Min"
    entity_id: sensor.filtered_inside_temp
    state_characteristic: value_min
    max_age:
      hours: 24

HA 2022.7.5 on Docker

Just a thought but has it existed for 24hours yet?

The statistics sensor is new but the underlying sensor data is ancient. Well maybe ancient is a slight exaggeration. Unless what you are intimating is that the statistics sensor has its own dataset and is not simply performing a database search in the underlying sensor data.

I’m not actually sure but would the value make sense in that context?

The statistics sensor does utilise the recorder data.

For 24 hours you are probably going to need a lot more samples than the default 20. Unless your sensor only updates every 90 minutes.

https://www.home-assistant.io/integrations/statistics/#sampling_size

You can calculate the number of samples you need like this:

max age / source sensor update interval

Both must be in the same units (e.g. seconds). Then add another 10% or so to be safe.

e.g. for a sensor that updates every 30 seconds:

86400 / 30 = 2880 samples. + 10% = 3170 samples.

If your source sensor is pushed rather than polled and does not have a constant update interval then you need to look at the history graph for the sensor and make a guess at the average update interval. Then add a more of a safety margin than 10% depending on the confidence of your guess.

You can monitor what percentage of samples you are using by looking at the buffer usage attribute for the stats sensor. You want this to be close to 1 but never actually reaching 1 (100% full). If after 24 hours it reaches 1 then add some more samples. If it is always a low value then reduce your sample size.

1 Like

Yes, I believe it would make sense based on what I am seeing. As you suggested, now that the sensor has been in play for more than the max_age value it appears that it now represents the actual ‘min’ value as desired.

That is all very interesting but I do not believe any of that is an issue. As I indicated in my reply to SgtBatten, the sensor now seems to reporting the correct ‘min’ value. All if this suggests that HA is just creating a separate sensor which makes sense as it is set up as a sensor. This ‘statistics’ sensor is subject to the same database recording as every other sensor as long as the recording of sensor data is enabled in the config file. This means that the statistics sensors are replicating the underlying sensor data and acting on this data to produce the min/max/mean/etc values according to other settings such as max_age. Despite the ‘count’ parameter not being set outside of the default value this now seems to be working without bloating the database with more stored samples. My configuration is based on the example shown in the Statistics information nation page. The underlying sensor is updated on a 2 minute basis which would mean 30/hour or 720/24 hrs but the default value of ‘count’ seems to produce the correct and desired value.

The default value for sampling_size is 20. If you don’t change it you are only taking the minimum over the last 40 minutes. (20 x 2 min).

Look at the buffer_usage_ratio of your stats sensor in Dev Tools States if you don’t believe me.