Ordering bus times array by time (UK transport integration)

Hi there, I’m a bit of a newbie to HA and I’m sure it must be possible to organise these in time order but I’m not sure how. This is the sensor’s state attributes from the developer tools (I’ve censored the exact location):

atcocode: xxxxx
locality: xxxxx
stop_name: xxxxx
request_time: '2021-02-19T16:34:50+00:00'
next_buses:
  - route: '124'
    direction: Cardiff
    scheduled: '17:19'
    estimated: '17:19'
  - route: '24'
    direction: Cardiff
    scheduled: '17:11'
    estimated: '17:11'
  - route: '24'
    direction: Cardiff
    scheduled: '18:11'
    estimated: '18:11'
  - route: '122'
    direction: Cardiff
    scheduled: '16:55'
    estimated: '16:55'
  - route: '122'
    direction: Cardiff
    scheduled: '17:25'
    estimated: '17:25'
  - route: '122'
    direction: Cardiff
    scheduled: '17:55'
    estimated: '17:55'
  - route: '63'
    direction: Cardiff
    scheduled: '17:33'
    estimated: '17:33'
  - route: '63'
    direction: Cardiff
    scheduled: '18:33'
    estimated: '18:33'
  - route: '62'
    direction: Cardiff
    scheduled: '17:03'
    estimated: '17:03'
  - route: '62'
    direction: Cardiff
    scheduled: '18:03'
    estimated: '18:03'
unit_of_measurement: min
friendly_name: Next bus to Cardiff
icon: 'mdi:bus'

As you can see the default order of these next buses is not clear, but it’s not in time order.

What I am trying to achieve is showing the next bus and also the one after, using templates like this:

- platform: template
  sensors:
    next_bus_route:
      friendly_name: 'Next bus route'
      value_template: "{{state_attr('sensor.next_bus_to_cardiff', 'next_buses')[0].route}}"
    next_bus_direction:
      friendly_name: 'Next bus direction'
      value_template: "{{state_attr('sensor.next_bus_to_cardiff', 'next_buses')[0].direction}}"
    next_bus_scheduled:
      friendly_name: 'Next bus scheduled'
      value_template: "{{state_attr('sensor.next_bus_to_cardiff', 'next_buses')[0].scheduled}}"
    next_bus_estimated:
      friendly_name: 'Next bus estimated'
      value_template: "{{state_attr('sensor.next_bus_to_cardiff', 'next_buses')[0].estimated}}"
- platform: template
  sensors:
    next_but_one_bus_route:
      friendly_name: 'Next bus route'
      value_template: "{{state_attr('sensor.next_bus_to_cardiff', 'next_buses')[1].route}}"
    next_but_one_bus_direction:
      friendly_name: 'Next bus direction'
      value_template: "{{state_attr('sensor.next_bus_to_cardiff', 'next_buses')[1].direction}}"
    next_but_one_bus_scheduled:
      friendly_name: 'Next bus scheduled'
      value_template: "{{state_attr('sensor.next_bus_to_cardiff', 'next_buses')[1].scheduled}}"
    next_but_one_bus_estimated:
      friendly_name: 'Next bus estimated'
      value_template: "{{state_attr('sensor.next_bus_to_cardiff', 'next_buses')[1].estimated}}"

But this results in the first two from that array being used and they’re generally not the actual next buses.

Is there any way I can re-order this array before I access indexes 0 and 1? Or is there a better way to do this?

Thanks in advance!

I’ve edited the title to give a better idea of what I’m asking. Hopefully someone out there can help me?

Just found this thread and wanted the exact same thing :wink: Did you managed to sort it at all?

Sadly no, and even more sadly the API has been rendered pretty much unusable by the much lower limits on the free plan so I’ve pretty much abandoned all hope of it working anyway.

Yeah, I’d seen that 30 API calls a day. I’ve had to restrict the refresh to just the 30 mins before the kids leave for school to get updates.