Repository navigation
v1.3.1 - Never lock the recorder out; keep energy totals continuous
Fixes
The import can no longer lock the recorder out of its database
Reported in #15. On SQLite only one thing can write at a time, and the import was taking a second database connection to do its work. While it ran, Home Assistant's recorder could not commit its own events. On a large import that lasted long enough for the recorder to give up and discard what it had queued, which is lost history, and the only way to stop it was restarting Home Assistant.
The import no longer takes a second connection. It is queued onto Home Assistant's recorder thread and runs on the recorder's own connection, the same way Home Assistant's own bulk statistics import does. With no second writer there is nothing to lock out, however long the import takes. Events simply queue while it runs and are written immediately afterwards.
It is also considerably faster. Rows now go in with a multi-row insert rather than one at a time, roughly an order of magnitude quicker: about a second per 100,000 states.
Imports stay all-or-nothing at any size. Either the whole import lands or the database is left exactly as it was.
An import can now be stopped. Reloading or disabling the integration, or shutting Home Assistant down, cancels one that is running. Since nothing is committed until the end, a cancelled import writes nothing.
Energy history no longer restarts the destination from zero
Reported in #16. You could import an energy sensor's history correctly and still see the destination meter drop back to zero for everything recorded after the import.
Home Assistant continues a sensor's running total from that sensor's own most recent 5-minute statistics row, and starts again from zero when there is none. The import only ever looked at the destination's hourly rows to work out where to join the two series. A destination too new to have an hourly row got no join point at all, so its live readings stayed anchored at zero right after the imported history.
The destination's 5-minute rows are now used as the join point when there is no hourly row, and a destination with no statistics of its own gets its running total seeded at the end of the imported history so its new readings continue from there.
Repairing a meter that already restarted
If an earlier version left your destination with its history intact but its own later rows counting up from zero, re-run the import, or just click Preview. The result explains what happened and when, and offers a Repair running total button.
Repairing lifts every statistics row from the restart point onwards so the two halves line up and future readings continue from the corrected total. Rows before that point are left alone, and per-hour and per-day figures do not change: only the running total moves. You do not need to re-import anything, and it works even if the old sensor is long gone.
Nothing is repaired unless you click it, it re-checks your database when you click rather than trusting what the panel showed, and clicking twice cannot double the correction. It is only offered when the series shows a single unambiguous restart, so a sensor whose total legitimately goes down, such as a bidirectional one, is never flagged.
Notes
- Verified against a SQLite database mirroring Home Assistant's schema: a mid-import failure leaves it byte-identical, including for overwrite mode, where a crash previously had to leave part of the replaced window deleted.
- Behaviour was checked against Home Assistant's own recorder and sensor statistics source, not inferred.
- Back up your database before importing, as always.
Thanks to @PioThePio for the precise report on #15, including the BEGIN IMMEDIATE test that pinned down what was holding the lock, and to @janorn for #16.