From d8770b2bcee6867e1732bc754a72b646efc67fee Mon Sep 17 00:00:00 2001 From: maziggy Date: Sat, 14 Mar 2026 10:18:27 +0100 Subject: [PATCH] Fix energy costs not updating per timeframe in statistics (#695) In "total" energy tracking mode, the stats endpoint queried smart plug lifetime counters which can't be filtered by date range. Energy costs and kWh stayed the same regardless of timeframe selection. Fall back to per-print archive data when date filters are active. --- backend/app/api/routes/archives.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/app/api/routes/archives.py b/backend/app/api/routes/archives.py index 9ae28b326..46a28c4b0 100644 --- a/backend/app/api/routes/archives.py +++ b/backend/app/api/routes/archives.py @@ -642,7 +642,9 @@ async def get_archive_stats( energy_cost_per_kwh_str = await get_setting(db, "energy_cost_per_kwh") energy_cost_per_kwh = float(energy_cost_per_kwh_str) if energy_cost_per_kwh_str else 0.15 - if energy_tracking_mode == "total": + # When date filters are active, smart plug lifetime totals can't be + # filtered by date range — fall back to per-print archive data instead. + if energy_tracking_mode == "total" and not date_from and not date_to: # Total mode: sum up 'total' counter from all smart plugs (lifetime consumption) from backend.app.models.smart_plug import SmartPlug from backend.app.services.homeassistant import homeassistant_service