Statistics platform value_min sensor seems wrong

So I’m trying to get the min value of of a sensor over 24 hours, so I can build an automation when the current value of the sensor exceeds it for more than a few hours.

I have this in configuration.yaml

sensor:
- platform: statistics
  name: "Min Household Power Over Last 24 hours"
  entity_id: sensor.rainforest_emu_2_power
  state_characteristic: value_min
  max_age:
    hours: 24

But what I end up with is chart that looks like this, which doesn’t seem right at all. I was expecting the min over 24 hours to be the min value over a rolling 24 hour window.

You need to increase the number of samples to an amount sufficient for 24 hours. The default is only 20.

So if your sensor updates every 30 seconds, sampling size =

24 hours / 30 sec = 2880 samples.

There’s an attribute of your sensor that shows the % of samples used. It should never reach 100%.

1 Like

I am having a similar problem with extracting the min value using the statistics platform. The sensor involved is measuring the inside temperature and I want to extract the min temperature, typically overnight. I have been finding that in the early morning the value is correct but later as the temperature increases the min value increases as well.
I have been playing with number of samples and the max_age values and I can make it work (so far as my tests go over a limited time) but the values are not making sense to me.
The weather station is set to report on a 2 minute schedule so that maximum number of samples over 24 hours is 720. The max_age is set to 24 hours.
Increasing the sample size has no effect as it seems to hit the max_age barrier. Increasing max_age to 30 seems to fix the result.
Incrementing the max_age value by one repeatedly causes the result to follow the increasing temperature value, as shown in the attached graph, in the reverse direction, i.e. earlier in time, until reaching 29 hours where it lands on the correct sample. Tweaking the sampling_size in a similar manner with max_age set to 30 has a similar effect until the sampling_size is in the 900 range.

# Minimum overnight inside temperature
  - platform: statistics
    name: "Inside Temp Min"
    entity_id: sensor.upper_greenwood_road_inside_temp
    state_characteristic: value_min
    sampling_size: 900
    max_age:
      hours: 30

Can you show the history for the source sensor as well?

Did it only change to the min value 29 hours ago?

If so, then yes you need to increase your max age of the samples. This too will cut off the samples available to the statistics integration.

Graph of statistics min temperature vs source temperature sensor attached. The strangeness from around the 11AM time forward is my experiment with different settings.
The min temperature was achieved around 6AM which would be normal for this season.

Update:
I think I see the problem now. The max_age window is too large. It was including data from the previous day and apparently the correct min value was not determined until the 24 hour window elapsed and then the current day min temperature was detected. Since the min temps are not occurring necessarily 24 hours apart it was possible for the min temp from 2 days to be within the 24 hour window.
I wonder if it is possible to set the data window limits on absolute TOD instead of the duration.

Unfortunately it is not possible. This can do it though (with a template):

Something like

start: "{{ today_at('0:00') }}"
end: "{{ today_at('12:00') }}"

Not sure if this meets your requirements but the following Trigger-based Template Sensor reports the lowest temperature throughout the day then resets at 00:00.

template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: state
        entity_id: sensor.upper_greenwood_road_inside_temp
    sensor:
      - name: 'Temperature Daily Minimum'
        unique_id: 'temperature_daily_minimum'
        unit_of_measurement: '°C'
        device_class: temperature
        state: >
          {% set t_new = states('sensor.upper_greenwood_road_inside_temp') | float(-99) %}
          {{ [t_new, this.state | float(-99)] | min if trigger.platform != 'time' else t_new }}

Interesting template, something that I would never have anticipated, not being well versed in the possible constructs in this template. So, I have some stupid questions:

float (-99), I assume just uses -99 as the default value and is not some special case for | float ().

this.state, I am never sure about to what ‘this’ refers. Is ‘this’ bound to platform: state/entity_id such that it refers to the entity? That would make sense to me as it would be a choice for the | min filter along with t_new.

trigger.platform != ‘time’, I assume, once again, is bound to trigger/platform: time/at: in order to reset the template sensor value at 00:00:00 (midnight).

I know, RTFB. I appreciate the help and do not expect you to spend your time educating the uneducated.

Correct. Replace -99 with whatever value you think is best for your application.

this references the template sensor (i.e. self-referential). this.state references the sensor’s current state value.

Correct. It’s used to differentiate between which of the two triggers was responsible for triggering the Template Sensor.