Template looping through state-attributes

Hello, I am trying to create a card listing items based on values of attributes.
Being new to templates I am stuck … not being able to show all attribs in the entity
With below example, it only shows the first 2 lines.
When I add 2 dummy attributes to it (e.g. test1 and test2), it works fine so for some reason it cannot distinguish having two attribs with each 4 values.

Attributes in the entity (sensor.homework) look like this


  • 1
  • 2
  • 3
  • 4
  • task1
  • task2
  • task3
  • task4

To get the data out on display, for use markdown card

    {%- for attr in states.sensor.homework.attributes -%}
   {% endfor %}

I think I once used a markdown card with just {{ states.sensor.homework.attributes }}

Give it a try, I remember it being rather easy.
What is creating the sensor?

Hi, I am creating the sensor myself so I have full control over content/values…just that this ‘interesting’ iteration does not seem to work (for me). The key challenge is that I want to display a list of attribs and their values where ‘markdown card’ seems to provide the only option for that.
Could you please indicate how you would have used your solution?

This card (another card, set up differently):


is set up like this:

and the sensor looks like this:

I believe your content should just be the yaml I wrote in the previous post.

Thanks but this seems to be one single attrib…or at least…for what I can read. In my case there are two attribs each with #values between 1 and 14…the one thing that is stable is that the number of values between attribs is always the same, i.e. if ‘date’ has 10 values, so has ‘description’

The formatting of your original post makes it look like “description” is a sub-attribute of 4…

I’m going to assume that description is an attribute… and there are no other attributes.

That is because states.sensor.homework.attributes returns a dictionary with 2 items… meaning there are only 2 “attr” in for attr in states.sensor.homework.attributes… meaning there will only be 2 rotations through the loop. By adding two dummy attributes, you are increasing the number of rotations to 4 which matches your test setup, but it would fail if you had more dates or tasks.

Instead of basing your number of rotations through the loop on the number of items in the dictionary, you need to use the number of values in one of the attributes’ list.

You could use the count filter to define a range and use that as the index:

{% set dates = state_attr('sensor.homework','date') %}
{% for i in range(0, dates | count, 1) %}
{{ dates[i] }}
{{ state_attr('sensor.homework','description')[i] }}
{% endfor %}

Or, if you just wanted to use loop.index, you could get the same results like this:

{% set d = states.binary_sensor.homework.attributes %}
{% for attr in d['date'] %}
{{ d['date'][loop.index -1] }}
{{ d['description'][loop.index -1] }}
{% endfor %}

Yes that is correct.
It’s one long string in the attribute “text”.
But you said before you had total control of the sensor. So why not make it easy for yourself and just make it one string?

Hello… and this above did the trick… you do not want to know how much time I spent on &^#@ template-writing…so a HUGE thanks :grinning:

1 Like