Energy panel show wrong results

Yesterday i was shocked when locking at my energy panel in HA, it shows a negative water consumption:

What happened here? I’ve got this kind of problem before on other meters also (gas and power), so it seems to happen from now to then.

Can anybody explain what could caused this and how to repair it? The data comes from an ESP32Home sensor (simple binary counter) and the sensor wasn’t updated since weeks. Is there something wrong inside the ESP or does HA mixed things up?

I’ve got sqlite plugin installed also, if that is needed and i’m also a little bit familar with SQL but got no explanation where this completely wrong values should come from?

This shows statistics and in this case a negative jump from sum-value (example) 300 > 297.2 (i.e a minus of roughly 2.8). Why this happens…beats me, but you are not alone.
Option 1: try to look in the dev tools > statistics and there you can find the sensor that registers this…on the far right there is an icon and you can try to correct the value
Option2: the statistics…this can become tricky depending on where the issue lies. Probably on statistics_short_term that feed into statistics. Without much detail, the field ‘sum’ values have to match what you want. e.g. 300, 302, 305, 305, 306, 309 etc.
If statistics_short_term is off then you have to start there before correcting statistics
In any case, backup first, I would copy the two tables temporarily in the same db. And if this is a sensor delivering belwo a few minutes freq…switch off sensor or HA too

Can you show the history graph for the water sensor for the time this happened?

Oh, good you asked. That is another part of the riddle, because the history of that day shows normal values:

What I have seen with me and others, it is the ‘reset’ of statistics_short_term that beep’s up.

So it appears that your history starts at 13:00 on the 19th. You asked for data back as far as the beginning of the day (0:00) but there is none shown before 13:00.

That could point to an issue with your database.

What could that be? I have no idea what could have happened. Where should i look?

Settings → logs.

I’m also having an issue with this.
I was on holiday for a couple of weeks and the ESP Watermeter froze.
Now I’m back, restart the thing and my Energy shows -47m3.

Basically negated the entire year’s consumption.

I found out that everytime i restart one of my metering-ESP nodes, e.g. because of an ESP-firmware-upgrade through HA, the node sends the initial consumption, set in the config.

What i do to measure the consumption is

globals:
  - id: total_pulses
    type: int
    #restore_value: true
    initial_value: '553313'  # set initial counter read from device as decimal (three counts after decimal point included, e.g. if it reads "553.313" write as "553313")

binary_sensor:
  - platform: gpio
    id: internal_pulse_counter
    pin:
      number: GPIO5 # "D1"
      inverted: true
      mode: INPUT_PULLUP
    name: water-meter-impulse
    filters:
      - delayed_on: 200ms
    on_press:
      then:
        - lambda: id(total_pulses) += 1;

sensor:
  - platform: template
    name: "Total water consumption"
    device_class: water
    unit_of_measurement: "m³"
    state_class: "total_increasing"
    icon: "mdi:water"
    accuracy_decimals: 3
    lambda: |-
      return id(total_pulses) * 0.001;

I thought “globals” where stored in HA but it looks like i was wrong and they are only inside the node itself. So as part of an firmware-upgrade the old initial value will we reset. Maybe this is also the case if there was a powerloss or reboot of the ESP node.