home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.31k stars 30.62k forks source link

REST sensor stopped to evaluate templates for entity icons after update to 2024.8 #123520

Closed mirekmal closed 2 months ago

mirekmal commented 2 months ago

The problem

After update to 2024.8 some REST sensor stopped to be created and throws errors into log file. Sensors that are affected contains templated icon attribute. Sensor is visible in Developer Tools->Satets with old, not update value, but with no icon attribute. The same sensor with removed code for icon works fine. Icon template moved to state value (value_template) works fine, so it is not template error (anyhow was working properly in previous versions of core)

What version of Home Assistant Core has the issue?

2024.8

What was the last working version of Home Assistant Core?

2024.7 (I think)

What type of installation are you running?

Home Assistant OS

Integration causing the issue

RESTful sensor

Link to integration documentation on our website

https://www.home-assistant.io/integrations/sensor.rest/

Diagnostics information

No response

Example YAML snippet

rest:
- resource: http://192.168.52.53:8123/local/weatherbit_forecast.json
  sensor:

      # sample sensor that works fine
    - name: weather_code_current
      value_template: '{{ value_json.data[0].weather.code }}'

      # Sample sensor that throws errors. If icon section removed works fine
    - name: condition_0
      value_template: "{{ value_json.data[0].weather.description }}"  
      icon: >
        {% set condition = value_json.data[0].weather.code %}
        {% if condition == 200 %} mdi:weather-partly-lightning
        {% elif condition == 201 %} mdi:weather-partly-lightning
        {% elif condition == 202 %} mdi:weather-partly-lightning
        {% elif condition == 203 %} mdi:weather-partly-lightning
        {% elif condition == 230 %} mdi:weather-lightning
        {% elif condition == 231 %} mdi:weather-lightning-rainy
        {% elif condition == 232 %} mdi:weather-lightning-rainy
        {% elif condition == 233 %} mdi:weather-lightning-rainy
        {% elif condition == 300 %} mdi:weather-rainy
        {% elif condition == 301 %} mdi:weather-rainy
        {% elif condition == 302 %} mdi:weather-rainy
        {% elif condition == 500 %} mdi:weather-pouring
        {% elif condition == 501 %} mdi:weather-pouring
        {% elif condition == 502 %} mdi:weather-pouring
        {% elif condition == 511 %} mdi:weather-pouring
        {% elif condition == 520 %} mdi:weather-pouring
        {% elif condition == 521 %} mdi:weather-partly-rainy
        {% elif condition == 522 %} mdi:weather-pouring
        {% elif condition == 600 %} mdi:weather-partly-snowy
        {% elif condition == 601 %} mdi:weather-snowy
        {% elif condition == 602 %} mdi:weather-snowy-heavy
        {% elif condition == 610 %} mdi:weather-partly-snowy-rainy
        {% elif condition == 611 %} mdi:weather-snowy-rainy
        {% elif condition == 612 %} mdi:weather-snowy-rainy
        {% elif condition == 621 %} mdi:weather-partly-snowy
        {% elif condition == 622 %} mdi:weather-snowy-heavy
        {% elif condition == 623 %} mdi:weather-snowy
        {% elif condition == 700 %} mdi:weather-hazy
        {% elif condition == 711 %} mdi:weather-hazy
        {% elif condition == 721 %} mdi:weather-hazy
        {% elif condition == 731 %} mdi:weather-dust
        {% elif condition == 741 %} mdi:weather-fog
        {% elif condition == 751 %} mdi:weather-hazy
        {% elif condition == 800 %} mdi:weather-sunny
        {% elif condition == 801 %} mdi:weather-partly-cloudy
        {% elif condition == 802 %} mdi:weather-partly-cloudy
        {% elif condition == 803 %} mdi:weather-partly-cloudy
        {% elif condition == 804 %} mdi:weather-cloudy
        {% else %} mdi:cloud-question-outline
        {% endif %}

Anything in the logs that might be useful for us?

2024-08-10 08:56:56.324 ERROR (MainThread) [homeassistant.components.sensor] Error adding entity sensor.condition_0 for domain sensor with platform rest
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 598, in _async_add_entities
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 912, 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 "/usr/src/homeassistant/homeassistant/helpers/trigger_template_entity.py", line 162, in async_added_to_hass
    await super().async_added_to_hass()
  File "/usr/src/homeassistant/homeassistant/components/rest/entity.py", line 43, in async_added_to_hass
    self._update_from_rest_data()
  File "/usr/src/homeassistant/homeassistant/components/rest/sensor.py", line 187, in _update_from_rest_data
    self._process_manual_data(raw_value)
  File "/usr/src/homeassistant/homeassistant/helpers/trigger_template_entity.py", line 250, in _process_manual_data
    self._render_templates(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/trigger_template_entity.py", line 190, in _render_templates
    rendered[key] = self._config[key].async_render(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 597, in async_render
    compiled = self._compiled or self._ensure_compiled(limited, strict, log_fn)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 798, in _ensure_compiled
    assert self.hass is not None, "hass variable not set on template"
           ^^^^^^^^^^^^^^^^^^^^^
AssertionError: hass variable not set on template

Additional information

No response

home-assistant[bot] commented 2 months ago

rest documentation rest source

mib1185 commented 2 months ago

will be fixed in 2024.8.1 (scheduled for today) by #123377

mirekmal commented 2 months ago

Great! Thank you!