I have been struggling to develop a template to extract a list of items that are nested in an array (I am not sure if this is the fully correct terminology).
I would ultimately like to display a list of addon names that have pending updates for display in markdown, either in notifications or in lovelace (Something like this example, but this specifically does not work because the items are in an array). I would also like to better understand templating, and how to extract listed information, for other purposes.
The new-ish sensor.supervisor_updates
has an attribute addons
that is an array (correct term?) of addons with updates. I am able to get one addon name from the list, but am struggling with the for
logic, as will as with json & yaml object lists.
Here is an example of the data that is attempting to be parsed, using template example:
{{state_attr('sensor.supervisor_updates','addons')}}
[{"name":"Grocy","slug":"a0d7b954_grocy","description":"ERP beyond your fridge! A groceries & household management solution for your home","state":"started","version":"0.16.0","version_latest":"0.17.0","update_available":true,"repository":"a0d7b954","icon":true,"logo":true},{"name":"Grafana","slug":"a0d7b954_grafana","description":"The open platform for beautiful analytics and monitoring","state":"started","version":"7.5.1","version_latest":"7.5.2","update_available":true,"repository":"a0d7b954","icon":true,"logo":true},{"name":"ESPHome","slug":"a0d7b954_esphome","description":"ESPHome add-on for intelligently managing all your ESP8266/ESP32 devices","state":"started","version":"2022.3.0","version_latest":"2022.3.1","update_available":true,"repository":"a0d7b954","icon":true,"logo":true}]
The template below will return the name of the first addon:
{% set addon_update = state_attr('sensor.supervisor_updates','addons') %}
{% set addon_update_list = addon_update[0]["name"] %}
{{addon_update_list}}
This returns the second:
{% set addon_update = state_attr('sensor.supervisor_updates','addons') %}
{% set addon_update_list = addon_update[1]["name"] %}
{{addon_update_list}}
From here I cannot get the right syntax. How do I iterate the [0]
path? I have tried many options and referred to many other posts, with no luck.
{% set addon_update = state_attr('sensor.supervisor_updates','addons') %}
{% set i = 0 %}
{% for addon_update_list in addon_update[i]['name'] %}
{{addon_update_list}}
{% set i = i+1 %}
{% endfor %}
Any help would be greatly appreciated. Do I need a filter? do I need an if
expression?