Template to count entities that contain certain characters?

I am trying to get a count (number) of all my entities that contain the word tmpl in the entity_id

I suffix all my template sensors with tmpl to distinguish them from the normal sensors.

I found this code and modified a bit. It returns all the entities as a list.

{%-for state in states 
    if (
          (state.entity_id.endswith("tmpl"))
        )  -%}        
        {%- if loop.first -%}{% elif loop.last -%} {% else -%}  {% endif -%} 
           {{state.name}} {% if state.name.endswith('tmpl')%}  {%else%}  {%endif%}
{% endfor%}

Now my question is; how do I count them?

Try this ({{ loop.index }} is the number you are looking for

{% for state in states
    if (
          (state.entity_id.endswith("tmpl"))
        )  -%}  
        {{state.name}}
         {% if loop.last -%}
           {{ loop.index }}
         {%- endif %}
   
{%- endfor %}

Tried it now, gives me the exact same result as the code above :confused:

Not exatly at the end of the list you should have a number equal to the number of entities found matching the condition you wrote…