Skip to content

Commit f9d0b98

Browse files
committed
Remove the maximum charging duration limitation and set it fixed to 2 hours
1 parent 2334c0f commit f9d0b98

File tree

3 files changed

+9
-49
lines changed

3 files changed

+9
-49
lines changed

source/energy_classes.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass
4-
from datetime import datetime, timedelta
4+
from datetime import datetime
55

66
from environment_variable_getter import EnvironmentVariableGetter
77
from logger import LoggerMixin
@@ -64,7 +64,6 @@ def from_kilo_watts(kilo_watts: float) -> Power:
6464
class EnergyRate:
6565
rate: float
6666
timestamp: datetime
67-
maximum_charging_duration: timedelta = timedelta(hours=1)
6867
has_to_be_rechecked: bool = False
6968

7069
def __repr__(self):
@@ -76,12 +75,6 @@ def __lt__(self, other: EnergyRate) -> bool:
7675
def __gt__(self, other: EnergyRate) -> bool:
7776
return self.rate > other.rate
7877

79-
def format_maximum_charging_duration(self) -> str:
80-
charging_duration_in_hours = int(self.maximum_charging_duration.total_seconds() // 3600)
81-
if charging_duration_in_hours == 1:
82-
return "1 hour"
83-
return f"{charging_duration_in_hours} hours"
84-
8578

8679
soc_logger = LoggerMixin("StateOfCharge")
8780
battery_capacity = EnergyAmount(int(EnvironmentVariableGetter.get("INVERTER_BATTERY_CAPACITY")))

source/inverter_charge_controller.py

+7-18
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,6 @@ def _do_iteration(self) -> None:
121121
self.next_price_minimum = self._get_next_price_minimum()
122122
self.log.info(f"The next price minimum is at {self.next_price_minimum}")
123123

124-
self.tibber_api_handler.set_maximum_charging_duration_of_current_energy_rate(current_energy_rate)
125-
self.log.info(
126-
f"The maximum charging duration of the current energy rate is "
127-
f"{current_energy_rate.maximum_charging_duration}"
128-
)
129-
130124
current_state_of_charge = self.inverter.get_state_of_charge()
131125
self.log.info(f"The battery is currently is at {current_state_of_charge}")
132126

@@ -163,7 +157,6 @@ def _do_iteration(self) -> None:
163157
"timestamp now": str(timestamp_now),
164158
"next price minimum": self.next_price_minimum,
165159
"next price minimum has to be rechecked": self.next_price_minimum.has_to_be_rechecked,
166-
"maximum charging duration": current_energy_rate.format_maximum_charging_duration(),
167160
"current state of charge": current_state_of_charge,
168161
"average power consumption": average_power_consumption.watts,
169162
"target min soc": target_min_soc,
@@ -186,7 +179,7 @@ def _do_iteration(self) -> None:
186179
if charging_target_soc is None:
187180
return
188181

189-
self.handle_charging(charging_target_soc, current_energy_rate.maximum_charging_duration)
182+
self.handle_charging(charging_target_soc)
190183

191184
self.iteration_cache = {}
192185

@@ -292,20 +285,19 @@ def _calculate_target_soc(
292285

293286
return charging_target_soc
294287

295-
def handle_charging(self, charging_target_soc: StateOfCharge, maximum_charging_duration: timedelta) -> None:
288+
def handle_charging(self, charging_target_soc: StateOfCharge) -> None:
296289
"""
297290
Handles the charging process. This involves getting the energy bought before charging, charging the inverter,
298291
getting the energy bought after charging, and updating the database with the consumed energy data.
299292
300293
Args:
301294
charging_target_soc: Desired state of charge for the battery after charging.
302-
maximum_charging_duration: Maximum allowable duration for the charging process.
303295
"""
304296
energy_bought_before_charging = self.sems_portal_api_handler.get_energy_buy()
305297
timestamp_starting_to_charge = TimeHandler.get_time()
306298
self.log.debug(f"The amount of energy bought before charging is {energy_bought_before_charging}")
307299

308-
self._charge_inverter(charging_target_soc, maximum_charging_duration)
300+
self._charge_inverter(charging_target_soc)
309301

310302
timestamp_ending_to_charge = TimeHandler.get_time()
311303

@@ -327,7 +319,7 @@ def handle_charging(self, charging_target_soc: StateOfCharge, maximum_charging_d
327319
timestamp_starting_to_charge, timestamp_ending_to_charge, energy_bought
328320
)
329321

330-
def _charge_inverter(self, target_state_of_charge: StateOfCharge, maximum_charging_duration: timedelta) -> None:
322+
def _charge_inverter(self, target_state_of_charge: StateOfCharge) -> None:
331323
"""
332324
Charges the inverter battery to the target state of charge within a specified maximum charging duration.
333325
Monitors the charging progress at regular intervals and stops the charging process if specific conditions are
@@ -340,13 +332,10 @@ def _charge_inverter(self, target_state_of_charge: StateOfCharge, maximum_chargi
340332
Args:
341333
target_state_of_charge: The desired battery state of charge percentage to reach during the
342334
charging process.
343-
maximum_charging_duration: The maximum duration allowed for the charging process to complete.
344335
"""
345336
charging_progress_check_interval = timedelta(minutes=5)
346337

347-
maximum_end_charging_time = (
348-
TimeHandler.get_time(sanitize_seconds=True).replace(minute=0) + maximum_charging_duration
349-
)
338+
maximum_end_charging_time = TimeHandler.get_time(sanitize_seconds=True).replace(minute=0) + timedelta(hours=2)
350339

351340
self.log.info("Starting to charge")
352341
self.inverter.set_operation_mode(OperationMode.ECO_CHARGE)
@@ -367,8 +356,8 @@ def _charge_inverter(self, target_state_of_charge: StateOfCharge, maximum_chargi
367356
# Can't set the mode of the inverter as it is unresponsive
368357
break
369358

370-
# Account for program execution times, this way the check happens at 05:00 and does not add up delays
371-
# (minor cosmetics)
359+
# Account for program execution times, this way the check happens at 5-minute intervals and delays do not
360+
# add up (minor cosmetics)
372361
pause.seconds(charging_progress_check_interval.total_seconds() - TimeHandler.get_time().second)
373362

374363
try:

source/tibber_api_handler.py

+1-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import datetime, timedelta
1+
from datetime import datetime
22

33
from database_handler import DatabaseHandler, InfluxDBField
44
from energy_classes import EnergyRate
@@ -85,28 +85,6 @@ def get_next_price_minimum(self, first_iteration: bool = False) -> EnergyRate:
8585

8686
return minimum_of_energy_rates
8787

88-
def set_maximum_charging_duration_of_current_energy_rate(self, current_energy_rate: EnergyRate) -> None:
89-
"""
90-
It takes the current energy rate from the InverterChargeController and compares it against the upcoming energy
91-
rates. It then calculates and sets the maximum possible charging duration based on the comparison of the current
92-
price and the consecutive ones.
93-
94-
We have to provide the current energy rate as the upcoming energy rates (which are saved as an instance
95-
variable) only include the **upcoming** energy rates and not the current one.
96-
97-
Args:
98-
current_energy_rate (EnergyRate): The current energy rate for comparison against upcoming energy rates.
99-
"""
100-
charging_duration = timedelta(hours=1)
101-
102-
for energy_rate in self.upcoming_energy_rates_cache:
103-
if energy_rate.rate <= current_energy_rate.rate + TibberAPIHandler.MAXIMUM_THRESHOLD:
104-
charging_duration += timedelta(hours=1)
105-
else:
106-
break
107-
108-
current_energy_rate.maximum_charging_duration = charging_duration
109-
11088
@staticmethod
11189
def _check_if_next_three_prices_are_greater_than_current_one(all_upcoming_energy_rates: list[EnergyRate]) -> bool:
11290
"""

0 commit comments

Comments
 (0)