From 47820d36414545252a99291b0093481b13d45429 Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Tue, 6 Jan 2026 16:10:42 +0100 Subject: [PATCH] electricity pricing: update pricces 4 times per day --- packages/control/optional.py | 36 ++++++++++--------------------- packages/control/optional_data.py | 25 ++++++++++++++------- 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/packages/control/optional.py b/packages/control/optional.py index 044dd32a70..0c5fc12627 100644 --- a/packages/control/optional.py +++ b/packages/control/optional.py @@ -8,7 +8,7 @@ from control import data from control.ocpp import OcppMixin -from control.optional_data import TARIFF_UPDATE_HOUR, FlexibleTariff, GridFee, OptionalData, PricingGet +from control.optional_data import FlexibleTariff, GridFee, OptionalData, PricingGet from helpermodules import hardware_configuration from helpermodules.constants import NO_ERROR from helpermodules.pub import Pub @@ -235,18 +235,6 @@ def ep_get_loading_hours(self, duration: float, remaining_time: float) -> List[i return [] def et_price_update_required(self) -> bool: - def is_tomorrow(last_timestamp: str) -> bool: - return (day_of(date=datetime.now()) < day_of(datetime.fromtimestamp(float(last_timestamp))) - or day_of(date=datetime.now()).hour < TARIFF_UPDATE_HOUR) - - def day_of(date: datetime) -> datetime: - return date.replace(hour=0, minute=0, second=0, microsecond=0) - - def get_last_entry_time_stamp() -> str: - last_known_timestamp = "0" - if self.data.electricity_pricing.get.prices is not None: - last_known_timestamp = max(self.data.electricity_pricing.get.prices) - return last_known_timestamp self._set_ep_configured() if self.data.electricity_pricing.configured is False: return False @@ -254,18 +242,16 @@ def get_last_entry_time_stamp() -> str: return True if self.data.electricity_pricing.get.next_query_time is None: return True - if is_tomorrow(get_last_entry_time_stamp()): - if timecheck.create_timestamp() > self.data.electricity_pricing.get.next_query_time: - next_query_formatted = datetime.fromtimestamp( - self.data.electricity_pricing.get.next_query_time).strftime("%Y%m%d-%H:%M:%S") - log.info(f'Wartezeit {next_query_formatted} abgelaufen, Strompreise werden abgefragt') - return True - else: - next_query_formatted = datetime.fromtimestamp( - self.data.electricity_pricing.get.next_query_time).strftime("%Y%m%d-%H:%M:%S") - log.info(f'Nächster Abruf der Strompreise {next_query_formatted}.') - return False - return False + if timecheck.create_timestamp() > self.data.electricity_pricing.get.next_query_time: + next_query_formatted = datetime.fromtimestamp( + self.data.electricity_pricing.get.next_query_time).strftime("%Y%m%d-%H:%M:%S") + log.info(f'Wartezeit {next_query_formatted} abgelaufen, Strompreise werden abgefragt') + return True + else: + next_query_formatted = datetime.fromtimestamp( + self.data.electricity_pricing.get.next_query_time).strftime("%Y%m%d-%H:%M:%S") + log.info(f'Nächster Abruf der Strompreise {next_query_formatted}.') + return False def ocpp_transfer_meter_values(self): try: diff --git a/packages/control/optional_data.py b/packages/control/optional_data.py index 74eb0da6cd..e6bdf78759 100644 --- a/packages/control/optional_data.py +++ b/packages/control/optional_data.py @@ -8,7 +8,8 @@ from helpermodules.pub import Pub from modules.display_themes.cards.config import CardsDisplayTheme -TARIFF_UPDATE_HOUR = 14 # latest expected time for daily tariff update +# Stunden für tägliche Tarifaktualisierung, manche Anbieter aktualisieren mehrfach täglich +TARIFF_UPDATE_HOURS = [2, 8, 14, 20] @dataclass @@ -53,13 +54,21 @@ def prices(self) -> Dict: def prices(self, value: Dict): self._prices = value if value: - next_query_time = datetime.fromtimestamp(float(max(value))).replace( - hour=TARIFF_UPDATE_HOUR, minute=0, second=0 - ) + timedelta( - # actully ET providers issue next day prices up to half an hour earlier then 14:00 - # reduce serverload on their site by trying early and randomizing query time - minutes=random.randint(1, 7) * -5 - ) + now = datetime.now() + current_hour = now.hour + next_hour = None + for hour in TARIFF_UPDATE_HOURS: + if hour > current_hour: + next_hour = hour + break + # Wenn keine Stunde heute gefunden, nimm die erste Stunde vom nächsten Tag + if next_hour is None: + next_hour = TARIFF_UPDATE_HOURS[0] + next_query_time = (now + timedelta(days=1)).replace(hour=next_hour, minute=0, second=0, microsecond=0) + else: + next_query_time = now.replace(hour=next_hour, minute=0, second=0, microsecond=0) + # reduce serverload on their site by trying early and randomizing query time + next_query_time += timedelta(minutes=random.randint(1, 7) * -5) Pub().pub("openWB/set/optional/ep/get/next_query_time", next_query_time.timestamp())