Skip to content

Commit f71d1be

Browse files
authored
Merge pull request #1464 from LKuemmel/fix
fix simcount in case of error, fix zero values if there is no previous value
2 parents 58f161b + 64f156c commit f71d1be

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

packages/helpermodules/measurement_logging/write_log.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ def fix_values(new_entry: Dict, previous_entry: Dict) -> Dict:
257257
def find_and_fix_value(value_name):
258258
if value.get(value_name) is not None:
259259
if value[value_name] == 0:
260-
value[value_name] = previous_entry[group][component][value_name]
260+
try:
261+
value[value_name] = previous_entry[group][component][value_name]
262+
except KeyError:
263+
log.exception("Es konnte kein vorheriger Wert gefunden werden.")
261264
for group, value in new_entry.items():
262265
if group not in ("bat", "counter", "cp", "pv", "hc"):
263266
continue

packages/helpermodules/measurement_logging/write_log_test.py

+50
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,53 @@ def test_fix_values():
7070
'pv': {'all': {'exported': 3269}, 'pv1': {'exported': 3269}},
7171
'sh': {},
7272
'timestamp': 1709109001}
73+
74+
75+
def test_fix_values_missing_components():
76+
# setup
77+
previous_entry = {'bat': {'all': {'exported': 0, 'imported': 2281.851, 'soc': 96},
78+
'bat2': {'exported': 0, 'imported': 2281.851, 'soc': 96}},
79+
'counter': {'counter0': {'exported': 21.382,
80+
'grid': True,
81+
'imported': 17.913}},
82+
'cp': {'all': {'exported': 0, 'imported': 0},
83+
'cp3': {'exported': 0, 'imported': 0},
84+
'cp4': {'exported': 0, 'imported': 0},
85+
'cp5': {'exported': 0, 'imported': 0}},
86+
'date': '09:25',
87+
'ev': {'ev0': {'soc': 0}},
88+
'hc': {'all': {'imported': 108647.2791628165}},
89+
'pv': {'all': {'exported': 3269}},
90+
'sh': {},
91+
'timestamp': 1709108702}
92+
new_entry = {'bat': {'all': {'exported': 0, 'imported': 2369.658, 'soc': 97},
93+
'bat2': {'exported': 0, 'imported': 2369.658, 'soc': 97}},
94+
'counter': {'counter0': {'exported': 22.167, 'grid': True, 'imported': 18.54}},
95+
'cp': {'all': {'exported': 0, 'imported': 0},
96+
'cp3': {'exported': 0, 'imported': 0},
97+
'cp4': {'exported': 0, 'imported': 0},
98+
'cp5': {'exported': 0, 'imported': 0}},
99+
'date': '09:30',
100+
'ev': {'ev0': {'soc': 0}},
101+
'hc': {'all': {'imported': 108683.21291147666}},
102+
'pv': {'all': {'exported': 0}, 'pv1': {'exported': 0}},
103+
'sh': {},
104+
'timestamp': 1709109001}
105+
106+
# execution
107+
fixed_values = write_log.fix_values(new_entry, previous_entry)
108+
109+
# evaluation
110+
assert fixed_values == {'bat': {'all': {'exported': 0, 'imported': 2369.658, 'soc': 97},
111+
'bat2': {'exported': 0, 'imported': 2369.658, 'soc': 97}},
112+
'counter': {'counter0': {'exported': 22.167, 'grid': True, 'imported': 18.54}},
113+
'cp': {'all': {'exported': 0, 'imported': 0},
114+
'cp3': {'exported': 0, 'imported': 0},
115+
'cp4': {'exported': 0, 'imported': 0},
116+
'cp5': {'exported': 0, 'imported': 0}},
117+
'date': '09:30',
118+
'ev': {'ev0': {'soc': 0}},
119+
'hc': {'all': {'imported': 108683.21291147666}},
120+
'pv': {'all': {'exported': 3269}, 'pv1': {'exported': 0}},
121+
'sh': {},
122+
'timestamp': 1709109001}

packages/modules/common/simcount/_simcount.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
import time
66

7+
from control import data as data_module
78
from modules.common.simcount._calculate import calculate_import_export
89
from modules.common.simcount.simcounter_state import SimCounterState
910
from modules.common.simcount._simcounter_store import get_sim_counter_store
@@ -35,7 +36,13 @@ def sim_count(power_present: float, topic: str = "", data: SimCounterState = Non
3536
return store.initialize(prefix, topic, power_present, timestamp_present)
3637
else:
3738
log.debug("Previous state: %s", previous_state)
38-
hours_since_previous = (timestamp_present - previous_state.timestamp) / 3600
39+
control_interval = data_module.data.general_data.data.control_interval
40+
if 2 * control_interval < timestamp_present - previous_state.timestamp:
41+
log.warning("Time difference between previous state and current state is too large. "
42+
"Set time difference to control interval.")
43+
hours_since_previous = control_interval / 3600
44+
else:
45+
hours_since_previous = (timestamp_present - previous_state.timestamp) / 3600
3946
imported, exported = calculate_import_export(hours_since_previous, previous_state.power, power_present)
4047
current_state = SimCounterState(
4148
timestamp_present,

0 commit comments

Comments
 (0)