Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature home consumption in log data #1117

Merged
merged 9 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/control/bat_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ def calc_power_for_all_components(self):
self.data.get.power += battery.data.get.power
self.data.get.imported += battery.data.get.imported
self.data.get.exported += battery.data.get.exported
self.data.get.daily_exported += battery.data.get.daily_exported
self.data.get.daily_imported += battery.data.get.daily_imported
soc_sum += battery.data.get.soc
soc_count += 1
except Exception:
log.exception(f"Fehler im Bat-Modul {battery.num}")
self.data.get.soc = int(soc_sum / soc_count)
# Alle Summen-Topics im Dict veröffentlichen
{Pub().pub("openWB/set/bat/get/"+k, v) for (k, v) in asdict(self.data.get).items()}
Pub().pub("openWB/set/bat/get/power", self.data.get.power)
Pub().pub("openWB/set/bat/get/exported", self.data.get.exported)
Pub().pub("openWB/set/bat/get/imported", self.data.get.imported)
Pub().pub("openWB/set/bat/get/soc", self.data.get.soc)
else:
self.data.config.configured = False
Pub().pub("openWB/set/bat/config/configured", self.data.config.configured)
Expand Down
36 changes: 8 additions & 28 deletions packages/control/counter_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from helpermodules.pub import Pub
from modules.common.component_type import ComponentType, component_type_to_readable_text
from modules.common.fault_state import FaultStateLevel
from modules.common.simcount import SimCounter

log = logging.getLogger(__name__)

Expand All @@ -32,6 +33,7 @@ class Set:
home_consumption: float = 0
invalid_home_consumption: int = 0
daily_yield_home_consumption: float = 0
imported_home_consumption: float = 0
disengageable_smarthome_power: float = 0


Expand Down Expand Up @@ -65,6 +67,8 @@ def __init__(self):
self.connected_counters = []
self.connected_chargepoints = []
self.childless = []
self.sim_counter = SimCounter("", "", prefix="bezug")
self.sim_counter.topic = "openWB/set/counter/set/"

def get_evu_counter(self) -> Counter:
return data.data.counter_data[f"counter{self.get_id_evu_counter()}"]
Expand Down Expand Up @@ -120,6 +124,9 @@ def set_home_consumption(self) -> None:
self.data.set.invalid_home_consumption)
self.data.set.home_consumption = home_consumption
Pub().pub("openWB/set/counter/set/home_consumption", self.data.set.home_consumption)
imported, _ = self.sim_counter.sim_count(self.data.set.home_consumption)
Pub().pub("openWB/set/counter/set/imported_home_consumption", imported)
self.data.set.imported_home_consumption = imported
except Exception:
log.exception("Fehler in der allgemeinen Zähler-Klasse")

Expand Down Expand Up @@ -154,35 +161,8 @@ def _get_elements_for_home_consumption_calculation(self):
elements_to_sum_up.extend(self._add_hybrid_bat(element['id']))
return elements_to_sum_up

def calc_daily_yield_home_consumption(self) -> None:
""" daily_yield_home_consumption = (evu_imported + pv - cp_imported + cp_exported + bat_exported
- bat_imported - evu_exported)
"""
def sum_up_imported_exported(component):
self.daily_yield_home_consumption -= component.data.get.daily_imported
self.daily_yield_home_consumption += component.data.get.daily_exported
self.daily_yield_home_consumption = 0
try:
self.daily_yield_home_consumption += data.data.counter_data[self.get_evu_counter_str()
].data.get.daily_imported
self.daily_yield_home_consumption -= data.data.counter_data[self.get_evu_counter_str()
].data.get.daily_exported
elements_to_sum_up = self._get_elements_for_home_consumption_calculation()
for element in elements_to_sum_up:
if element["type"] == ComponentType.CHARGEPOINT.value:
sum_up_imported_exported(data.data.cp_data[f"cp{element['id']}"])
elif element["type"] == ComponentType.BAT.value:
sum_up_imported_exported(data.data.bat_data[f"bat{element['id']}"])
elif element["type"] == ComponentType.COUNTER.value:
sum_up_imported_exported(data.data.counter_data[f"counter{element['id']}"])
elif element["type"] == ComponentType.INVERTER.value:
self.daily_yield_home_consumption += data.data.pv_data[f"pv{element['id']}"].data.get.daily_exported
Pub().pub("openWB/set/counter/set/daily_yield_home_consumption", self.daily_yield_home_consumption)
self.data.set.daily_yield_home_consumption = self.daily_yield_home_consumption
except Exception:
log.exception("Fehler in der allgemeinen Zähler-Klasse")

# Hierarchie analysieren

def get_all_elements_without_children(self, id: int) -> List[Dict]:
self.childless.clear()
self.get_all_elements_without_children_recursive(self.get_entry_of_element(id))
Expand Down
15 changes: 0 additions & 15 deletions packages/control/counter_home_consumption_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,3 @@ def test_set_home_consumption(home_consumption: int,
# evaluation
assert c.data.set.invalid_home_consumption == expected_invalid_home_consumption
assert c.data.set.home_consumption == expected_home_consumption


@pytest.mark.parametrize("counter_all",
[pytest.param(hierarchy_standard, id="standard"),
pytest.param(hierarchy_hybrid, id="hybrid"),
pytest.param(hierarchy_nested, id="nested")])
def testcalc_daily_yield_home_consumption(counter_all: Callable[[], CounterAll], data_):
#
c = counter_all()

# execution
c.calc_daily_yield_home_consumption()

# evaluation
assert c.data.set.daily_yield_home_consumption == 14000
7 changes: 2 additions & 5 deletions packages/control/pv_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,10 @@ def calc_power_for_all_components(self) -> None:
module_data = data.data.pv_data[module].data
self.data.get.power += module_data.get.power
self.data.get.exported += module_data.get.exported
self.data.get.daily_exported += module_data.get.daily_exported
self.data.get.monthly_exported += module_data.get.monthly_exported
self.data.get.yearly_exported += module_data.get.yearly_exported
except Exception:
log.exception("Fehler im allgemeinen PV-Modul für "+str(module))
# Alle Summen-Topics im Dict veröffentlichen
{Pub().pub("openWB/set/pv/get/"+k, v) for (k, v) in asdict(self.data.get).items()}
Pub().pub("openWB/set/pv/get/power", self.data.get.power)
Pub().pub("openWB/set/pv/get/exported", self.data.get.exported)
self.data.config.configured = True
Pub().pub("openWB/set/pv/config/configured", self.data.config.configured)
else:
Expand Down
20 changes: 15 additions & 5 deletions packages/helpermodules/measurement_logging/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def daily_log_sample():
"pv": {"pv1": {"exported": 804}, "all": {"exported": 804}},
"bat": {"bat2": {"imported": 2.42, "exported": 1742.135, "soc": 15},
"all": {"imported": 2.42, "exported": 1742.135, "soc": 15}},
"sh": {"sh1": {"temp0": 300, "temp1": 300, "temp2": 300, "imported": 0.1, "exported": 0}}},
"sh": {"sh1": {"temp0": 300, "temp1": 300, "temp2": 300, "imported": 0.1, "exported": 0}},
"hc": {"all": {"imported": 100}}},
{"timestamp": 1690530060,
"date": "09:40",
"cp": {
Expand All @@ -45,7 +46,8 @@ def daily_log_sample():
"pv": {"pv1": {"exported": 930}, "all": {"exported": 930}},
"bat": {"bat2": {"imported": 2.42, "exported": 2017.569, "soc": 10},
"all": {"imported": 2.42, "exported": 2017.569, "soc": 10}},
"sh": {"sh1": {"temp0": 300, "temp1": 300, "temp2": 300, "imported": 0.2, "exported": 0}}},
"sh": {"sh1": {"temp0": 300, "temp1": 300, "temp2": 300, "imported": 0.2, "exported": 0}},
"hc": {"all": {"imported": 110}}},
{"timestamp": 1690530360,
"date": "09:45",
"cp": {
Expand All @@ -58,7 +60,8 @@ def daily_log_sample():
"pv": {"pv1": {"exported": 1055}, "all": {"exported": 1055}},
"bat": {"bat2": {"imported": 2.42, "exported": 2292.992, "soc": 4},
"all": {"imported": 2.42, "exported": 2292.992, "soc": 4}},
"sh": {"sh1": {"temp0": 300, "temp1": 300, "temp2": 300, "imported": 0.4, "exported": 0}}}
"sh": {"sh1": {"temp0": 300, "temp1": 300, "temp2": 300, "imported": 0.4, "exported": 0}},
"hc": {"all": {"imported": 120}}}
]


Expand All @@ -73,7 +76,8 @@ def daily_log_totals():
'cp5': {'exported': 0, 'imported': 191.928},
'cp6': {'exported': 0, 'imported': 0}},
'pv': {'all': {'exported': 251}, 'pv1': {'exported': 251}},
"sh": {"sh1": {"imported": 0.3, "exported": 0}}}
"sh": {"sh1": {"imported": 0.3, "exported": 0}},
"hc": {"all": {"imported": 20}}}


@pytest.fixture()
Expand Down Expand Up @@ -188,4 +192,10 @@ def daily_log_entry_kw():
"energy_imported": 0.0001,
"energy_exported": 0
}
}}
},
'hc': {'all': {'energy_exported': 0.0,
'energy_imported': 0.01,
'imported': 100,
'power_average': 0.12040133779264214,
'power_exported': 0,
'power_imported': 0.12040133779264214}}}
142 changes: 74 additions & 68 deletions packages/helpermodules/measurement_logging/process_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,45 @@ def string_to_int(value: str, default: int = 0) -> int:


def get_totals(entries: List) -> Dict:
totals: Dict[str, Dict] = {"cp": {}, "counter": {}, "pv": {}, "bat": {}, "sh": {}}
totals: Dict[str, Dict] = {"cp": {}, "counter": {}, "pv": {}, "bat": {}, "sh": {}, "hc": {}}
prev_entry: Dict = {}
for group in totals.keys():
for entry in entries:
for module in entry[group]:
try:
if not prev_entry or module not in totals[group]:
totals[group][module] = {"exported": 0} if group == "pv" else {"imported": 0, "exported": 0}
else:
for key, value in entry[group][module].items():
if key != "soc" and key != "grid" and "temp" not in key:
if value == "":
# Manchmal fehlen Werte im alten Log
value = 0
try:
prev_value = prev_entry[group][module][key]
# Wenn ein Modul neu hinzugefügt wurde, das es mit dieser ID schonmal gab, werden
# die Werte zusammen addiert.
except KeyError:
prev_value = entry[group][module][key]
if prev_value == "":
# Manchmal fehlen Werte im alten Log
prev_value = 0
# avoid floating point issues with using Decimal
value = (Decimal(str(value))
- Decimal(str(prev_value))
+ Decimal(str(totals[group][module][key])))
value = f'{value: f}'
# remove trailing zeros
totals[group][module][key] = string_to_float(
value) if "." in value else string_to_int(value)
except Exception:
log.exception(f"Fehler beim Berechnen der Summe von {module}")
if group in entry:
for module in entry[group]:
try:
if not prev_entry or module not in totals[group]:
if group == "hc":
totals[group][module] = {"imported": 0}
elif group == "pv":
totals[group][module] = {"exported": 0}
else:
totals[group][module] = {"imported": 0, "exported": 0}
else:
for key, value in entry[group][module].items():
if key != "soc" and key != "grid" and "temp" not in key:
if value == "":
# Manchmal fehlen Werte im alten Log
value = 0
try:
prev_value = prev_entry[group][module][key]
# Wenn ein Modul neu hinzugefügt wurde, das es mit dieser ID schonmal gab, werden
# die Werte zusammen addiert.
except KeyError:
prev_value = entry[group][module][key]
if prev_value == "":
# Manchmal fehlen Werte im alten Log
prev_value = 0
# avoid floating point issues with using Decimal
value = (Decimal(str(value))
- Decimal(str(prev_value))
+ Decimal(str(totals[group][module][key])))
value = f'{value: f}'
# remove trailing zeros
totals[group][module][key] = string_to_float(
value) if "." in value else string_to_int(value)
except Exception:
log.exception(f"Fehler beim Berechnen der Summe von {module}")
prev_entry = entry
return totals

Expand Down Expand Up @@ -265,44 +271,44 @@ def _process_entries(data, calculation):

def _process_entry(entry: dict, next_entry: dict, calculation: CalculationType):
time_diff = next_entry["timestamp"] - entry["timestamp"]
for type in ("bat", "counter", "cp", "pv", "sh"):
for module in entry[type].keys():
try:
new_data = {}
if "imported" in entry[type][module].keys() or "exported" in entry[type][module].keys():
try:
value_imported = entry[type][module]["imported"]
except KeyError:
value_imported = 0
try:
next_value_imported = next_entry[type][module]["imported"]
except KeyError:
next_value_imported = value_imported
try:
value_exported = entry[type][module]["exported"]
except KeyError:
value_exported = 0
try:
next_value_exported = next_entry[type][module]["exported"]
except KeyError:
next_value_exported = value_exported
average_power = _calculate_average_power(time_diff, value_imported, next_value_imported,
value_exported, next_value_exported)
if calculation in [CalculationType.POWER, CalculationType.ALL]:
new_data.update({
"power_average": average_power,
"power_imported": average_power if average_power >= 0 else 0,
"power_exported": average_power * -1 if average_power < 0 else 0
})
if calculation in [CalculationType.ENERGY, CalculationType.ALL]:
new_data.update({
"energy_imported": _calculate_energy_difference(value_imported, next_value_imported),
"energy_exported": _calculate_energy_difference(value_exported, next_value_exported)
})
entry[type][module].update(new_data)
except Exception:
log.exception("Fehler beim Berechnen der Leistung")
# ToDo: add home consumption
for type in ("bat", "counter", "cp", "pv", "sh", "hc"):
if type in entry:
for module in entry[type].keys():
try:
new_data = {}
if "imported" in entry[type][module].keys() or "exported" in entry[type][module].keys():
try:
value_imported = entry[type][module]["imported"]
except KeyError:
value_imported = 0
try:
next_value_imported = next_entry[type][module]["imported"]
except KeyError:
next_value_imported = value_imported
try:
value_exported = entry[type][module]["exported"]
except KeyError:
value_exported = 0
try:
next_value_exported = next_entry[type][module]["exported"]
except KeyError:
next_value_exported = value_exported
average_power = _calculate_average_power(time_diff, value_imported, next_value_imported,
value_exported, next_value_exported)
if calculation in [CalculationType.POWER, CalculationType.ALL]:
new_data.update({
"power_average": average_power,
"power_imported": average_power if average_power >= 0 else 0,
"power_exported": average_power * -1 if average_power < 0 else 0
})
if calculation in [CalculationType.ENERGY, CalculationType.ALL]:
new_data.update({
"energy_imported": _calculate_energy_difference(value_imported, next_value_imported),
"energy_exported": _calculate_energy_difference(value_exported, next_value_exported)
})
entry[type][module].update(new_data)
except Exception:
log.exception("Fehler beim Berechnen der Leistung")
return entry


Expand Down
Loading