Counting issue in template with for loop

trying to count the number of persistent notifications with the string ‘rss_feeds’ I had this for loop, and I do think it worked correctly before:

          {% for state in states.persistent_notification  %}
            {% if 'rss_feed' in state.entity_id %}
              {% if loop.first %}{{loop.length}}
              {% endif %}
            {% endif %}
          {% endfor %}

I had an issue in case of 0, since this template then shows nothing… but, and this is what’s more surprising now, it doesnt even count at all anymore…

check this:

showing correctly there are 3 pers notifications, and 1 of them has the string ’ rss_feed’ in its entity_id:

yet the template shows nothing:

what am I doing wrong here, to not have it show 1, and, how can I show 0 if there is no notification with the string in the entity_id?

a binary_sensor test works fine too:

    {% set feed = states.persistent_notification
                |map(attribute='object_id')
               |join %}
    {{'rss_feed' in feed }}


thanks for having a look

I know it’s not the most elegant but if you try a full loop and use namespace do you you get something that would give you a clue ?

I’m flabbergasted because a mere restart made it work again:

Really don’t grasp why/how that can be.

I agree, that’s weird. :thinking: :man_shrugging:

I tried to replicate the problem but the template worked for me on the first try.

{% for state in states if 'front' in state.object_id %}
 {% if loop.first %}{{ loop.length}}{% endif %}
{% endfor %}

2 Likes

like the way you cut another 2 lines :wink: and the test in the object_id. thanks for the tip.