Template with a For Loop, why can't I see variables set inside it?

Ok, here we go again…
Can someone explain why this isn’t working?
Why can’t I see the value that the variable lowest is set inside the for loop when I come out of it?

{% set lowest = states('sensor.latest_tasmota_version')[1:] %}

initial lowest {{ lowest }}               {# for testing #}

{% for state in states if (state.entity_id.endswith('firmware')) and state.state != 'unknown' %}

  state name - {{ state.name }}            {# for testing #}
  current state - {{ state.state }}        {# for testing #}       
  current lowest - {{ lowest }}            {# for testing #}
  
  {%- if state.state != 'unknown' and state.state < lowest %}
     {% set lowest = state.state -%}

     new lowest - {{ lowest }}              {# for testing #}

  {%- endif %}
  
  lowest after endif - {{ lowest }}         {# for testing #}
  --------                                  {# for testing #}

{%- endfor %}

lowest after endfor - {{ lowest }}           {# for testing #}

I want to see what the lowest version of firmware is that I have on all my Sonoffs
Thanks in advance.

(No, I haven’t quite got the hang of whitespace control so the above code has been reformatted to make it easier to read!)