Count updates available

I am very new to HA templates but was looking for solution for few days and can’t fix. I would like to count number of availeble updates in

sensor.supervisor_updates
addons:
update_available: true

I manage to gat this working but don’t knowe how to count true results.

update_ave:

    value_template: >-

      {% set addons = state_attr('sensor.supervisor_updates', 'addons')%}

      {% for update in addons %}

        {{update.update_available}}

      {% endfor %}
value_template: >-
  {{ state_attr('sensor.supervisor_updates', 'addons') 
      | selectattr('update_available', 'true') | list | count }}

Truth be told, you usually don’t have to for loop in jinja templates. In the cases when you do its a bit annoying. You need to use namespaces to capture state that can then be used outside the loop. I’d recommend reading HA’s guide on templating if you haven’t yet. Also Jinja has quite detailed docs, the list of built-in tests and filters is a particularly handy reference.

Also for the particular use case of managing updates, I think you may find this helpful.

1 Like

Thank you it is working great.

I read HA’s guide for templating and namespaces but I am just very bad at coding. Also thank you for other links. Hope I will get better at this.

1 Like