Handling midnight rollover power usage

I am logging power usage data via ESPhome, excerpt of my .yaml file:

  • platform: total_daily_energy
    name: Heat Pump Total kWh
    power_id: heatPumpTotalWatts
    filters:
    • multiply: 0.001
      unit_of_measurement: kWh

time:

  • platform: homeassistant
    id: homeassistant_time

This works great, dumps into influxdb. In Grafana I’m using 2 queries, query A finds the max() per hour interval, B finds the min() per hour interval, then I plot the expression ($A-$B) as a bar chart. Displays how I want, but there’s an issue at midnight. Specifically, total kWh resets to zero at midnight, but sometimes it resets just prior to midnight, so when I plot max() - min() in 1h intervals, min() for the 23:00-23:59:59 interval returns zero and causes a big spike for the hour interval just before midnight.

I suppose it could also happen where the reset occurs just after midnight causing a big spike for the 0:00-0:59:59 interval.

What are my options for fixing this?

If you can then stop resetting the counter at midnight but instead add another value that holds the midnight value.
You can then control when you update that midnight value and make sure that it is right after the 23-00 calculations.

@texasranger556 Did you ever find a solution for this? I have the same problem, but my issue is the value rolls over a few milliseconds after midnight. So my min-max values are way wrong for the 00-01 hours. Its driving me crazy!