Import long term statistics works but sum of new values are wrong

Hi,
I’m using GitHub - klausj1/homeassistant-statistics: This HA integration allows to import long term statistics from a file like csv or tsv and ask this question also in github.

My smart meter (EDL21 Integration) transmit the actual Wh (total consumption) to HA.

I import data in this scheme with the integration.

statistic_id	unit	start	state	sum
sensor.smart_meter_positive_gesamtwirkenergie	Wh	31.12.2024 23:00	10858000.0	0.0
sensor.smart_meter_positive_gesamtwirkenergie	Wh	31.01.2025 23:00	11415000.0	557000.0
sensor.smart_meter_positive_gesamtwirkenergie	Wh	13.02.2025 20:00	11588000.0	730000.0
sensor.smart_meter_positive_gesamtwirkenergie	Wh	13.02.2025 21:00	11588000.0	730000.0
sensor.smart_meter_positive_gesamtwirkenergie	Wh	13.02.2025 22:00	11589000.0	731000.0

...

sensor.smart_meter_positive_gesamtwirkenergie	Wh	17.12.2025 09:00	13374000.0	2516000.0
sensor.smart_meter_positive_gesamtwirkenergie	Wh	17.12.2025 10:00	13374000.0	2516000.0
sensor.smart_meter_positive_gesamtwirkenergie	Wh	17.12.2025 11:00	13374000.0	2516000.0

After this, my energy grid graph looks good

In this moment where HA does insert a new value for longtime statistics, the sum is wrong and my graph looks like this.

Graph:

Data in database (last three rows):

id created created_ts metadata_id start start_ts mean min max last_reset last_reset_ts state sum mean_weight
1676675 1765965610,16729 418 2025-12-17 10:00 13374000 2516000
1676730 1765967570,87445 418 2025-12-17 11:00 13374000 2516000
1676825 1765972810,17392 418 2025-12-17 12:00 13375000 -115802

There are not other data rows in table “statistics” in relation to the column “metadata_id” with my id 418. All data with this id is imported. The sensor sensor.smart_meter_positive_gesamtwirkenergie already exists.

I don’t understand why HA calculate the new value with this difference. What is the reason? Thanks for helping … :slight_smile:

I read this Long- and short-term statistics | Home Assistant Data Science Portal

statistics: Hourly aggregates of the data from the statistics_short_term table.

I exported all related rows to metadata_id for backup. After this I deleted all entries with this metadata_id from table “statistics” and “statistics_short_term”. All looks good after reimport via the integration “homeassistant-statistics”.