Skip to content

Commit 1606e94

Browse files
authored
fix pv yields: yields for inverters added in the current month or year (#1654)
* fix pv yields: yields for inverters added in the current month or year * consider first day when no monthly logfile exists
1 parent a5e54d5 commit 1606e94

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

packages/helpermodules/measurement_logging/update_yields.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,22 @@ def _update_pv_monthly_yields():
8282
try:
8383
with open(f"data/monthly_log/{timecheck.create_timestamp_YYYYMM()}.json", "r") as f:
8484
monthly_log = json.load(f)
85-
monthly_yield = data.data.pv_all_data.data.get.exported - monthly_log["entries"][0]["pv"]["all"]["exported"]
85+
for entry in monthly_log["entries"]:
86+
# erster Eintrag des Moduls im Monat, falls ein Modul im laufenden Monat hinzugefügt wurde
87+
if entry["pv"].get("all"):
88+
monthly_yield = data.data.pv_all_data.data.get.exported - entry["pv"]["all"]["exported"]
89+
break
8690
Pub().pub("openWB/set/pv/get/monthly_exported", monthly_yield)
8791
for pv_module in data.data.pv_data.values():
88-
for i in range(0, len(monthly_log["entries"])):
89-
# erster Eintrag im Monat, in dem das PV-Modul existiert (falls ein Modul im laufenden Monat hinzugefügt
90-
# wurde)
91-
if monthly_log["entries"][i]["pv"].get(f"pv{pv_module.num}"):
92+
for entry in monthly_log["entries"]:
93+
if entry["pv"].get(f"pv{pv_module.num}"):
9294
monthly_yield = data.data.pv_data[f"pv{pv_module.num}"].data.get.exported - \
93-
monthly_log["entries"][i]["pv"][f"pv{pv_module.num}"]["exported"]
95+
entry["pv"][f"pv{pv_module.num}"]["exported"]
9496
Pub().pub(f"openWB/set/pv/{pv_module.num}/get/monthly_exported", monthly_yield)
9597
break
98+
except FileNotFoundError:
99+
# am Tag der Ersteinrichtung gibt es noch kein Monatslog-File, das wird erst um Mitternacht erstellt.
100+
log.debug("No monthly logfile found for calculation of monthly yield")
96101
except Exception:
97102
log.exception("Fehler beim Veröffentlichen der monatlichen Erträge für PV")
98103

@@ -101,12 +106,11 @@ def pub_yearly_module_yield(sorted_path_list: List[str], pv_module: Pv):
101106
for path in sorted_path_list:
102107
with open(path, "r") as f:
103108
monthly_log = json.load(f)
104-
for i in range(0, len(monthly_log["entries"])):
105-
# erster Eintrag im Jahr, in dem das PV-Modul existiert (falls ein Modul im laufenden Jahr hinzugefügt
106-
# wurde)
107-
if monthly_log["entries"][i]["pv"].get(f"pv{pv_module.num}"):
109+
for entry in monthly_log["entries"]:
110+
# erster Eintrag mit PV im Jahr,falls WR erst im laufenden Jahr hinzugefügt wurden
111+
if entry["pv"].get(f"pv{pv_module.num}"):
108112
yearly_yield = data.data.pv_data[f"pv{pv_module.num}"].data.get.exported - \
109-
monthly_log["entries"][i]["pv"][f"pv{pv_module.num}"]["exported"]
113+
entry["pv"][f"pv{pv_module.num}"]["exported"]
110114
Pub().pub(f"openWB/set/pv/{pv_module.num}/get/yearly_exported", yearly_yield)
111115
return
112116

@@ -117,13 +121,27 @@ def _update_pv_yearly_yields():
117121
try:
118122
path_list = list(Path(_get_parent_path()/"data"/"monthly_log").glob(f"{timecheck.create_timestamp_YYYY()}*"))
119123
sorted_path_list = sorted([str(p) for p in path_list])
120-
with open(sorted_path_list[0], "r") as f:
121-
monthly_log = json.load(f)
122-
yearly_yield = data.data.pv_all_data.data.get.exported - monthly_log["entries"][0]["pv"]["all"]["exported"]
123-
Pub().pub("openWB/set/pv/get/yearly_exported", yearly_yield)
124-
log.debug(f"sorted_path_list{sorted_path_list}")
125-
for pv_module in data.data.pv_data.values():
126-
pub_yearly_module_yield(sorted_path_list, pv_module)
124+
found_pv = False
125+
for path in sorted_path_list:
126+
with open(path, "r") as f:
127+
monthly_log = json.load(f)
128+
for entry in monthly_log["entries"]:
129+
if entry["pv"].get("all"):
130+
yearly_yield = data.data.pv_all_data.data.get.exported - entry["pv"]["all"]["exported"]
131+
Pub().pub("openWB/set/pv/get/yearly_exported", yearly_yield)
132+
found_pv = True
133+
break
134+
if found_pv:
135+
break
136+
else:
137+
# am Tag der Ersteinrichtung gibt es noch kein Monatslog-File, das wird erst um Mitternacht erstellt.
138+
log.debug("No monthly logfile found for calculation of yearly yield")
139+
return
140+
if found_pv:
141+
for pv_module in data.data.pv_data.values():
142+
pub_yearly_module_yield(sorted_path_list, pv_module)
143+
else:
144+
log.debug("PV not found in any entry or file for calculation of yearly yield")
127145
except Exception:
128146
log.exception("Fehler beim Veröffentlichen der jährlichen Erträge für PV")
129147

0 commit comments

Comments
 (0)