bramstroker / homeassistant-powercalc

Custom component to calculate estimated power consumption of lights and other appliances
MIT License
968 stars 256 forks source link

RuntimeError with 2024.9.0b1 #2466

Closed Gloomyeye closed 1 month ago

Gloomyeye commented 1 month ago

System Health details

System Information

version core-2024.9.0b1
installation_type Home Assistant OS
dev false
hassio true
docker true
user root
virtualenv false
python_version 3.12.4
os_name Linux
os_version 6.6.46-haos
arch x86_64
timezone Europe/Berlin
config_dir /config
Home Assistant Community Store GitHub API | ok -- | -- GitHub Content | ok GitHub Web | ok HACS Data | ok GitHub API Calls Remaining | 5000 Installed Version | 2.0.0 Stage | running Available Repositories | 1390 Downloaded Repositories | 22
Home Assistant Cloud logged_in | false -- | -- can_reach_cert_server | ok can_reach_cloud_auth | ok can_reach_cloud | ok
Home Assistant Supervisor host_os | Home Assistant OS 13.1 -- | -- update_channel | beta supervisor_version | supervisor-2024.08.0 agent_version | 1.6.0 docker_version | 26.1.4 disk_total | 395.0 GB disk_used | 34.3 GB healthy | true supported | true host_connectivity | true supervisor_connectivity | true ntp_synchronized | true virtualization | kvm board | ova supervisor_api | ok version_api | ok installed_addons | ESPHome (2024.8.1), Samba share (12.3.2), Piper (1.5.2), openWakeWord (1.10.0), Terminal & SSH (9.14.0), Samba Backup (5.2.0), Studio Code Server (5.15.0), vosk (1.6.1), Lovelace Kindle Screensaver (1.0.12)
Dashboards dashboards | 5 -- | -- resources | 18 views | 38 mode | storage
Recorder oldest_recorder_run | 21. August 2024 um 16:11 -- | -- current_recorder_run | 29. August 2024 um 18:25 estimated_db_size | 946.35 MiB database_engine | sqlite database_version | 3.45.3

Checklist

Describe the issue

Hello,

I got the following runtime error during start up of the latest home assistant beta version 2024.9.0b1.

Reproduction steps

  1. Start Home Assistant
  2. Check the logs

Debug logs

Logger: homeassistant.components.sensor
Quelle: helpers/entity_platform.py:595
Integration: Sensor (Dokumentation, Probleme)
Erstmals aufgetreten: 29. August 2024 um 18:25:33 (2 Vorkommnisse)
Zuletzt protokolliert: 29. August 2024 um 18:25:33

Error adding entity sensor.bose_media_soundbar_300_power for domain sensor with platform powercalc
Error adding entity sensor.apple_media_homepod_edith_power for domain sensor with platform powercalc
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 595, in _async_add_entities
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 909, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1365, in add_to_platform_finish
    await self.async_added_to_hass()
  File "/config/custom_components/powercalc/sensors/power.py", line 439, in async_added_to_hass
    async_track_template_result(
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1361, in async_track_template_result
    tracker.async_setup(strict=strict, log_fn=log_fn)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1027, in async_setup
    self._info[template] = info = template.async_render_to_info(
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 699, in async_render_to_info
    raise RuntimeError(f"hass not set while rendering {self}")
RuntimeError: hass not set while rendering Template<template=({{ is_state('media_player.bose_media_soundbar_300', 'playing') }}) renders=1>

Diagnostics dump or YAML config

config_entry-powercalc-01J2Q1XKK4JE638QHXW6SA8QMS.json

bramstroker commented 1 month ago

Where did you define the template {{ is_state('media_player.bose_media_soundbar_300', 'playing') }}? I don't see that in your diagnostics. Could you share diagnostics for the entity causing this issue?

Gloomyeye commented 1 month ago

I don't define any template with this media player in my system. I only configured powercalc for this media player to have power and energy sensors. I see that the power sensor is with the current beta unavailable. The same for my homepod edith. The bose soundbar is currently off (without any power), but the homepod is in idle mode.

Here are the diagnostics: config_entry-powercalc-5221ad35c11137304805ddfdf0305969.json config_entry-powercalc-01J2Q1XKK4JE638QHXW6SA8QMS.json

danieldotnl commented 1 month ago

I see the same error in my powercalc. And I also saw this error for my own custom integration 'MeasureIt'. I know the core team is trying to optimize things of which this is probably the result, as last month there was a similar change that was reverted: https://github.com/home-assistant/core/issues/123251

Not sure if something will be reverted again, but for MeasureIt I just made a quick and dirty fix (setting the hass variable again) (https://github.com/danieldotnl/ha-measureit/commit/0044eb1c59d2ff383d7d14ccc769c37d30d8ba89)

bramstroker commented 1 month ago

@Gloomyeye Ah I see, it's a built in powercalc profile which also uses a template for calculation_enabled_condition. https://github.com/bramstroker/homeassistant-powercalc/blob/master/profile_library/bose/767520-2100/model.json#L2

I have made some possible fix already this morning, similar to the fix @danieldotnl has done in his integration. To make sure hass variable is set when instanciating the template.

I will release new version with the fix shortly. Please check if this resolves the issue for you.

danieldotnl commented 1 month ago

I can confirm your fix works for me. Thanks!

Gloomyeye commented 1 month ago

Yes, I can also confirm your quick fix. Thank you very much!