-
Notifications
You must be signed in to change notification settings - Fork 115
chargelog: fix calc energy mix #3185
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,8 +10,7 @@ | |
| from control import data | ||
| from dataclass_utils import asdict | ||
| from helpermodules.measurement_logging.process_log import ( | ||
| FILE_ERRORS, CalculationType, _analyse_energy_source, | ||
| _process_entries, analyse_percentage, get_log_from_date_until_now, get_totals) | ||
| FILE_ERRORS, CalculationType, _analyse_energy_source, _process_entries, get_totals) | ||
| from helpermodules.pub import Pub | ||
| from helpermodules import timecheck | ||
| from helpermodules.utils.json_file_handler import write_and_check | ||
|
|
@@ -203,6 +202,13 @@ def _get_range_charged(log_data, charging_ev) -> float: | |
| return None | ||
|
|
||
|
|
||
| def _calc_power_source_percentages(log_data) -> Dict[str, float]: | ||
| power_source = {} | ||
| for source in ENERGY_SOURCES: | ||
| power_source[source] = log_data.charged_energy_by_source[source] / log_data.imported_since_mode_switch | ||
| return power_source | ||
|
|
||
|
|
||
| def save_data(chargepoint, charging_ev): | ||
| """ json-Objekt für den Log-Eintrag erstellen, an die Datei anhängen und die Daten, die sich auf den Ladevorgang | ||
| beziehen, löschen. | ||
|
|
@@ -245,8 +251,8 @@ def _create_entry(chargepoint, charging_ev): | |
| # log_data.imported_since_mode_switch / (duration / 3600) | ||
| power = get_value_or_default(lambda: round(log_data.imported_since_mode_switch / (time_charged / 3600), 2)) | ||
| calc_energy_costs(chargepoint, True) | ||
| energy_source = get_value_or_default(lambda: analyse_percentage(get_log_from_date_until_now( | ||
| log_data.timestamp_mode_switch)["totals"])["energy_source"]) | ||
| energy_source = get_value_or_default(lambda: _calc_power_source_percentages(log_data), { | ||
| source: 0 for source in ENERGY_SOURCES}) | ||
|
Comment on lines
+254
to
+255
|
||
| costs = round(log_data.costs, 2) | ||
|
|
||
| new_entry = { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_calc_power_source_percentagescan raiseZeroDivisionErrorwhenlog_data.imported_since_mode_switchis0/0.0(e.g., export-only sessions, or very small imports that were rounded to0.0in_create_entry). Right now this is only handled indirectly viaget_value_or_default, which will emit a full exception log for a normal/expected scenario. Consider explicitly guarding onimported_since_mode_switch <= 0and returning the zero-dict without raising, and optionally rounding the ratios (e.g., to 4 decimals) to keep the JSON output stable/consistent with other energy-mix calculations.