Skip to content

Commit 114ec52

Browse files
committed
Refine minimum-check logic for end-of-day energy rates.
Updated the method to also verify if energy rates for tomorrow are unavailable when determining if the minimum is at the end of the day.
1 parent 2503fbc commit 114ec52

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

source/tibber_api_handler.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ def get_next_price_minimum(self, first_iteration: bool = False) -> EnergyRate:
6969
energy_rates_between_first_and_second_maximum
7070
)
7171

72-
if self._check_if_minimum_is_at_end_of_day(minimum_of_energy_rates):
72+
if self._check_if_minimum_is_at_end_of_day_and_energy_rates_of_tomorrow_are_unavailable(
73+
minimum_of_energy_rates, upcoming_energy_rates
74+
):
7375
minimum_of_energy_rates.is_minimum_that_has_to_be_rechecked = True
7476

7577
return minimum_of_energy_rates
@@ -257,20 +259,34 @@ def get_global_minimum_of_energy_rates(self, energy_rates_till_maximum: list[Ene
257259
)
258260
return global_minimum_of_energy_rates
259261

260-
def _check_if_minimum_is_at_end_of_day(self, price_minimum: EnergyRate) -> bool:
262+
def _check_if_minimum_is_at_end_of_day_and_energy_rates_of_tomorrow_are_unavailable(
263+
self, price_minimum: EnergyRate, upcoming_energy_rates: list[EnergyRate]
264+
) -> bool:
261265
"""
262-
This method determines whether the timestamp of the `price_minimum` falls within the last 3 hours a day.
263-
This is done since the price rates of the next day are only available after ~ 02:00 PM.
266+
This method determines whether the timestamp of the `price_minimum` is in the last hour of the day and checks if
267+
there are no energy rates available for the subsequent day.
268+
This is done since the price rates of the next day are only available after ~ 02:00 PM. If the price rates of
269+
the next day are unavailable while determining the price minimum, it is likely that the price minimum is just
270+
the last rate of the day but not actually the minimum.
271+
In this case we have to check in later (at ~ 02:00 PM) to re-request the prices from the Tibber API to get
272+
the values of the next day.
264273
265274
Args:
266275
price_minimum (EnergyRate): The energy rate with the minimum price.
276+
upcoming_energy_rates (list[EnergyRate]): List of upcoming energy rates.
267277
268278
Returns:
269-
bool: True if the price minimum is in the last 3 hours of the day, otherwise False.
279+
bool: True if the price minimum is in the last hour current day and there are no rates for
280+
the next day, otherwise False.
270281
"""
271282

272-
is_price_minimum_near_end_of_day = 21 <= price_minimum.timestamp.hour <= 23
283+
is_price_minimum_near_end_of_day = price_minimum.timestamp.hour == 23
273284
self.log.trace(
274285
f"The price minimum {price_minimum.timestamp} is at the end of the day: {is_price_minimum_near_end_of_day}"
275286
)
276-
return is_price_minimum_near_end_of_day
287+
288+
today = datetime.now().date()
289+
are_tomorrows_rates_unavailable = all(rate.timestamp.date() == today for rate in upcoming_energy_rates)
290+
self.log.trace(f"The price rates for tomorrow are unavailable: {are_tomorrows_rates_unavailable}")
291+
292+
return is_price_minimum_near_end_of_day and are_tomorrows_rates_unavailable

0 commit comments

Comments
 (0)