Optimization of the Inverter class to speed up scr calculation (#607)
Some checks failed
docker-build / platform-excludes (push) Has been cancelled
pre-commit / pre-commit (push) Has been cancelled
Run Pytest on Pull Request / test (push) Has been cancelled
docker-build / build (push) Has been cancelled
docker-build / merge (push) Has been cancelled

* fix(ElecPriceEnergyCharts): get history series, update docs

Signed-off-by: redmoon2711 <redmoon2711@gmx.de>

* feat(inverter): using lookup table for calculate scr

Signed-off-by: redmoon2711 <redmoon2711@gmx.de>

---------

Signed-off-by: redmoon2711 <redmoon2711@gmx.de>
This commit is contained in:
redmoon2711 2025-06-25 21:13:11 +02:00 committed by GitHub
parent eb23123bba
commit ba4f5ecbb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -25,6 +25,22 @@ class Inverter(DeviceBase):
self.parameters: Optional[InverterParameters] = None self.parameters: Optional[InverterParameters] = None
super().__init__(parameters) super().__init__(parameters)
self.scr_lookup: dict = {}
def _calculate_scr(self, consumption: float, generation: float) -> float:
"""Check if the consumption and production is in the lookup table. If not, calculate and store the value."""
if consumption not in self.scr_lookup:
self.scr_lookup[consumption] = {}
if generation not in self.scr_lookup[consumption]:
scr = self.self_consumption_predictor.calculate_self_consumption(
consumption, generation
)
self.scr_lookup[consumption][generation] = scr
return scr
return self.scr_lookup[consumption][generation]
def _setup(self) -> None: def _setup(self) -> None:
if self.parameters is None: if self.parameters is None:
raise ValueError(f"Parameters not set: {self.parameters}") raise ValueError(f"Parameters not set: {self.parameters}")
@ -60,9 +76,8 @@ class Inverter(DeviceBase):
grid_import = -remaining_power # Negative indicates feeding into the grid grid_import = -remaining_power # Negative indicates feeding into the grid
self_consumption = self.max_power_wh self_consumption = self.max_power_wh
else: else:
scr = self.self_consumption_predictor.calculate_self_consumption( # Calculate scr with lookup table
consumption, generation scr = self._calculate_scr(consumption, generation)
)
# Remaining power after consumption # Remaining power after consumption
remaining_power = (generation - consumption) * scr # EVQ remaining_power = (generation - consumption) * scr # EVQ