Cant select/map attributes 'title' in template

using

{% set noti = states.persistent_notification
   |selectattr('state','eq','notifying')|map(attribute='entity_id')|list %}
{{noti}} 

works fine and returns a list of ‘on’ notifications with the entity_id. However, I want the show a list of titles, using

{% set noti = states.persistent_notification
   |selectattr('state','eq','notifying')|map(attribute='title')|list %}
{{noti}} 

which does nothing (shows ‘Undefined’ in the list for the number of ‘on’ notifications)

Ive checked a current persistent notification itself,

{{states.persistent_notification.rss_feed_home_assistant_0_107.attributes.title}}

which works fine:

this is the full attributes list of the notification:

had a long day of puzzling, so must be overlooking the obvious… please have a look? thanks!

1 Like
{% set noti = states.persistent_notification |selectattr('state','eq','notifying')|map(attribute='attributes.title')|list %} {{noti}}
1 Like

one more Petro:

why do I have to use this:

            {% set noti = states.persistent_notification
            |selectattr('state','eq','notifying')  %}
            {% if noti|list|length == 0 %} No persistent notifications
            {% else %}
            {{states.persistent_notification
            |selectattr('state','eq','notifying')|map(attribute='attributes.title')|list|join(', \n')}}
 {% endif %}

and cant I use:

          notifications: >
            {% set noti = states.persistent_notification
            |selectattr('state','eq','notifying')  %}
            {% if noti|list|length == 0 %} No persistent notifications
            {% else %}
            {{noti|map(attribute='attributes.title')|list|join(', \n')}}
            {% endif %}

I cant seem to use the {{noti}} in both if and else, only in either of the if or else…

1 Like

because noti is a generator. Once it’s used once, you can’t use it again. Convert it to a list and you’ll be able to use it multiple times

{% set noti = states.persistent_notification
            |selectattr('state','eq','notifying') | list %}
{% if noti|length == 0 %} No persistent notifications
{% else %}
{{noti|map(attribute='attributes.title')|list|join(', \n')}}
{% endif %}

I see, thanks!

now I can play with the various options again, nice:

{% set noti = states.persistent_notification
              |selectattr('state','eq','notifying')
              |map(attribute='attributes.title')
              |list %}
{% if noti|length == 0 %} No persistent notifications
{% else %}
{{noti|join(', \n')}}
{% endif %}

to use it in my template sensor:

      persistent_notifications:
        value_template: >
          {% set noti = states.persistent_notification
                        |selectattr('state','eq','notifying')
                        |map(attribute='entity_id')
                        |list %}
          {{noti|count}}
        attribute_templates:
          notifications: >
            {% set noti = states.persistent_notification
                          |selectattr('state','eq','notifying')
                          |map(attribute='attributes.title')
                          |list %}
            {% if noti|length == 0 %} No persistent notifications
            {% else %}
            {{noti|join(', \n')}}
            {% endif %}

though the value_template could probably just be

        {{states.persistent_notification|list|length}}

have a good weekend!