I need some help with templating, please

Hello all,

Trying to create a template sensor for updates, but I could’t figure out how to add the title/friendly name at the end instead of the entity_id together with the available version number. This is what I have now:

{% set update_available = namespace(entities=[]) %}
{% set entities = ['update.deconz_update', 'update.home_assistant_core_update']
%}
{% for item in entities %}
  {% if states[item].state == 'on' %}
    {% set update_available.entities = update_available.entities + [item] %}
  {% endif %}
{% endfor %}
{{ update_available.entities }}

result:

[‘update.deconz_update’, ‘update.home_assistant_core_update’]

I also tried this:

{{ states.update|selectattr(‘state’,‘eq’,‘on’)|map(attribute=‘attributes.title’)|join(‘,\n’) }}

result:

deCONZ,
Home Assistant Core

target would be:

deCONZ - 6.15.0
Home Assistant Core - 2022.7.1

Attribute latest_version needs to be added, but also interested what to be done with [item] in the first example. Thanks for the suggestions in advance.

{% set update_available = namespace(entities=[]) %}
{% set entities = ['update.deconz_update', 'update.home_assistant_core_update']
%}
{% for item in entities if states(item) == 'on' %}
    {% set update_available.entities = 
    update_available.entities +
    [ state_attr(item, 'title') ~' - '~ state_attr(item, 'latest_version') ] %}
{% endfor %}
{{ update_available.entities | join(' \n') }}

Thank you Didgeridrew, works like a charm. One last question, because I used 2 entities for the testing, how can I add the whole update domain to the entities without listing them one by one?

UPDATE: I was able to figure out the missing pieces based on yours:

{% set entities = states.update| list %}
{% set update_available = namespace(entities=[]) %}
{% for item in entities %}
  {% if item.state == 'on' %}
    {% set update_available.entities = update_available.entities + [item.attributes.friendly_name ~' - '~ item.attributes.latest_version] %}
  {% endif %}
{% endfor %}
{{ update_available.entities | join(',\n') if update_available.entities | length > 0 else 'none' }}

Thanks again for your help.

@Didgeridrew: the formatting of the list is discarded on the property screen of the entity, so I would like to replace the comma and line break separated list with an enumetation. What should be used instrad of (’, \n’) in this case? Thanks in advance.

I don’t know if I understand exactly what you want, but maybe something like the following would work…?

template:
  - sensor:
      - name: "Updates Available"
        state: >
          {{ (this.attributes.integrations if this is defined) | default('') | count }}
        attributes:
          integrations: >
            {%- set update_available = namespace(entities=[]) %}
            {% for item in state_attr('sensor.hacs', 'repositories')%}
            {%- set update_available.entities = update_available.entities 
              + ['HACS - '~item.display_name ~' - '~ item.available_version] %}
            {% endfor %}
            {%- set entities = states.update| list %}
            {%- for item in entities if item.state == 'on' %}
              {%- set update_available.entities = update_available.entities 
              + [item.attributes.friendly_name 
              ~' - '~ item.attributes.latest_version] %}
            {%- endfor %}
            {{ update_available.entities }}

Sorry, my explanation was not clear. I would like to have something similar to the sensor.hacs, but with less entries (diplay name and available version) and beside HACS, the HA/add-on updates too:

Basically, I want to increase the readability, as this part of HA discards ‘\n’, and sometimes it is hard to read what needs to be updated. Thank you.

Just to make sure I understand…

sensor.hacs outputs a count for the state and has an attribute that returns a dictionary for each updatable entity.

The only dictionary keys you want for each updatable entities are “name” and “version”

You want to include all updatable entities from HACS as well as those from HA and Add-Ons…

I think the following meets those criteria:

      - name: "Updates Available"
        unique_id: 'sensor_updates_available'
        state: >
          {{ (this.attributes.entities if this is defined) | default('') | count }}
        attributes:
          entities: >
            {%- set update_available = namespace(entities=[]) %}
            {% for item in state_attr('sensor.hacs', 'repositories')%}
            {%- set update_available.entities = update_available.entities 
              + [{'name':'HACS - '~item.display_name, 'version':item.available_version}] %}
            {% endfor %}
            {%- set entities = states.update| list %}
            {%- for item in entities if item.state == 'on' %}
              {%- set update_available.entities = update_available.entities 
              + [{'name': item.attributes.friendly_name, 'version': item.attributes.latest_version}] %}
            {%- endfor %}
            {{ update_available.entities }}

Your understanding was perfect, this is exactly the solution I was looking for (see entities vs. entities2):

image

Thank you, I really appreciate your help.

1 Like