Rewrite template from a for loop to regular list

having an issue in my auto-entities config I realized it might be the output of the template causing the trouble, and ask for your help to rewrite it in another format. Hence this separate thread.

Given the examples on template filter by Thomas (scroll all the way done there) I originally made:

          {% set km = states('input_number.quakes_near')|float %}
          {% set location = states('input_select.quakes_near') %}
          {% for s in states.geo_location|selectattr('attributes.source','eq','usgs_earthquakes_feed')
             if distance(s.entity_id,location) < km %}
          {{- s.entity_id -}}{{',' if not loop.last else ''}}
          {% endfor %}

this complains about the card needed either entities (which I believe the template above provides) or geo_location sources (which of course these are too…)

still, I’d like to give it a try and rewrite the above to this format

template: "{{states.light | selectattr('state', '==', 'on') | list}}"

so started with

{{states.geo_location|selectattr('attributes.source','eq','usgs_earthquakes_feed') | list}}

but struggle getting the if distance(s.entity_id,location) < km into this format.

Please have a look, thanks!

not possible.

figured as much… thanks for confirming.

might I ask you to have a look at the issue this was an effort for to solve? The auto-entities with the map, using this template as a filter.

I don’t know what you’re asking?

sorry, didnt want to advocate my own post too much.

the issue in the opening post above tells about my issue with this template in an auto-entities card using type: map

Hoped you could spot why this doesn’t work.

You have to convert it to using namespace with a list of strings and add to the list of strings and out put the final result.

ok thanks, like so?:

{%- set ns = namespace(quakes_near=[]) %}
{% set km = states('input_number.quakes_near')|float %}
{% set location = states('input_select.quakes_near') %}
{% for s in states.geo_location|selectattr('attributes.source','eq','usgs_earthquakes_feed')
     if distance(s.entity_id,location) < km %}
      {% set ns.quakes_near = ns.quakes_near + s.entity_id %}
{% endfor %}
{{ns.quakes_near}}

funny thing is I had this already:

      earthquakes_near:
        friendly_name: Quakes near
        value_template: >
          {% set km = states('input_number.quakes_near')|float %}
          {% set location = states('input_select.quakes_near') %}
          {% set ns = namespace(count=0) %}
          {% for s in states.geo_location|selectattr('attributes.source','eq','usgs_earthquakes_feed')
             if distance(s.entity_id,location) < km %}
              {% set ns.count = ns.count + 1 %}
          {% endfor %}
          {{ns.count}}
        attribute_templates:
         raw_list: >
          {% set km = states('input_number.quakes_near')|float %}
          {% set location = states('input_select.quakes_near') %}
          {% for s in states.geo_location|selectattr('attributes.source','eq','usgs_earthquakes_feed')
             if distance(s.entity_id,location) < km %}
          {{- s.entity_id -}}{{',' if not loop.last else ''}}
          {% endfor %}
         list: >
          {% set km = states('input_number.quakes_near')|float %}
          {% set location = states('input_select.quakes_near') %}
          {% set ns = namespace(count=0) %}
          {% for s in states.geo_location|selectattr('attributes.source','eq','usgs_earthquakes_feed')
             if distance(s.entity_id,location) < km %}
              {% set ns.count = ns.count + 1 %}
          {% endfor %}

          {% if ns.count == 0 %} No Quake to list within {{km|int}} km
          {% else %}
          Quakes near:
          {% for s in states.geo_location|selectattr('attributes.source','eq','usgs_earthquakes_feed')
             if distance(s.entity_id,location) < km %}
          {{- s.name -}}: {{s.state|int}} km{{', \n' if not loop.last else ''}}
          {% endfor %}
          {% endif %}

but because the namespace() technique wasn’t necessary for the attributes templates to list the quakes, I thought I could use that same template in the auto-entities.

Must confess I don’t truly grasp why the list my earlier template creates doesn’t work in the auto-entities for Map, and this would. (can’t test it right now, because no quakes are happening thank goodness)