Numeric Template Sensor Value Not Available in Any LoveLace Card

Hi,

I have defined template to read the 30 minute electricity export values over defined time. It use a known and proven HACS sensor(sensor.agile_export_rate) gets dict of todays 30 mins prices. The sorts them and determines if any the export prices are above my target profit. input_number.target_profit_am

Within the Developer tools it works perfectly and returns the count as expected and there are no errors generated etc.

However, when use the sensor in any Lovelace card i get “Unavailable” but, if I use the “States” tool and set a value manually it displays correctly. I had thought that the line {{ {{ ns.count }} would return the value I see the Developer tool.

I share the code in the hope someone can show me the errors of the ways…
sensor:

  • platform: template
    sensors:
    discharge_for_profit_am:
    friendly_name: “AM Profit Slots”
    value_template: >-
    {% set ns = namespace(count = 0) %}
    {% set rates_today = state_attr(‘sensor.agile_export_rate’, ‘rates_today’) %}
    {% if rates_today | count > 0 %}
    {% set sorted_rates_today = dict((rates_today.items() | list)[52:102]) | dictsort %}
    {% if sorted_rates_today | count > 0 %}
    {% set time_values = sorted_rates_today | map(attribute=‘0’) | list %}
    {% for time in [‘05:00’, ‘05:30’, ‘06:00’, ‘06:30’, ‘07:00’, ‘07:30’, ‘08:00’, ‘08:30’, ‘09:00’, ‘09:30’] %}
    {% if time in time_values %}
    {{ time }}: {{ sorted_rates_today | selectattr(‘0’, ‘eq’, time) | map(attribute=‘1’) | list | first }}
    {% set rate_value = sorted_rates_today | selectattr(‘0’, ‘eq’, time) | map(attribute=‘1’) | list | first %}
    {% if rate_value > states(‘input_number.target_profit_am’) | float / 100 %}
    {% set ns.count = ns.count + 1 %}
    {{ ns.count }}
    {% endif %}
    {% else %}
    {{ time }} #time out of range
    {% endif %}
    {% endfor %}
    {% else %}
    {% set ns.count = 0 %} # No rates found between in the morning
    {% endif %}
    {% else %}
    {% set ns.count = 0 %} # Rate information is unavailable.
    {% endif %}
    {{ ns.count }}
sensor:
  - platform: template
    sensors:
      discharge_for_profit_am: 
        friendly_name: "AM Profit Slots"
        value_template: >-
          {% set ns = namespace(count = 0) %}
          {% set rates_today = state_attr('sensor.agile_export_rate', 'rates_today') %}
          {% if rates_today | count > 0 %}
            {% set sorted_rates_today = dict((rates_today.items() | list)[5*2:10*2]) | dictsort %}
            {% if sorted_rates_today | count > 0 %}
              {% set time_values = sorted_rates_today | map(attribute='0') | list %}
              {% for time in ['05:00', '05:30', '06:00', '06:30', '07:00', '07:30', '08:00', '08:30', '09:00', '09:30'] %}
                {% if time in time_values %}
                  {{ time }}: {{ sorted_rates_today | selectattr('0', 'eq', time) | map(attribute='1') | list | first }}
                     {% set rate_value = sorted_rates_today | selectattr('0', 'eq', time) | map(attribute='1') | list | first %}
                      {% if rate_value > states('input_number.target_profit_am') | float / 100 %}
                        {% set ns.count = ns.count + 1 %}
                          {{  ns.count  }}
                       {% endif %}
                {% else %}
                  {{ time }} #time out of range
                {% endif %}
              {% endfor %}
            {% else %}
              {% set ns.count = 0 %} # No rates found between in the morning
            {% endif %}
          {% else %}
            {% set ns.count = 0 %} # Rate information is unavailable.
          {% endif %}
          {{ ns.count }}```