Skip to content

Commit 47d0620

Browse files
authored
electricity pricing: update pricces 4 times per day (openWB#3083)
1 parent 4bfc0d5 commit 47d0620

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

packages/control/optional.py

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from control import data
1010
from control.ocpp import OcppMixin
11-
from control.optional_data import TARIFF_UPDATE_HOUR, FlexibleTariff, GridFee, OptionalData, PricingGet
11+
from control.optional_data import FlexibleTariff, GridFee, OptionalData, PricingGet
1212
from helpermodules import hardware_configuration
1313
from helpermodules.constants import NO_ERROR
1414
from helpermodules.pub import Pub
@@ -235,37 +235,23 @@ def ep_get_loading_hours(self, duration: float, remaining_time: float) -> List[i
235235
return []
236236

237237
def et_price_update_required(self) -> bool:
238-
def is_tomorrow(last_timestamp: str) -> bool:
239-
return (day_of(date=datetime.now()) < day_of(datetime.fromtimestamp(float(last_timestamp)))
240-
or day_of(date=datetime.now()).hour < TARIFF_UPDATE_HOUR)
241-
242-
def day_of(date: datetime) -> datetime:
243-
return date.replace(hour=0, minute=0, second=0, microsecond=0)
244-
245-
def get_last_entry_time_stamp() -> str:
246-
last_known_timestamp = "0"
247-
if self.data.electricity_pricing.get.prices is not None:
248-
last_known_timestamp = max(self.data.electricity_pricing.get.prices)
249-
return last_known_timestamp
250238
self._set_ep_configured()
251239
if self.data.electricity_pricing.configured is False:
252240
return False
253241
if len(self.data.electricity_pricing.get.prices) == 0:
254242
return True
255243
if self.data.electricity_pricing.get.next_query_time is None:
256244
return True
257-
if is_tomorrow(get_last_entry_time_stamp()):
258-
if timecheck.create_timestamp() > self.data.electricity_pricing.get.next_query_time:
259-
next_query_formatted = datetime.fromtimestamp(
260-
self.data.electricity_pricing.get.next_query_time).strftime("%Y%m%d-%H:%M:%S")
261-
log.info(f'Wartezeit {next_query_formatted} abgelaufen, Strompreise werden abgefragt')
262-
return True
263-
else:
264-
next_query_formatted = datetime.fromtimestamp(
265-
self.data.electricity_pricing.get.next_query_time).strftime("%Y%m%d-%H:%M:%S")
266-
log.info(f'Nächster Abruf der Strompreise {next_query_formatted}.')
267-
return False
268-
return False
245+
if timecheck.create_timestamp() > self.data.electricity_pricing.get.next_query_time:
246+
next_query_formatted = datetime.fromtimestamp(
247+
self.data.electricity_pricing.get.next_query_time).strftime("%Y%m%d-%H:%M:%S")
248+
log.info(f'Wartezeit {next_query_formatted} abgelaufen, Strompreise werden abgefragt')
249+
return True
250+
else:
251+
next_query_formatted = datetime.fromtimestamp(
252+
self.data.electricity_pricing.get.next_query_time).strftime("%Y%m%d-%H:%M:%S")
253+
log.info(f'Nächster Abruf der Strompreise {next_query_formatted}.')
254+
return False
269255

270256
def ocpp_transfer_meter_values(self):
271257
try:

packages/control/optional_data.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from helpermodules.pub import Pub
99
from modules.display_themes.cards.config import CardsDisplayTheme
1010

11-
TARIFF_UPDATE_HOUR = 14 # latest expected time for daily tariff update
11+
# Stunden für tägliche Tarifaktualisierung, manche Anbieter aktualisieren mehrfach täglich
12+
TARIFF_UPDATE_HOURS = [2, 8, 14, 20]
1213

1314

1415
@dataclass
@@ -53,13 +54,21 @@ def prices(self) -> Dict:
5354
def prices(self, value: Dict):
5455
self._prices = value
5556
if value:
56-
next_query_time = datetime.fromtimestamp(float(max(value))).replace(
57-
hour=TARIFF_UPDATE_HOUR, minute=0, second=0
58-
) + timedelta(
59-
# actully ET providers issue next day prices up to half an hour earlier then 14:00
60-
# reduce serverload on their site by trying early and randomizing query time
61-
minutes=random.randint(1, 7) * -5
62-
)
57+
now = datetime.now()
58+
current_hour = now.hour
59+
next_hour = None
60+
for hour in TARIFF_UPDATE_HOURS:
61+
if hour > current_hour:
62+
next_hour = hour
63+
break
64+
# Wenn keine Stunde heute gefunden, nimm die erste Stunde vom nächsten Tag
65+
if next_hour is None:
66+
next_hour = TARIFF_UPDATE_HOURS[0]
67+
next_query_time = (now + timedelta(days=1)).replace(hour=next_hour, minute=0, second=0, microsecond=0)
68+
else:
69+
next_query_time = now.replace(hour=next_hour, minute=0, second=0, microsecond=0)
70+
# reduce serverload on their site by trying early and randomizing query time
71+
next_query_time += timedelta(minutes=random.randint(1, 7) * -5)
6372
Pub().pub("openWB/set/optional/ep/get/next_query_time", next_query_time.timestamp())
6473

6574

0 commit comments

Comments
 (0)