Statistics Graph not resetting at the same time as the utility meter

This is a duplicate of a bug report, because I didn’t yet get a response there. Maybe here somebody has an explanation of the given issues:

The problem

I have a sensor which reports me my gas meter value as a continuously increasing value:
grafik

sensor:
# Gasmeter
- platform: mqtt
  name: "Gasmeter Value"
  unique_id: gasmeter_value
  state_topic: "gaszaehler/main/value"
  unit_of_measurement: 'm³'
  state_class: total_increasing
  device_class: gas
  icon: 'mdi:fire'
  last_reset_value_template: '1970-01-01T00:00:00+00:00'

Also, I have an utility meter which resets the data once a day (its nowhere stated, but I expect it to happen on midnight, 00:00:00):
grafik

utility_meter:
  utility_meter_gas_per_day:
    source: sensor.gasmeter_value
    cycle: daily

I now would like to have a statistics diagram which shows me the usage per day similar to what the Energy dashboard provides.
Since the Statistics Graph requires state_class: measurement, I added a template sensor for this:

# The Statistics Graph needs to have the "state_class" set to "measurement", see https://community.home-assistant.io/t/statistics-graph-just-keeps-going-up/335516/5
  - name: "Gasmeter per day"
    state: "{{ states('sensor.utility_meter_gas_per_day') }}"
    unit_of_measurement: 'm³'
    state_class: measurement

Then I created a statistics graph with it:

I now would expect that the max starts new every day, how ever it keeps the max of the previous day and only goes over it if the consumption is higher than on the previous day (like 2. to 3. Oct.)!

It looks like as if the Statistics Graph takes the max the last time on midnight (00:00:00), how ever the Utility meter only resets a bit later (eg. on 00:00:01). This would cause the Statistics graph to take the max value of the previous day as its start value for max.

I tested this on a clean, new instance of HA.
Also I set the timezone correctly (right from the beginning):

default_config:

homeassistant:
  unit_system: metric
  time_zone: Europe/Zurich

Below 2 other screenshots of the next day showing the issue once more. And the Energy Dashboard, which does it correctly:

What version of Home Assistant Core has the issue?

Home Assistant Core 2022.6.5

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Statistics Graph

Link to integration documentation on our website

Yeah I’ve struggled with the same. Technically, I don’t think it’s a bug. Anything that resets daily will likely do so at least one second after midnight. Therefore, for that second, the current state is the value from yesterday. Consequently, the ‘max’ recorded for the first hour of the day is likely to be the max from the last hour of the previous day, and similarly the ‘max’ for the whole day will often be that from the previous day.

I therefore avoid min/max from statistics. I’m using ApexCharts to plot similar data from the statistics themselves…

  statistics:
    type: state
    period: day

This produces the correct values. For values that reset just after midnight, the state from statistics correctly shows the maximum for the day. I’m now struggling with daily rain, where the forecast I use resets at 9am.

Thanks for your reply and suggestion!

IMO it is still a bug because it makes “max” daily statistics useless.
Looking on the monthly statistics, the reset day (offset) can be defined by user. Having such a parameter also for the daily reset would solve this issue.

2 Likes

I have same problem.

2 Likes

Could you show more code on how you solved the problem? I’m having the same issue as the OP with the max value flowing over to the next day.

I didn’t need code to fix the problem as I’m using ApexCharts and by using state&day it shows the last state for the day, which in my case was the correct max.

However, depending on your needs, you could create a template sensor that copies the original sensor, and at (say) 11:59pm resets the sensor to zero. Something along the lines of (completely untested):

template:
- trigger:
    - platform: time
      at: "23:59:00"
  sensor:
    - name: New sensor
      unique_id: new_sensor
      state: >-
        {% if (now().strftime('%H%M') | int) > 2350 %}
          0
        {% elif has_value('sensor.original') %}
          {{ states('sensor.original') }}
        {% endif %}

It may also be possible to use the HA API to post a new state to the original sensor just before midnight to fix the issue. Good luck!