@@ -54,9 +54,20 @@ def get_timestamp_of_next_price_minimum(self, first_iteration: bool = False) ->
54
54
api_result = self ._fetch_upcoming_prices_from_api ()
55
55
all_energy_rates = self ._extract_energy_rates_from_api_response (api_result )
56
56
upcoming_energy_rates = self ._remove_energy_rates_from_the_past (all_energy_rates )
57
- energy_rates_between_first_and_second_maximum = self ._get_energy_rates_between_first_and_second_maximum (
58
- upcoming_energy_rates , first_iteration
59
- )
57
+ if first_iteration and not self ._check_if_next_three_prices_are_greater_than_current_one (
58
+ upcoming_energy_rates
59
+ ):
60
+ self .log .info (
61
+ "This is the first time finding the minimum prices and the prices are currently on a decline. "
62
+ + "Thus the next price minimum is considered (instead of the one after the first maximum)."
63
+ )
64
+ energy_rates_between_first_and_second_maximum = self ._find_energy_rates_till_first_maximum (
65
+ upcoming_energy_rates
66
+ )
67
+ else :
68
+ energy_rates_between_first_and_second_maximum = self ._get_energy_rates_between_first_and_second_maximum (
69
+ upcoming_energy_rates , first_iteration
70
+ )
60
71
minimum_of_energy_rates = self .get_global_minimum_of_energy_rates (
61
72
energy_rates_between_first_and_second_maximum
62
73
)
@@ -69,6 +80,25 @@ def get_timestamp_of_next_price_minimum(self, first_iteration: bool = False) ->
69
80
70
81
return minimum_of_energy_rates .timestamp , minimum_has_to_be_rechecked
71
82
83
+ @staticmethod
84
+ def _check_if_next_three_prices_are_greater_than_current_one (all_upcoming_energy_rates : list [EnergyRate ]) -> bool :
85
+ """
86
+ Args:
87
+ all_upcoming_energy_rates (list[EnergyRate]): List of upcoming energy rates.
88
+
89
+ Returns:
90
+ bool: True if the average of the second, third, and fourth rates is higher than the first rate.
91
+ """
92
+ future_energy_rates_to_consider = 3
93
+ if len (all_upcoming_energy_rates ) < future_energy_rates_to_consider + 1 :
94
+ return False # Not enough data to compare, should never happen
95
+
96
+ considered_upcoming_energy_rates = all_upcoming_energy_rates [1 : future_energy_rates_to_consider + 1 ]
97
+ average_of_considered_upcoming_energy_rates = sum (
98
+ energy_rate .rate for energy_rate in considered_upcoming_energy_rates
99
+ ) / len (considered_upcoming_energy_rates )
100
+ return average_of_considered_upcoming_energy_rates > all_upcoming_energy_rates [0 ].rate
101
+
72
102
def _fetch_upcoming_prices_from_api (self ) -> dict :
73
103
"""
74
104
This method constructs a GraphQL query to retrieve the electricity prices for the current day and the following
@@ -206,6 +236,17 @@ def _find_energy_rates_till_first_maximum(
206
236
207
237
return energy_rates_till_maximum
208
238
239
+ def determine_if_average_of_next_few_prices_higher (self , upcoming_energy_rates : list [EnergyRate ]) -> bool :
240
+ """
241
+ Determines whether the average of the 2nd and 3rd energy rates is higher than the first one.
242
+
243
+ Args:
244
+ upcoming_energy_rates (list[EnergyRate]): List of upcoming energy rates.
245
+
246
+ Returns:
247
+ bool: True if the average of the 2nd and 3rd rates is higher than the 1st rate, otherwise False.
248
+ """
249
+
209
250
def get_global_minimum_of_energy_rates (self , energy_rates_till_maximum : list [EnergyRate ]) -> EnergyRate :
210
251
"""
211
252
Determines the global minimum energy rate from a list of energy rates (in this case up until the first maximum).
@@ -260,10 +301,3 @@ def _check_if_minimum_is_at_end_of_day_and_energy_rates_of_tomorrow_are_unavaila
260
301
self .log .trace (f"The price rates for tomorrow are unavailable: { are_tomorrows_rates_unavailable } " )
261
302
262
303
return is_price_minimum_near_end_of_day and are_tomorrows_rates_unavailable
263
-
264
-
265
- if __name__ == "__main__" :
266
- api_handler = TibberAPIHandler ()
267
- timestamp , minimum_has_to_be_rechecked = api_handler .get_timestamp_of_next_price_minimum (first_iteration = True )
268
- print (f"The timestamp of the next minimum energy rate is { timestamp } " )
269
- print (f"The minimum has to be re-checked: { minimum_has_to_be_rechecked } " )
0 commit comments