fix(smart-plugs): read a REST plug's lifetime counter, and derive Today/Yesterday from it (issue #2539)

A Shelly reports one energy figure — aenergy.total, a lifetime counter in Wh
that never resets. Bambuddy had a single REST energy field and filed whatever
it found under "today", so the value never reset at midnight, and Yesterday
and Total stayed at zero: get_energy() simply never set those keys.

With `total` unpopulated, the hourly snapshot recorder skipped the plug, so
the Statistics page's energy figure was zero as well, not just the Settings
card.

Split the REST energy config in two: rest_energy_path still means "used
today", rest_energy_total_path means "lifetime counter". A Shelly has only
the latter; a Tasmota behind a REST bridge has both; sharing a URL costs one
fetch, not two.

Then derive Today and Yesterday from that counter using the snapshots we were
already taking: today = counter now - counter at the last local midnight;
yesterday = the gap between the two previous midnights. Local midnight, not
UTC — a UTC boundary rolls Today over at 02:00 in Berlin. The snapshot loop
now ticks on the local hour so a reading lands on the boundary instead of up
to an hour early. A counter that goes backwards (factory reset) reports
nothing rather than a negative.

Collateral, found while verifying on both engines: the smart-plug DateTime
columns are naive UTC but the code wrote aware datetimes into them. SQLite
drops the offset; asyncpg raises DataError. So on Postgres every snapshot
capture raised inside the loop's except, and every status poll raised on
last_checked — the whole subsystem was dead on the database we recommend for
multi-printer installs. All plug timestamps are naive UTC now.

Existing REST users with a cumulative path in the today field must move it to
the new lifetime field; the form and wiki now name which counter each wants.
This commit is contained in:
maziggy
2026-07-11 14:22:31 +02:00
parent d09db436c3
commit aba00598bb
30 changed files with 845 additions and 110 deletions
+8
View File
@@ -1855,6 +1855,10 @@ export interface SmartPlug {
rest_energy_url: string | null;
rest_energy_path: string | null;
rest_energy_multiplier: number;
// Lifetime counter, separate from the daily one (#2539). A Shelly reports only
// this; Today and Yesterday are derived from its hourly snapshots.
rest_energy_total_path: string | null;
rest_energy_total_multiplier: number;
printer_id: number | null;
enabled: boolean;
auto_on: boolean;
@@ -1929,6 +1933,8 @@ export interface SmartPlugCreate {
rest_energy_url?: string | null;
rest_energy_path?: string | null;
rest_energy_multiplier?: number;
rest_energy_total_path?: string | null;
rest_energy_total_multiplier?: number;
printer_id?: number | null;
enabled?: boolean;
auto_on?: boolean;
@@ -1995,6 +2001,8 @@ export interface SmartPlugUpdate {
rest_energy_url?: string | null;
rest_energy_path?: string | null;
rest_energy_multiplier?: number;
rest_energy_total_path?: string | null;
rest_energy_total_multiplier?: number;
printer_id?: number | null;
enabled?: boolean;
auto_on?: boolean;