Jinja and sorting

I need to sort the json output returned from an API which represents a list of items, one of which is a list of dicts, and I’d like to sort by one of the attributes of the dict. It is formatted like so

routes
|-route_ref (int)
|-type (str)
|-times
          |-t (int)
          |-other_attr (str)
          |...

I started with the Templates section in devtools where the following seems to work:

{% for route in state_attr("sensor.my_sensor_name","routes") | sort(attribute="times.t") %}
...
{% endfor %}

However, when I do the same in a markdown card, the browser fails to render it.
How can I troubleshoot if my jinja sort is actually correct and what is the error I’m facing?

Post what you have in the Markdown Card.

type: vertical-stack
cards:
  
  - type: markdown
    content: |-
      <table>
        {% for route in state_attr("sensor.my_sensor_name","routes") | sort(attribute='route_ref') |sort(attribute=route['times.t']) %}
        <tr>
          <td>ref={{route['route_ref']}}</td>
            <td> t= {% for time in route['times'] %}
              {{-time['t']}},
          {% endfor %} </td>
        </tr>
        {% endfor %}
      </table>

When you test that in the Template Editor, does it produce valid code for an HTML table?

FWIW, for a Markdown card, I typically create templates that produce a Markdown table.

Let me know if you need help composing a Markdown table.

1 Like

That’s not correct. If the attribute is times.t then you can’t use sort(attribute=route[...]

in general it seems like you have syntax errors everywhere. This should work

type: vertical-stack
cards:
  
  - type: markdown
    content: |-
      <table>
        {% for route in state_attr("sensor.my_sensor_name","routes") | sort(attribute='route_ref')  %}
        <tr>
          <td>ref={{route['route_ref']}}</td>
            <td> t= {% for time in route['times'] | sort(attribute='t') %}
              {{-time['t']}},
          {% endfor %} </td>
        </tr>
        {% endfor %}
      </table>

assuing your td is correct

Thank you for your response. The errors were introduced when summarizing my code to skip additional formatting, and made a mess in the process. Let me try again:
I’d like the routes list sorted according to the value of times.t (integer, ascending).
Your code will sort the times list by t, then sort the routes list by route_ref which is not what I want.

My code sorts the list by refcount, then sorts by t. which is what it looked like you wanted originally.

If you just want to sort by t, then that’s all you need to do.

type: vertical-stack
cards:
  
  - type: markdown
    content: |-
      <table>
        {% for route in state_attr("sensor.my_sensor_name","routes") | sort(attribute='times.0.t')  %}
        <tr>
          <td>ref={{route['route_ref']}}</td>
            <td> t= {% for time in route['times'] %}
              {{-time['t']}},
          {% endfor %} </td>
        </tr>
        {% endfor %}
      </table>

and if this doesn’t work, then you need to output your object as is without your explanation of the structure because your explanation doesn’t really make sense.

1 Like

Thanks, works like a charm :smiley:

thank you so much, the sort by times.0.t did it!

1 Like