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
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?
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.