Skip to content

Fix UnboundLocalError in check_tarif when generate_energy_meters is disabled - #1015

Open
njloof wants to merge 2 commits into
dvd-dev:mainfrom
njloof:fix/check-tarif-unbound-variable
Open

Fix UnboundLocalError in check_tarif when generate_energy_meters is disabled#1015
njloof wants to merge 2 commits into
dvd-dev:mainfrom
njloof:fix/check-tarif-unbound-variable

Conversation

@njloof

@njloof njloof commented Aug 19, 2026

Copy link
Copy Markdown

When the 'Generate energy meters' option is disabled in the integration configuration, the check_tarif() method crashes with:

UnboundLocalError: cannot access local variable 'tarif_config' where
it is not associated with a value

Root cause: the variable tarif_config (along with energy_used, plan_name, base_sensor, and tarif) is only assigned inside the 'if self.generate_energy_meters:' block, but the code that uses these variables (the for loop iterating tarif_config.items(), the threshold check, the high_times check, and the tarif select update) was placed outside that block at a lower indentation level.

This means every coordinator update cycle crashes when energy meters are disabled, flooding the logs with errors.

Fix: move all code that depends on variables defined within the 'if self.generate_energy_meters:' block to be inside that block. When the option is disabled, check_tarif() now simply returns without action.

Tested on Home Assistant 2026.8.2 (Green) with both generate_energy_meters enabled and disabled — no errors in either case.

…isabled

When the 'Generate energy meters' option is disabled in the integration
configuration, the check_tarif() method crashes with:

  UnboundLocalError: cannot access local variable 'tarif_config' where
  it is not associated with a value

Root cause: the variable tarif_config (along with energy_used, plan_name,
base_sensor, and tarif) is only assigned inside the
'if self.generate_energy_meters:' block, but the code that uses these
variables (the for loop iterating tarif_config.items(), the threshold
check, the high_times check, and the tarif select update) was placed
outside that block at a lower indentation level.

This means every coordinator update cycle crashes when energy meters are
disabled, flooding the logs with errors.

Fix: move all code that depends on variables defined within the
'if self.generate_energy_meters:' block to be inside that block. When
the option is disabled, check_tarif() now simply returns without action.

Tested on Home Assistant 2026.8.2 (Green) with both generate_energy_meters
enabled and disabled — no errors in either case.
@ic-dev21

Copy link
Copy Markdown
Collaborator

Can you share logging? I cannot reproduce it here on multiple hosts.

@njloof

njloof commented Aug 20, 2026

Copy link
Copy Markdown
Author

Sorry about that -- good callout.

Reproduced on Home Assistant 2026.8.2 (Home Assistant Green, HAOS 18.2)
Hilo integration version: dvd-dev/hilo main branch (commit 40339bc)
Configuration: generate_energy_meters = false, track_unknown_sources = true

The error fires every coordinator update cycle (~10 seconds), flooding the logs:

2026-08-20 00:47:13.907 ERROR (MainThread) [custom_components.hilo] Unexpected error fetching hilo data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 435, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 315, in _async_update_data
    return await self.update_method()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/hilo/__init__.py", line 955, in async_update
    self.check_tarif()
    ~~~~~~~~~~~~~~~~^^
  File "/config/custom_components/hilo/__init__.py", line 1059, in check_tarif
    for tarif_name, rate in tarif_config.items():
                            ^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'tarif_config' where it is not associated with a value

When generate_energy_meters is False but track_unknown_sources is True,
check_tarif() executes, skips the assignment block, then crashes on
the uninitialized variable.

Steps to reproduce:

  1. Install Hilo integration from dvd-dev/hilo (main branch, commit 40339bc)
  2. In integration options, disable "Generate energy meters"
  3. Keep "Track unknown sources" enabled (default: true)
  4. Restart Home Assistant
  5. Observe repeated UnboundLocalError every ~10 seconds in logs

@ic-dev21

ic-dev21 commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

Sorry about that -- good callout.

Reproduced on Home Assistant 2026.8.2 (Home Assistant Green, HAOS 18.2) Hilo integration version: dvd-dev/hilo main branch (commit 40339bc) Configuration: generate_energy_meters = false, track_unknown_sources = true

The error fires every coordinator update cycle (~10 seconds), flooding the logs:

2026-08-20 00:47:13.907 ERROR (MainThread) [custom_components.hilo] Unexpected error fetching hilo data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 435, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 315, in _async_update_data
    return await self.update_method()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/hilo/__init__.py", line 955, in async_update
    self.check_tarif()
    ~~~~~~~~~~~~~~~~^^
  File "/config/custom_components/hilo/__init__.py", line 1059, in check_tarif
    for tarif_name, rate in tarif_config.items():
                            ^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'tarif_config' where it is not associated with a value

When generate_energy_meters is False but track_unknown_sources is True, check_tarif() executes, skips the assignment block, then crashes on the uninitialized variable.

Steps to reproduce:

  1. Install Hilo integration from dvd-dev/hilo (main branch, commit 40339bc)
  2. In integration options, disable "Generate energy meters"
  3. Keep "Track unknown sources" enabled (default: true)
  4. Restart Home Assistant
  5. Observe repeated UnboundLocalError every ~10 seconds in logs

Track unknown source is meant to be used with energy meters and is not defaulted to true, as it generates one as well. I am seriously thinking of removing that checkbox entirely as now HA tracks this natively (and has since.. 2025.9 I think?) unless you specifically need the power meter itself for something.

@njloof

njloof commented Aug 21, 2026

Copy link
Copy Markdown
Author

Nope, not using the unknown source data tracking anyway -- so if the simpler solution is just to kill that option, feel free to decline this patch

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