Sorting responses from Calendar

Im really liking the ability to display a reminder of the days events, but have noticed that the response is not correctly sorted by time. Im using the following code:

  - service: calendar.list_events
    target:
      entity_id: calendar.xxx_yyy
    data:
      duration:
        hours: 24
    response_variable: agenda

and get a response like this

2023-07-10T10:30:00-04:00: Item 2
2023-07-10T08:00:00-04:00: Item 1
2023-07-10T14:30:00-04:00: Item 4
2023-07-10T12:30:00-04:00: Item 3

Ideally, id like it to be sorted by time to look like this

2023-07-10T08:00:00-04:00: Item 1
2023-07-10T10:30:00-04:00: Item 2
2023-07-10T12:30:00-04:00: Item 3
2023-07-10T14:30:00-04:00: Item 4

I tried adding this line but it fails ( agenda.events|sort )

Your agenda for today: <p>
{% agenda.events|sort %}
{% for event in agenda.events %}
{{ as_timestamp(event.start)|timestamp_custom ('%Y/%m/%d %H:%M') }} - {{ event.summary }}<br>
{% endfor %}
</p>

Anyone have a solution??

The statement {% agenda.events|sort %} isn’t really doing anything the way you have it set up. You need to either save your sorted list to a variable, or move your sort to the for statement so the list is sorted prior to being looped over:

Your agenda for today: <p>
{% for event in agenda.events|sort(attribute='start') %}
{{ (event.start|as_datetime).strftime('%Y/%m/%d %H:%M') }} - {{ event.summary }}<br>
{% endfor %}
</p>
1 Like

@ Didgeridrew
Thank you. That worked perfectly.