core: "hass variable not set on template" in 2024.8.b04

The problem

In 2024.8.0b4 I receive the following error message on all rest integrations calls.

2024-08-06 15:16:19.314 ERROR (MainThread) [homeassistant.components.sensor] Error adding entity sensor.group_therapy_episode_2 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

I have a packages folder set up as follows in configuration.yaml

homeassistant:
  customize: !include customize.yaml
  packages: !include_dir_named packages

I also have rest configured as follows with some other data being pulled, if this is relevant.

rest: !include rest.yaml

Bear in mind this works just fine in 2024.7.4.

In my packages folder, I have a file called podcasts.yaml which defines a load of entries to retrieve podcasts and data from youtube and also defines some templates. I can’t post a copy, it’s 1000+ lines but here’s an example:

It starts like this:

rest:
  - resource: "https://www.youtube.com/feeds/videos.xml?playlist_id=PLhHqebDQf-abSrU9LHEsaLU4P2R6W_RiK"
    scan_interval: 3600
    sensor:
      - name: "lane_8_episode_0"
        unique_id: lane_8_episode_0
        value_template: "{{ value_json.feed.entry[0].title }}"
        picture: "{{ value_json.feed.entry[0]['media:group']['media:thumbnail']['@url'] }}"
        json_attributes_path: "$.feed.entry[0]"
        json_attributes:
          - link
          - published
      - name: "lane_8_episode_1"
        unique_id: lane_8_episode_1
        value_template: "{{ value_json.feed.entry[1].title }}"
        picture: "{{ value_json.feed.entry[1]['media:group']['media:thumbnail']['@url'] }}"
        json_attributes_path: "$.feed.entry[1]"
        json_attributes:
          - link
          - published
      - name: "lane_8_episode_2"
        unique_id: lane_8_episode_2
        value_template: "{{ value_json.feed.entry[2].title }}"
        picture: "{{ value_json.feed.entry[2]['media:group']['media:thumbnail']['@url'] }}"
        json_attributes_path: "$.feed.entry[2]"
        json_attributes:
          - link
          - published
      - name: "lane_8_episode_3"
        unique_id: lane_8_episode_3
        value_template: "{{ value_json.feed.entry[3].title }}"
        picture: "{{ value_json.feed.entry[3]['media:group']['media:thumbnail']['@url'] }}"
        json_attributes_path: "$.feed.entry[3]"
        json_attributes:
          - link
          - published
      - name: "lane_8_episode_4"
        unique_id: lane_8_episode_4
        value_template: "{{ value_json.feed.entry[4].title }}"
        picture: "{{ value_json.feed.entry[4]['media:group']['media:thumbnail']['@url'] }}"
        json_attributes_path: "$.feed.entry[4]"
        json_attributes:
          - link
          - published

Using a template, defined within the same podcasts.yaml file, I use a number helper (which is the thing that changes) to pull data from the rest sensors defined above and populate the template sensor.

template:
  - sensor:
      - name: Lane 8
        unique_id: lane_8
        icon: mdi:podcast
        state: "{{ states('sensor.lane_8_episode_'~(states('input_number.podcast_episode') | int(0))) }}"
        attributes:
          link: "{{ state_attr('sensor.lane_8_episode_'~(states('input_number.podcast_episode') | int(0)), 'link')['@href'] }}"
          image: "{{ state_attr('sensor.lane_8_episode_'~(states('input_number.podcast_episode') | int(0)), 'entity_picture') }}"
          published: |
            {% set date = state_attr('sensor.lane_8_episode_'~(states('input_number.podcast_episode') | int(0)), 'published') %}
            {% from 'date_ordinals.jinja' import date_ordinal %}
            {{date_ordinal(as_datetime(date, now()).isoformat(), false, false, true)}}

I noticed initially that the image attribute was null in the template sensor but on investigation, it was because the picture attribute wasn’t on the episode entry anymore. Again, this works just fine in 2024.7.4

My logs have the above error code for every podcast episode entry in the podcasts.yaml file but I don’t get this in 2024.7.4 or any previous version.

The rest integration entries are created but they do not have the picture attribute (entity_picture) for some reason. 2024.8.b04 image

2024.7.4 image

What version of Home Assistant Core has the issue?

core-2024.8.0b4

What was the last working version of Home Assistant Core?

core-2024.7.4

What type of installation are you running?

Home Assistant OS

Integration causing the issue

REST

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 37 (15 by maintainers)

Most upvoted comments

I tracked it down to the template() method in config_validation.py. It does a call to async_get_hass_or_none() in core.py, which now returns None instead of a hass instance. In 2024.7 it DID return a hass instance.

That’s on 2024.8.b05 too btw.

Same issue here after upgrading to 2024.8.0. Here is my config that is impacted:

- resource_template: >-
    {% if is_state('switch.desktop_adama','on') %}
      http://10.144.1.10:8085/data.json
    {% else %}
      http://10.144.1.153:8000/unavailable.html
    {% endif %}

  sensor:
    - name: "Desktop CPU Load"
      unique_id: rest_desktop_cpu_load
      availability: '{{ is_state("switch.desktop_adama","on") }}'
      value_template: "{{ value_json.Children[0].Children[1].Children[0].Children[0].Value | replace(',', '.') | replace(' %', '') | float(0) }}"
      unit_of_measurement: "%"
      state_class: measurement
    - name: "Desktop CPU Temperature"
      unique_id: rest_desktop_cpu_temperature
      availability: '{{ is_state("switch.desktop_adama","on") }}'
      value_template: "{{ value_json.Children[0].Children[0].Children[0].Children[1].Children[0].Value | replace(',', '.') | replace(' °C', '') | float(0) }}"
      unit_of_measurement: "°C"
      device_class: temperature
      state_class: measurement
    - name: "Desktop Memory Usage"
      unique_id: rest_desktop_memory_usage
      availability: '{{ is_state("switch.desktop_adama","on") }}'
      value_template: "{{ value_json.Children[0].Children[2].Children[0].Children[0].Value | replace(',', '.') | replace(' %', '') | float(0) }}"
      unit_of_measurement: "%"
      state_class: measurement
    - name: "Desktop GPU Temperature"
      unique_id: rest_desktop_gpu_temperature
      availability: '{{ is_state("switch.desktop_adama","on") }}'
      value_template: "{{ value_json.Children[0].Children[3].Children[1].Children[0].Value | replace(',', '.') | replace(' °C', '') | float(0) }}"
      unit_of_measurement: "°C"
      device_class: temperature
      state_class: measurement
    - name: "Desktop GPU Memory Usage"
      unique_id: rest_desktop_gpu_memory_usage
      availability: '{{ is_state("switch.desktop_adama","on") }}'
      value_template: "{{ value_json.Children[0].Children[3].Children[2].Children[4].Value | replace(',', '.') | replace(' %', '') | float(0) }}"
      unit_of_measurement: "%"
      state_class: measurement
    - name: "Desktop GPU Load"
      unique_id: rest_desktop_gpu_load
      availability: '{{ is_state("switch.desktop_adama","on") }}'
      value_template: "{{ value_json.Children[0].Children[3].Children[2].Children[0].Value | replace(',', '.') | replace(' %', '') | float(0) }}"
      unit_of_measurement: "%"
      state_class: measurement
    - name: "Desktop GPU Fan"
      unique_id: rest_desktop_gpu_fan
      availability: '{{ is_state("switch.desktop_adama","on") }}'
      value_template: "{{ value_json.Children[0].Children[3].Children[4].Children[0].Value | replace(',', '.') | replace(' %', '') | float(0) }}"
      unit_of_measurement: "%"
      state_class: measurement
    - name: "Desktop GPU Power"
      unique_id: rest_desktop_gpu_power
      availability: '{{ is_state("switch.desktop_adama","on") }}'
      value_template: "{{ value_json.Children[0].Children[3].Children[5].Children[0].Value | replace(',', '.') | replace(' W', '') | float(0) }}"
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
    - name: "Desktop C used space"
      unique_id: rest_desktop_c_drive_used_space
      availability: '{{ is_state("switch.desktop_adama","on") }}'
      value_template: "{{ value_json.Children[0].Children[7].Children[0].Children[0].Value | replace(',', '.') | replace(' %', '') | float(0) }}"
      unit_of_measurement: "%"
      state_class: measurement
    - name: "Desktop D used space"
      unique_id: rest_desktop_d_drive_used_space
      availability: '{{ is_state("switch.desktop_adama","on") }}'
      value_template: "{{ value_json.Children[0].Children[6].Children[1].Children[0].Value | replace(',', '.') | replace(' %', '') | float(0) }}"
      unit_of_measurement: "%"
      state_class: measurement

Oh that sounds like a nice scope now. I will probably take a look sometimes later this week

Oh whoops, looked over it