How to truncate this sensors loop output to max 255 characters

using:

      list_lightning_strikes:
        entity_id: sensor.time
        friendly_name: Lightning strikes
        value_template: >
          {{ states.geo_location|selectattr('attributes.source','eq','wwlln')| 
                            map(attribute='name') |list|truncate(255,true)}}

I can truncate the list if it would be too long for a HA card.

when using a for loop, this gets more difficult:

      list_lightning_strikes_near:
        entity_id: sensor.time, input_number.lightning_strikes_near
        friendly_name: List lightning strikes near
        value_template: >
          {% set km = states('input_number.lightning_strikes_near')|int %}
          {% set strikes = states.geo_location|selectattr('attributes.source','eq','wwlln')%}
          {% for s in strikes if s.state|int < km %}
            {%- if not loop.first %},{% endif -%}
            {{- s.name.split(':')[1] -}}
            {% else %} No strikes near
          {% endfor %}

or maybe I should say, I don’t now how to truncate this.
I cant add it after the {{- s.name.split(’:’)[1] -}} obviously, but theres really no other place to do so…?
I should somehow set the output to a new variable, and truncate that.
thanks for any assistance.

Something like this might work. (But I’m not petro :slight_smile: )

  list_lightning_strikes_near:
    entity_id: sensor.time, input_number.lightning_strikes_near
    friendly_name: List lightning strikes near
    value_template: >
      {% set km = states('input_number.lightning_strikes_near')|int %}
      {% set strikes = states.geo_location|selectattr('attributes.source','eq','wwlln')%}
      {% set ns = namespace(name = '')
      {% for s in strikes if s.state|int < km %}
        {%- if not loop.first %},{% endif -%}
        {%- set ns.name = ns.name + s.name.split(':')[1] -%}
      {% endfor %}
      {% if ns.name = '' %}
        No strikes near
      {% else %} 
        {{ ns.name.truncate(255) }}
      {% endif %}

thanks! that looks like what I was looking for :wink: Had to get my head around encapsulating the for loop into 1 variable (namespace). This must be it.

edit a few typo’s, but for now (no strikes) this works:

    entity_id: sensor.time, input_number.lightning_strikes_near
    friendly_name: List lightning strikes near
    value_template: >
      {% set km = states('input_number.lightning_strikes_near')|int %}
      {% set strikes = states.geo_location|selectattr('attributes.source','eq','wwlln')%}
      {% set ns = namespace(name = '') %}
      {% for s in strikes if s.state|int < km %}
        {%- if not loop.first %},{% endif -%}
        {%- set ns.name = ns.name + s.name.split(':')[1] -%}
      {% endfor %}
      {% if ns.name == '' %}
        No strikes near
      {% else %} 
        {{ ns.name.truncate(255) }}
      {% endif %}

Marked as solved, thanks @klogg