@@ -260,10 +260,7 @@ def clear_statistics() -> None:
260
260
261
261
def electricity_tariff_percentage (start : date , end : date ) -> Optional [Dict ]:
262
262
"""Returns the total electricity consumption percentage by tariff (high/low tariff)."""
263
- totals = DayStatistics .objects .filter (
264
- day__gte = start ,
265
- day__lte = end ,
266
- ).aggregate (
263
+ totals = DayStatistics .objects .filter (day__gte = start , day__lte = end ,).aggregate (
267
264
electricity1 = Sum ("electricity1" ),
268
265
electricity2 = Sum ("electricity2" ),
269
266
)
@@ -419,7 +416,6 @@ def year_statistics(target_date: datetime.date):
419
416
return range_statistics (start = start_of_year , end = end_of_year )
420
417
421
418
422
- # @TODO: Consider reworking this to meter positions in favor of https://github.com/dsmrreader/dsmr-reader/issues/1811
423
419
def period_totals () -> Dict :
424
420
"""Retrieves year/month period totals and merges them with today's consumption."""
425
421
today = timezone .localtime (timezone .now ())
@@ -429,6 +425,17 @@ def period_totals() -> Dict:
429
425
except LookupError :
430
426
today_stats = {}
431
427
428
+ # also add yesterday stats if today totals are not available yet
429
+ # see https://github.com/dsmrreader/dsmr-reader/issues/1811
430
+ if not day_statistics (target_date = today ):
431
+ try :
432
+ yesterday = today - timezone .timedelta (days = 1 )
433
+ yesterday_stats = dsmr_consumption .services .day_consumption (day = yesterday )
434
+ except LookupError :
435
+ yesterday_stats = {}
436
+ else :
437
+ yesterday_stats = {}
438
+
432
439
month_stats = month_statistics (target_date = today )
433
440
year_stats = year_statistics (target_date = today )
434
441
excluded_keys = (
@@ -443,13 +450,15 @@ def period_totals() -> Dict:
443
450
continue
444
451
445
452
# Assumes same keys, zero value fallback.
453
+ month_stats [k ] += yesterday_stats .get (k , 0 )
446
454
month_stats [k ] += today_stats .get (k , 0 )
447
455
448
456
for k in year_stats .keys ():
449
457
if k in excluded_keys or year_stats [k ] is None :
450
458
continue
451
459
452
460
# Assumes same keys, zero value fallback.
461
+ year_stats [k ] += yesterday_stats .get (k , 0 )
453
462
year_stats [k ] += today_stats .get (k , 0 )
454
463
455
464
return dict (
0 commit comments