HA 2023.4.0 no longer shows my template sensor attribute list

I have been using the following template in HA for a while in order to track and show entities that have gone unavailable to HA due to lost connection, powered off, etc. It has proven a very useful sensor for letting me know of wider issues on my home network - like when one of my APs was randomly rebooting multiple times a day and taking half of my devices offline with it or when an Integration duplicates all of its entities (which happens way more often than it should).

However, since upgrading to 2023.4.0 I’ve noticed that, while the sensor still reports the number of entities offline, I can no longer see the Attributes list portion that shows exactly which sensors have gone offline. The portion I’m referring to is when you click the item and it shows the histograph. There used to be a section directly underneath that showed the sensor attributes (as you’ll see defined in my template below).

Any thoughts on what might have happened to this and how to get it back?

Here’s my template; note the {{ entities|map(attribute='entity_id')|list }} which should enumerate the items into a list that can be viewed. This is the part that doesn’t appear to be working anymore.

template:
  - sensor:
      - name: "Unavailable Entities"
        unique_id: unavailable_entities
        icon: "{{ iif(states(this.entity_id)|int(0) > 0,'mdi:alert-circle','mdi:check-circle') }}"
        unit_of_measurement: entities
        state: >
          {% set entities = state_attr(this.entity_id,'entity_id') %}
          {% if entities != none %} {{ entities|count }} {% endif %}
        attributes:
          entity_id: >
            {% set ignore_seconds = 3 %}
            {% set ignored = state_attr('group.ignored_unavailable_entities','entity_id') %}
            {% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}
            {% set entities = states|rejectattr('domain','eq','group')
                |rejectattr('last_changed','ge',ignore_ts)
                |rejectattr('entity_id','search','(octoprint_|_debug|scene.|device_tracker|person.|_channel|openweathermap_|var.|_active_app|emby_|camera.|agent_alarm|_led|_consumption|unavailable_entities|media_player|sensor.hacs|hall_uv|speedtest|ender3_|10_10_0_21)')
                |selectattr('state','in',['unavailable','unknown']) %}
            {% if ignored != none %}
              {% set entities =  entities|rejectattr('entity_id','in',ignored) %}
            {% endif %}
            {{ entities|map(attribute='entity_id')|list }}

Screenshot from 2023-04-06 14-25-41

1 Like

same here, but it still shows in the developer tools. that’s weird

Change entity_id to something other, e.g. id.

1 Like

The reference to entity_id in the line a little above this will also need to be changed

eg. - {% set entities = state_attr(this.entity_id,‘id’) %}

2 Likes

Fair point (overlooked this because I don’t use a reference in favor of faster state change). It’s also recommended to check your automation if you are using one.

1 Like

I’ll post what I think happened here so someone who knows the actual answer will come along and correct me.

I believe that not recording entity_id attributes to the database may have been part of the improvements to the database with this update. The entity_id attribute is not displaying for group entities in the UI either now, they are actually shown as actual entities in the more-info card. I had a quick look through the change log but haven’t come across where this change was made yet. It probably should have been listed as a breaking change if my hunch is correct?

To keep everything standardized, the group entity is what I patterned this template (and a butt load of others in my config) after, where entity_id is always a list of the related entity id’s for the entity.

So if I’m right about why this changed, it is more of a feature than a bug and renaming the entity_id attribute as @pedolsky has suggested will circumvent that improvement. Obviously it will get you the attribute back into the more-info dialogue if that’s actually what you need.

Personally, I don’t use the more-info dialogue to view my unavailable entities, I have a fold-entity-row in an entities card (see example below) in the UI for that so I’ll take the database improvement as a win.

I haven’t decided if I’ll update the attribute name in the package yet, I’m going to wait for that 'right" answer to show up first.

Unavailable Entities - Fold Entity Row

Fold Row Collapsed

image

Fold Row Expanded

    type: custom:auto-entities
    show_empty: true
    unique: true
    filter:
      template: "{{ state_attr('sensor.unavailable_entities','entity_id') }}"
    sort:
      method: state
    card:
      type: custom:fold-entity-row
      padding: 0
      head:
        entity: sensor.unavailable_entities
        style: |
          :host {
            --paper-item-icon-color:
              {% if states('sensor.unavailable_entities')|int(-1) > 0 %} var(--entity-warning-color)
              {% elif states('sensor.unavailable_entities')|int(-1) == 0 %} var(--state-icon-color)
              {% else %} var(--entity-disabled-color)
              {% endif %}
            ;
          }
1 Like

Thanks, that is a much neater solution

1 Like

Sorry for the delay. Turns out I never turned on notifications and only just now noticed folks replied to me. haha

It was hard to choose a “solution” here, but I opted for not reinventing my existing wheel by using @pedolsky 's suggestion. I did have to alter a total of 3 lines, but this was my final result that works as desired:

template:
  - sensor:
      - name: "Unavailable Entities"
        unique_id: unavailable_entities
        icon: "{{ iif(states(this.device_id)|int(0) > 0,'mdi:alert-circle','mdi:check-circle') }}"
# changed this line ^
        unit_of_measurement: entities
        state: >
          {% set entities = state_attr(this.entity_id,'device_id') %}
# changed this line ^
          {% if entities != none %} {{ entities|count }} {% endif %}
        attributes:
          device_id: >
# changed this line ^
            {% set ignore_seconds = 3 %}
            {% set ignored = state_attr('group.ignored_unavailable_entities','entity_id') %}
            {% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}
            {% set entities = states|rejectattr('domain','eq','group')
                |rejectattr('last_changed','ge',ignore_ts)
                |rejectattr('entity_id','search','(octoprint_|_debug|scene.|device_tracker|person.|_channel|openweathermap_|var.|_active_app|emby_|camera.|agent_alarm|_led|_consumption|unavailable_entities|media_player|sensor.hacs|hall_uv|speedtest|ender3_|10_10_0_21)')
                |selectattr('state','in',['unavailable','unknown']) %}
            {% if ignored != none %}
              {% set entities =  entities|rejectattr('entity_id','in',ignored) %}
            {% endif %}
            {{ entities|map(attribute='entity_id')|list }}

Many thanks to @pedolsky, @uzi2git2, and @jazzyisj for the insight and solutions! I may eventually switch to @jazzyisj’s method, since it is a bit nicer looking, but this will get me by. I don’t normally have devices go offline unless there is a problem with the network. So, I rarely need to check this under normal circumstances.

Ugh…it was working after my last post as indicated…but now is broken again in 2023.5.2. I haven’t changed anything with the sensor. Just shows Unknown now.

Relevant error messages:

Logger: homeassistant.helpers.template
Source: helpers/template.py:544
First occurred: 1:22:16 AM (1 occurrences)
Last logged: 1:22:16 AM

Template variable error: 'homeassistant.helpers.template_entity.DummyState object' has no attribute 'device_id' when rendering '{{ iif(states(this.device_id)|int(0) > 0,'mdi:alert-circle','mdi:check-circle') }}'
Logger: homeassistant.helpers.event
Source: helpers/template.py:546
First occurred: 1:22:28 AM (1 occurrences)
Last logged: 1:22:28 AM

Error while processing template: Template<template=({{ iif(states(this.device_id)|int(0) > 0,'mdi:alert-circle','mdi:check-circle') }}) renders=3>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 544, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2164, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.10/site-packages/jinja2/sandbox.py", line 393, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 835, in __call__
    state = _get_state(self._hass, entity_id)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1093, in _get_state
    return _get_template_state_from_state(hass, entity_id, hass.states.get(entity_id))
  File "/usr/src/homeassistant/homeassistant/core.py", line 1445, in get
    return self._states.get(entity_id.lower())
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2190, in _fail_with_undefined_error
    raise ex
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2181, in _fail_with_undefined_error
    return super()._fail_with_undefined_error(*args, **kwargs)
jinja2.exceptions.UndefinedError: 'homeassistant.helpers.template.TemplateStateFromEntityId object' has no attribute 'device_id'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 665, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 546, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'homeassistant.helpers.template.TemplateStateFromEntityId object' has no attribute 'device_id'
Logger: homeassistant.helpers.template_entity
Source: helpers/template_entity.py:379
First occurred: 1:22:28 AM (1 occurrences)
Last logged: 1:22:28 AM

TemplateError('UndefinedError: 'homeassistant.helpers.template.TemplateStateFromEntityId object' has no attribute 'device_id'') while processing template 'Template<template=({{ iif(states(this.device_id)|int(0) > 0,'mdi:alert-circle','mdi:check-circle') }}) renders=5>' for attribute '_attr_icon' in entity 'sensor.unavailable_entities'
Logger: homeassistant
Source: components/sensor/__init__.py:583
First occurred: 1:22:28 AM (1 occurrences)
Last logged: 1:22:28 AM

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 579, in state
    numerical_value = int(value)
ValueError: invalid literal for int() with base 10: ''

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template_entity.py", line 379, in _async_template_startup
    result_info.async_refresh()
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 953, in async_refresh
    self._refresh(None)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1133, in _refresh
    self.hass.async_run_hass_job(self._job, event, updates)
  File "/usr/src/homeassistant/homeassistant/core.py", line 607, in async_run_hass_job
    hassjob.target(*args)
  File "/usr/src/homeassistant/homeassistant/helpers/template_entity.py", line 348, in _handle_results
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 585, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 649, in _async_write_ha_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 591, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 583, in state
    raise ValueError(
ValueError: Sensor sensor.unavailable_entities has device class None, state class None unit entities and suggested precision None thus indicating it has a numeric value; however, it has the non-numeric value:  (<class 'str'>)

My Template as it has been working for weeks:

template:
  - sensor:
      - name: "Unavailable Entities"
        unique_id: unavailable_entities
        icon: "{{ iif(states(this.device_id)|int(0) > 0,'mdi:alert-circle','mdi:check-circle') }}"
        unit_of_measurement: entities
        state: >
          {% set entities = state_attr(this.entity_id,'device_id') %}
          {% if entities != none %} {{ entities|count }} {% endif %}
        attributes:
          device_id: >
            {% set ignore_seconds = 3 %}
            {% set ignored = state_attr('group.ignored_unavailable_entities','entity_id') %}
            {% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}
            {% set entities = states|rejectattr('domain','eq','group')
                |rejectattr('last_changed','ge',ignore_ts)
                |rejectattr('entity_id','search','(octoprint_|_debug|scene.|device_tracker|person.|_channel|openweathermap_|var.|_active_app|emby_|camera.|agent_alarm|_led|_consumption|unavailable_entities|media_player|sensor.hacs|hall_uv|speedtest|ender3_|10_10_0_21)')
                |selectattr('state','in',['unavailable','unknown']) %}
            {% if ignored != none %}
              {% set entities =  entities|rejectattr('entity_id','in',ignored) %}
            {% endif %}
            {{ entities|map(attribute='entity_id')|list }}

So, what’s wrong now? :man_facepalming:t2:

I have the same issue. Just noticed it this morning…Misery loves company!

remove line:
unit_of_measurement: entities

2 Likes

Thank you!

Fore me it was I put the following in the Zigbee2Mqqt configuration file to get the “attribute” list in the sensor cards…

homeassistant:
legacy_entity_attributes: true
legacy_triggers: true
ETC …

Hope this helps others…