Statistics sensor outlasting max_age?

So I’ve set up statistics sensors for my weather station, and they always appear to outlast the defined max_age. Any ideas?

Image attached is dated 24/2 around 10:30am

 - platform: statistics
    name: weatherlink_max_wind_calendar_day
    unique_id: weatherlink_max_wind_calendar_day
    entity_id: sensor.weatherlink_wind_speed_last
    max_age:
      hours: 23
      minutes: 59
    state_characteristic: value_max 
  - platform: statistics
    unique_id: weatherlink_max_wind_calendar_day_date
    entity_id: sensor.weatherlink_wind_speed_last
    name: weatherlink_wind_speed_max_calendar_day_date
    state_characteristic: datetime_value_max
    max_age:
      hours: 23
      minutes: 59
  - platform: template
    sensors:
      weatherlink_wind_speed_max_calendar_day_date_formatted:
        unique_id: weatherlink_wind_speed_max_calendar_day_date_formatted
        friendly_name: 'Max Wind Speed Date Formatted Calendar Day'
        value_template: "{{ states('sensor.weatherlink_wind_speed_max_calendar_day_date') | as_timestamp | timestamp_custom('%d %b %Y at %H:%M:%S') }}"

https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

the statistics platform does not work with datetime values.

The ‘datetime’ works and matches the point in time when the high value was recorded.

My ‘issue’ is I’d like it to be within the 24 hour timeframe, but seems to be more in the line of midnight the following day that it updates. In fact ideally I’d like it to be for the current day, but that is nigh on impossible (I could use a statistics card but Mushroom cards are so much nicer looking when you customise them).

I’ve even set up an automation using the service ‘home assistant core integration: reload config entry’ (I’ve also tried ‘update entity’ in the automation, which did nothing), running every 30 minutes, but that doesn’t bring it within the 24 hours.

The only time that I see an entry update to within the 24 hour period is when I restart home assistant completely after updating or adding code, which obviously isn’t a usable solution for regular updates.

are you sure that isn’t caused by your source sensor? The stats sensor only looks at the age defined by your max, which rolls with time.

To get the current day only, you’d have to use utility meter which is designed to reset at midnight. But it won’t get the stats you’re looking for.

The source sensor updates every few seconds in this instance, as it reports current wind speed. So in theory (in my mind) that shouldn’t be a problem?

Right, but stats will only get you the wind speed in the past 24 hours, it wont’ be resetting at midnight.

Is there any reason you aren’t using max_age days: 1?

Days: 1 was my first solution, then hours: 24.

After that I tried the latest solution to try to be more specific.

As you say it should work, just seems not to for some reason. I thought there may be more insight on here.

So far I haven’t found a solution to this. I’ve tried just using minutes, hours, days etc etc.

If I set up a short term statistic sensor, 5 minutes for example, as an experiment it refreshes reliably when, as an example, the max wind speed hasn’t been exceeded it will show a more recent max wind speed that is lower.

However, with longer periods, no matter how they are defined, they don’t update when you expect them to.

I have set up an automation that will reload all statistics sensors at 3am in the morning (regular refreshes would be crazy as the time taken to load the proper data is 10 or 15 minutes), so if I don’t find any better solution I will activate that, and the statistics will be relatively up to date at least.

If anyone else has suggestions that might work without using a daily automation it would be much appreciated.

Similar problem.

     max_age:
       hours: 24

shows more than 24 hours.

What I’ve found is that the age only starts when HA last rebooted.

So if you’re adding more coding etc, you’re going to reset that period when you reboot to load it.

If you check the attributes, age coverage ratio reaches 1 when you have run the complete period of time without rebooting, and only then will it update within that time period.

Part of the issue is the constant updating of HA versions, which especially for time periods covering days, weeks and longer means you’ll probably never reach age coverage ratio of 1.

To test this issue. In the example below, a sensor is created with filling 4 times during the day.

configuration.yaml

input_number:
  age_test_num:
    name: age_test num
    min: 0
    max: 100000
    step: 1
    mode: box

sensor:
  - platform: statistics
    name: "max_age_test"
    unique_id: max_age_test_id
    entity_id: input_number.age_test_num
    state_characteristic: value_max
    max_age:
      hours: 24

  - platform: statistics
    name: "max_age_test_datetime"
    unique_id: max_age_test_datetime_id
    entity_id: input_number.age_test_num
    state_characteristic: datetime_value_max
    max_age:
      hours: 24

  - platform: statistics
    name: "min_age_test"
    unique_id: min_age_test_id
    entity_id: input_number.age_test_num
    state_characteristic: value_min
    max_age:
      hours: 24

  - platform: statistics
    name: "min_age_test_datetime"
    unique_id: min_age_test_datetime_id
    entity_id: input_number.age_test_num
    state_characteristic: datetime_value_min
    max_age:
      hours: 24

automations.yaml

- alias: max_age test
  id: '1'
  trigger:
    platform: time_pattern
    minutes: /1
  action:
  - service: input_number.set_value
    data_template:
      entity_id: input_number.age_test_num
      value: >
        {{ (now().strftime('%H') | int % 6) * 60 + now().strftime('%M') | int }}