Environment
- Image: iammeter/iammeter (created 2025-12-08, digest sha256:e6f4d0fe86c7...)
- .NET 7.0.20, SQLite database, data.db about 2.7 GB (about 12.8 million rows in BIZ_MeterData)
- Meter: 3-phase WEM3080T, firmware i.91.065.9, HTTP upload every 7 to 14 s, forward to cloud enabled
Symptom
- Power values are correct, but daily, monthly and yearly energy in the local UI are far too high.
Example for one day: local 654.23 kWh, IAMMETER cloud 65.78 kWh.
- The ratio is not constant: it changes every hour (from x7 to x25).
- The meter's cumulative registers (monitorjson) and the cloud values are correct.
Evidence
- BIZ_MeterData (raw data) is correct: the YDL/KDL registers match the meter.
- BIZ_MeterEnergy contains many rows with the same (MeterId, Time). Example for hour 02:00:
14 rows per phase, each holding the full hour's energy (about 4.1 to 4.25 kWh),
so the UI sums 14 copies of the same hour.
- UpdateTime of these duplicate rows is spread between 03:00:54 and 03:03:43:
the hourly calculation takes about 3 minutes.
Probable cause
1. The hourly energy calculation seems to be triggered by every incoming upload.
It checks whether the hour row exists, then inserts it. This is not atomic: every upload
received while a previous calculation is still running inserts its own row.
2. The calculation is slow because BIZ_MeterData has no index on (MeterId, RTCTime),
so each calculation scans the whole table. This makes the race window minutes long
and causes heavy disk reads (several TiB over a few months).
Workaround applied
- Added indexes: BIZ_MeterData (MeterId, RTCTime) and BIZ_MeterEnergy (MeterId, Time).
The calculation now takes seconds and no new duplicates have appeared so far.
- Removed existing duplicates, keeping the highest value per (MeterId, Time).
Suggested fix
- Add a unique constraint on BIZ_MeterEnergy (MeterId, Time) and use an upsert
(INSERT ... ON CONFLICT DO UPDATE), or serialize the hourly calculation
(single background job or lock) instead of running it on every upload.
- Add the (MeterId, RTCTime) index on BIZ_MeterData in a migration.
- Also noticed: "Unexpected end of request content" errors on /api/v1/sensor/uploadsensor
(about 55 per 24 h), and the user TimeZoneInfoId setting has no effect (all times are
stored and shown in the container's timezone).