I would suggest you place this in the attributes instead.
That would give you the busses easily indexed and accessible.
I just copy pasted it to make the point. Perhaps you can use the state as something else if you need that?
- platform: template
sensors:
sorted_bus_etas:
friendly_name: "Sorted Bus ETAs"
value_template: >
{% if state_attr('sensor.rest_next_196_bus_a', 'EstimatedArrival') != '' and state_attr('sensor.rest_next_196_bus_a', 'EstimatedArrival') != none %}
{% set bus196A = as_timestamp(state_attr('sensor.rest_next_196_bus_a', 'EstimatedArrival')) | timestamp_custom('%H:%M')%}
{% else %} {%set bus196A = "99:99" %} {%- endif %}
{% if state_attr('sensor.rest_next_196_bus_b', 'EstimatedArrival') != '' and state_attr('sensor.rest_next_196_bus_b', 'EstimatedArrival') != none %}
{% set bus196B = as_timestamp(state_attr('sensor.rest_next_196_bus_b', 'EstimatedArrival')) | timestamp_custom('%H:%M')%}
{% else %} {%set bus196B = "99:99" %} {%- endif %}
{% if state_attr('sensor.rest_next_196e_bus_a', 'EstimatedArrival') != '' and state_attr('sensor.rest_next_196e_bus_a', 'EstimatedArrival') != none %}
{% set bus196eA = as_timestamp(state_attr('sensor.rest_next_196e_bus_a', 'EstimatedArrival')) | timestamp_custom('%H:%M')%}
{% else %} {%set bus196eA = "99:99" %} {%- endif %}
{% if state_attr('sensor.rest_next_196e_bus_b', 'EstimatedArrival') != '' and state_attr('sensor.rest_next_196e_bus_b', 'EstimatedArrival') != none %}
{% set bus196eB = as_timestamp(state_attr('sensor.rest_next_196e_bus_b', 'EstimatedArrival')) | timestamp_custom('%H:%M')%}
{% else %} {%set bus196eB = "99:99" %} {%- endif %}
{% if state_attr('sensor.rest_next_196a_bus_a', 'EstimatedArrival') != '' and state_attr('sensor.rest_next_196a_bus_a', 'EstimatedArrival') != none %}
{% set bus196aA = as_timestamp(state_attr('sensor.rest_next_196a_bus_a', 'EstimatedArrival')) | timestamp_custom('%H:%M')%}
{% else %} {%set bus196aA = "99:99" %} {%- endif %}
{% if state_attr('sensor.rest_next_196a_bus_b', 'EstimatedArrival') != '' and state_attr('sensor.rest_next_196a_bus_b', 'EstimatedArrival') != none %}
{% set bus196aB = as_timestamp(state_attr('sensor.rest_next_196a_bus_b', 'EstimatedArrival')) | timestamp_custom('%H:%M')%}
{% else %} {%set bus196aB = "99:99" %} {%- endif %}
{% set bus_etas = [
{"name": "196", "eta": bus196A},
{"name": "196", "eta": bus196B},
{"name": "196e", "eta": bus196eA},
{"name": "196e", "eta": bus196eB},
{"name": "196a", "eta": bus196aA},
{"name": "196a", "eta": bus196aB}
] %}
{% set sorted_bus_etas = bus_etas | sort(attribute="eta") %}
{{sorted_bus_etas}}
attribute_templates:
busses: >-
{% if state_attr('sensor.rest_next_196_bus_a', 'EstimatedArrival') != '' and state_attr('sensor.rest_next_196_bus_a', 'EstimatedArrival') != none %}
{% set bus196A = as_timestamp(state_attr('sensor.rest_next_196_bus_a', 'EstimatedArrival')) | timestamp_custom('%H:%M')%}
{% else %} {%set bus196A = "99:99" %} {%- endif %}
{% if state_attr('sensor.rest_next_196_bus_b', 'EstimatedArrival') != '' and state_attr('sensor.rest_next_196_bus_b', 'EstimatedArrival') != none %}
{% set bus196B = as_timestamp(state_attr('sensor.rest_next_196_bus_b', 'EstimatedArrival')) | timestamp_custom('%H:%M')%}
{% else %} {%set bus196B = "99:99" %} {%- endif %}
{% if state_attr('sensor.rest_next_196e_bus_a', 'EstimatedArrival') != '' and state_attr('sensor.rest_next_196e_bus_a', 'EstimatedArrival') != none %}
{% set bus196eA = as_timestamp(state_attr('sensor.rest_next_196e_bus_a', 'EstimatedArrival')) | timestamp_custom('%H:%M')%}
{% else %} {%set bus196eA = "99:99" %} {%- endif %}
{% if state_attr('sensor.rest_next_196e_bus_b', 'EstimatedArrival') != '' and state_attr('sensor.rest_next_196e_bus_b', 'EstimatedArrival') != none %}
{% set bus196eB = as_timestamp(state_attr('sensor.rest_next_196e_bus_b', 'EstimatedArrival')) | timestamp_custom('%H:%M')%}
{% else %} {%set bus196eB = "99:99" %} {%- endif %}
{% if state_attr('sensor.rest_next_196a_bus_a', 'EstimatedArrival') != '' and state_attr('sensor.rest_next_196a_bus_a', 'EstimatedArrival') != none %}
{% set bus196aA = as_timestamp(state_attr('sensor.rest_next_196a_bus_a', 'EstimatedArrival')) | timestamp_custom('%H:%M')%}
{% else %} {%set bus196aA = "99:99" %} {%- endif %}
{% if state_attr('sensor.rest_next_196a_bus_b', 'EstimatedArrival') != '' and state_attr('sensor.rest_next_196a_bus_b', 'EstimatedArrival') != none %}
{% set bus196aB = as_timestamp(state_attr('sensor.rest_next_196a_bus_b', 'EstimatedArrival')) | timestamp_custom('%H:%M')%}
{% else %} {%set bus196aB = "99:99" %} {%- endif %}
{% set bus_etas = [
{"name": "196", "eta": bus196A},
{"name": "196", "eta": bus196B},
{"name": "196e", "eta": bus196eA},
{"name": "196e", "eta": bus196eB},
{"name": "196a", "eta": bus196aA},
{"name": "196a", "eta": bus196aB}
] %}
{% set sorted_bus_etas = bus_etas | sort(attribute="eta") %}
{{sorted_bus_etas}}
This should mean:
{{ state_attr('sensor.sorted_bus_etas', 'busses')[0]['name'] }} arrives in {{ state_attr('sensor.sorted_bus_etas', 'busses')[0]['eta'] }}