Skip to content

chargelog: fix calc energy mix#3185

Open
LKuemmel wants to merge 2 commits intoopenWB:masterfrom
LKuemmel:fix_chargelog
Open

chargelog: fix calc energy mix#3185
LKuemmel wants to merge 2 commits intoopenWB:masterfrom
LKuemmel:fix_chargelog

Conversation

@LKuemmel
Copy link
Contributor

@LKuemmel LKuemmel commented Mar 6, 2026

#66000952

  • Test mit realen Ladevorgängen

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts how the charge log’s energy mix (“power_source”) is calculated, switching from measurement-log-derived totals to a calculation based on charged_energy_by_source and imported_since_mode_switch in the current session.

Changes:

  • Remove dependency on analyse_percentage(get_log_from_date_until_now(...)) for computing the energy mix.
  • Add _calc_power_source_percentages(log_data) to compute mix from accumulated charged_energy_by_source.
  • Use the new calculation when creating charge log entries.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +206 to +208
power_source = {}
for source in ENERGY_SOURCES:
power_source[source] = log_data.charged_energy_by_source[source] / log_data.imported_since_mode_switch
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_calc_power_source_percentages can raise ZeroDivisionError when log_data.imported_since_mode_switch is 0/0.0 (e.g., export-only sessions, or very small imports that were rounded to 0.0 in _create_entry). Right now this is only handled indirectly via get_value_or_default, which will emit a full exception log for a normal/expected scenario. Consider explicitly guarding on imported_since_mode_switch <= 0 and 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.

Suggested change
power_source = {}
for source in ENERGY_SOURCES:
power_source[source] = log_data.charged_energy_by_source[source] / log_data.imported_since_mode_switch
"""Calculate share of each energy source in imported energy.
Returns a dict mapping each source to its fraction of the total imported
energy since the last mode switch. If the imported energy is zero or
negative (e.g. export-only session), all fractions are returned as 0.0.
"""
# Avoid ZeroDivisionError in export-only or zero-import sessions.
if getattr(log_data, "imported_since_mode_switch", 0) <= 0:
return {source: 0.0 for source in ENERGY_SOURCES}
total_imported = log_data.imported_since_mode_switch
power_source: Dict[str, float] = {}
for source in ENERGY_SOURCES:
# Use .get to be robust if a source key is missing.
charged_by_source = log_data.charged_energy_by_source.get(source, 0.0)
ratio = charged_by_source / total_imported
# Round to keep JSON output stable/consistent with other energy-mix calculations.
power_source[source] = round(ratio, 4)

Copilot uses AI. Check for mistakes.
Comment on lines +254 to +255
energy_source = get_value_or_default(lambda: _calc_power_source_percentages(log_data), {
source: 0 for source in ENERGY_SOURCES})
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The energy-mix for the charge log entry now comes from charged_energy_by_source / imported_since_mode_switch, which is a behavioral change from using the measurement-log totals. Since this file already has unit tests for cost/source calculations (chargelog_test.py), please add coverage for the new log-entry mix calculation (including edge cases like imported_since_mode_switch == 0 and small rounded values) to prevent regressions.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants