@@ -292,6 +292,59 @@ def _check_if_minimum_is_at_end_of_day_and_energy_rates_of_tomorrow_are_unavaila
292
292
293
293
return is_price_minimum_near_end_of_day and are_tomorrows_rates_unavailable
294
294
295
+ def get_energy_rate_before_and_after_the_price_is_higher_than_the_average_until_timestamp (
296
+ self , upcoming_energy_rates : list [EnergyRate ], ending_timestamp : datetime
297
+ ) -> tuple [EnergyRate , EnergyRate ]:
298
+ """
299
+ Identifies two specific energy rates from a list of upcoming energy rates up to a given timestamp:
300
+ the last rate before the price is higher than the average and the first rate
301
+
302
+ Args:
303
+ upcoming_energy_rates (list[EnergyRate]): The list of upcoming energy rates.
304
+ ending_timestamp (datetime): A timestamp limiting the processing to only consider energy rates up to and
305
+ including this time.
306
+
307
+ Returns:
308
+ tuple[EnergyRate, EnergyRate]: A tuple containing:
309
+ - The energy rate before the price is higher than the average price up to the provided timestamp.
310
+ - The energy rate after the price is higher than the average price up to the provided timestamp.
311
+ """
312
+ average_price = self ._get_average_price_of_energy_rates (upcoming_energy_rates )
313
+ self .log .debug (f"The average price of all upcoming energy rates is { average_price } " )
314
+
315
+ upcoming_energy_rates_until_ending_timestamp = [
316
+ energy_rate for energy_rate in upcoming_energy_rates if energy_rate .timestamp <= ending_timestamp
317
+ ]
318
+
319
+ self .log .trace ("Determining the last energy rate before the price is higher than the average price..." )
320
+ energy_rate_before_the_price_is_higher_than_the_average = upcoming_energy_rates_until_ending_timestamp [0 ]
321
+ for energy_rate in upcoming_energy_rates_until_ending_timestamp :
322
+ if energy_rate .rate < average_price :
323
+ self .log .trace (f"The energy rate { energy_rate } is cheaper than the average price" )
324
+ energy_rate_before_the_price_is_higher_than_the_average = energy_rate
325
+ else :
326
+ self .log .trace (f"The energy rate { energy_rate } is more expensive than the average price" )
327
+ break
328
+
329
+ self .log .trace ("Determining the first energy rate after the price was higher than the average price..." )
330
+ energy_rate_after_the_price_is_higher_than_the_average = upcoming_energy_rates_until_ending_timestamp [- 1 ]
331
+ for energy_rate in reversed (upcoming_energy_rates_until_ending_timestamp ):
332
+ if energy_rate .rate < average_price :
333
+ self .log .trace (f"The energy rate { energy_rate } is cheaper than the average price" )
334
+ energy_rate_after_the_price_is_higher_than_the_average = energy_rate
335
+ else :
336
+ self .log .trace (f"The energy rate { energy_rate } is more expensive than the average price" )
337
+ break
338
+
339
+ return (
340
+ energy_rate_before_the_price_is_higher_than_the_average ,
341
+ energy_rate_after_the_price_is_higher_than_the_average ,
342
+ )
343
+
344
+ @staticmethod
345
+ def _get_average_price_of_energy_rates (energy_rates : list [EnergyRate ]) -> float :
346
+ return sum (energy_rate .rate for energy_rate in energy_rates ) / len (energy_rates )
347
+
295
348
def write_energy_rates_to_database (self , energy_rates : list [EnergyRate ]) -> None :
296
349
"""
297
350
Writes the list of energy rates to the database while avoiding duplication of already existing data.
0 commit comments