@@ -101,6 +101,7 @@ def __init__(
101101 self ._energy_counters = EnergyCounters (mac )
102102 self ._retrieve_energy_logs_task : None | Task [None ] = None
103103 self ._last_energy_log_requested : bool = False
104+ self ._last_collected_energy_timestamp : datetime | None = None
104105
105106 self ._group_member : list [int ] = []
106107
@@ -443,13 +444,18 @@ async def get_missing_energy_logs(self) -> None:
443444
444445 if (missing_addresses := self ._energy_counters .log_addresses_missing ) is None :
445446 _LOGGER .debug (
446- "Start with initial energy request for the last 10 log addresses for node %s." ,
447+ "Start with initial energy requests for the last 10 log addresses for node %s." ,
447448 self ._mac_in_str ,
448449 )
449450 total_addresses = 11
450451 log_address = self ._current_log_address
451452 while total_addresses > 0 :
452453 await self .energy_log_update (log_address )
454+ if (
455+ datetime .now (tz = UTC ) - self ._last_collected_energy_timestamp
456+ ).total_seconds // 60 > 65 : # assuming log_interval is 60 minutes
457+ _LOGGER .debug ("Energy data collected is outdated, stopping collection" )
458+ break
453459 log_address , _ = calc_log_address (log_address , 1 , - 4 )
454460 total_addresses -= 1
455461
@@ -501,6 +507,7 @@ async def energy_log_update(self, address: int | None) -> bool:
501507 # Forward historical energy log information to energy counters
502508 # Each response message contains 4 log counters (slots) of the
503509 # energy pulses collected during the previous hour of given timestamp
510+ last_energy_timestamp_collected = False
504511 for _slot in range (4 , 0 , - 1 ):
505512 log_timestamp , log_pulses = response .log_data [_slot ]
506513 _LOGGER .debug (
@@ -519,6 +526,11 @@ async def energy_log_update(self, address: int | None) -> bool:
519526 import_only = True ,
520527 ):
521528 energy_record_update = True
529+ if not last_energy_timestamp_collected :
530+ self ._last_collected_energy_timestamp = (
531+ log_timestamp .replace (tzinfo = UTC )
532+ )
533+ last_energy_timestamp_collected = True
522534
523535 self ._energy_counters .update ()
524536 if energy_record_update :
0 commit comments