How to order json data before showing it on lovelace card

Hi, I am playing with a json who has info about gas stations prices in my city, without knowing nothing about rest sensor, I’ve managed to get that info this way:

rest:
  - resource: https://sedeaplicaciones.minetur.gob.es/ServiciosRESTCarburantes/PreciosCarburantes/EstacionesTerrestres/FiltroMunicipioProducto/1220/1
    scan_interval: 86400
    sensor:
      - name: Gasolinera 1 nombre
        value_template: "{{ value_json['ListaEESSPrecio'][0]['Rótulo'] }}"
      - name: Gasolinera 1 precio
        value_template: "{{ value_json['ListaEESSPrecio'][0]['PrecioProducto'] }}"
        unit_of_measurement: "€"
      - name: Gasolinera 1 dirección
        value_template: "{{ value_json['ListaEESSPrecio'][0]['Dirección'] }}"
#      - name: Gasolinera 2 nombre
#        value_template: "{{ value_json['ListaEESSPrecio'][1]['Rótulo'] }}"
#      - name: Gasolinera 2 precio
#        value_template: "{{ value_json['ListaEESSPrecio'][1]['PrecioProducto'] }}"
#        unit_of_measurement: "€"
#      - name: Gasolinera 2 dirección
#        value_template: "{{ value_json['ListaEESSPrecio'][1]['Dirección'] }}"   
#      - name: Gasolinera 3 nombre
#        value_template: "{{ value_json['ListaEESSPrecio'][2]['Rótulo'] }}"
#     - name: Gasolinera 3 precio
#        value_template: "{{ value_json['ListaEESSPrecio'][2]['PrecioProducto'] }}"
#        unit_of_measurement: "€"
#      - name: Gasolinera 3 dirección
#        value_template: "{{ value_json['ListaEESSPrecio'][2]['Dirección'] }}"

This return the json data:

{
   "Fecha": "22/03/2022 12:10:11",
   "ListaEESSPrecio": [
      {
         "C.P.": "09003",
         "Dirección": "CL GENERAL SANZ PASTOR, S.N.",
         "Horario": "L-V: 08:00-16:00",
         "Latitud": "42,344667",
         "Localidad": "BURGOS",
         "Longitud (WGS84)": "-3,698667",
         "Margen": "D",
         "Municipio": "Burgos",
         "PrecioProducto": "1,789",
         "Provincia": "BURGOS",
         "Remisión": "OM",
         "Rótulo": "REPSOL",
         "Tipo Venta": "P",
         "IDEESS": "5989",
         "IDMunicipio": "1220",
         "IDProvincia": "09",
         "IDCCAA": "08"
      },
      {
         "C.P.": "09001",
         "Dirección": "LOPEZ BRAVO, 93",
         "Horario": "L-V: 06:30-22:00; S: 08:00-14:30",
         "Latitud": "42,362528",
         "Localidad": "BURGOS",
         "Longitud (WGS84)": "-3,762083",
         "Margen": "D",
         "Municipio": "Burgos",
         "PrecioProducto": "1,798",
         "Provincia": "BURGOS",
         "Remisión": "dm",
         "Rótulo": "VILLALÓN",
         "Tipo Venta": "P",
         "IDEESS": "9803",
         "IDMunicipio": "1220",
         "IDProvincia": "09",
         "IDCCAA": "08"
      },
      {
         "C.P.": "09001",
         "Dirección": "CALLE CONDADO DE TREVIÑO, 30",
         "Horario": "L-D: 06:00-00:00",
         "Latitud": "42,370528",
         "Localidad": "BURGOS",
         "Longitud (WGS84)": "-3,718528",
         "Margen": "I",
         "Municipio": "Burgos",
         "PrecioProducto": "1,792",
         "Provincia": "BURGOS",
         "Remisión": "dm",
         "Rótulo": "VILLALONQUEJAR",
         "Tipo Venta": "P",
         "IDEESS": "11878",
         "IDMunicipio": "1220",
         "IDProvincia": "09",
         "IDCCAA": "08"
      }]
}

And so more results.
Then in a lovelace card I show the info like this:

title: Gasolineras
type: entities
entities:
  - entity: sensor.gasolinera_1_precio
    name: Gasolinera 1
    type: custom:secondaryinfo-entity-row
    secondary_info: '[[ sensor.gasolinera_1_nombre ]] - [[ sensor.gasolinera_1_direccion ]]'
  - entity: sensor.gasolinera_2_precio
    name: Gasolinera 2
    type: custom:secondaryinfo-entity-row
    secondary_info: '[[ sensor.gasolinera_2_nombre ]] - [[ sensor.gasolinera_2_direccion ]]'
  - entity: sensor.gasolinera_3_precio
    name: Gasolinera 3
    type: custom:secondaryinfo-entity-row
    secondary_info: '[[ sensor.gasolinera_3_nombre ]] - [[ sensor.gasolinera_3_direccion ]]'

But this gives me the first 3 results of course. My questions is, is it possible to order all the results for example by the field “Precio” (Price) and then in the card show the lower 3 gas stations of all of the results?

Thank you as always!

Ohh I’ve been playing on template editor with the solution on this topic https://community.home-assistant.io/t/value-template-sort-json-array/140894/1

And I think I got it, but I don’t know where to put that, and how to show it on lovelace…

{% set ListaEESSPrecio = [
      {
         "C.P.": "09003",
         "Dirección": "CL GENERAL SANZ PASTOR, S.N.",
         "Horario": "L-V: 08:00-16:00",
         "Latitud": "42,344667",
         "Localidad": "BURGOS",
         "Longitud (WGS84)": "-3,698667",
         "Margen": "D",
         "Municipio": "Burgos",
         "PrecioProducto": "1,789",
         "Provincia": "BURGOS",
         "Remisión": "OM",
         "Rótulo": "REPSOL",
         "Tipo Venta": "P",
         "IDEESS": "5989",
         "IDMunicipio": "1220",
         "IDProvincia": "09",
         "IDCCAA": "08"
      },
      {
         "C.P.": "09001",
         "Dirección": "LOPEZ BRAVO, 93",
         "Horario": "L-V: 06:30-22:00; S: 08:00-14:30",
         "Latitud": "42,362528",
         "Localidad": "BURGOS",
         "Longitud (WGS84)": "-3,762083",
         "Margen": "D",
         "Municipio": "Burgos",
         "PrecioProducto": "1,798",
         "Provincia": "BURGOS",
         "Remisión": "dm",
         "Rótulo": "VILLALÓN",
         "Tipo Venta": "P",
         "IDEESS": "9803",
         "IDMunicipio": "1220",
         "IDProvincia": "09",
         "IDCCAA": "08"
      },
      {
         "C.P.": "09001",
         "Dirección": "CALLE CONDADO DE TREVIÑO, 30",
         "Horario": "L-D: 06:00-00:00",
         "Latitud": "42,370528",
         "Localidad": "BURGOS",
         "Longitud (WGS84)": "-3,718528",
         "Margen": "I",
         "Municipio": "Burgos",
         "PrecioProducto": "1,792",
         "Provincia": "BURGOS",
         "Remisión": "dm",
         "Rótulo": "VILLALONQUEJAR",
         "Tipo Venta": "P",
         "IDEESS": "11878",
         "IDMunicipio": "1220",
         "IDProvincia": "09",
         "IDCCAA": "08"
      }

] %}

{% for item in ListaEESSPrecio | sort(attribute='PrecioProducto') %}
    {{ item.Rótulo, item.PrecioProducto, item.Dirección }}
{%- endfor %}

The result

('REPSOL', '1,789', 'CL GENERAL SANZ PASTOR, S.N.')
    ('VILLALONQUEJAR', '1,792', 'CALLE CONDADO DE TREVIÑO, 30')
    ('VILLALÓN', '1,798', 'LOPEZ BRAVO, 93')

Please someone step in…

I think I almos have it… I have tried with a loop inside the for, but I only get the first of the last, how can I get each item of the array, like when we selected with [0], [1]…

I tried this approach:

          {% for item in value_json['ListaEESSPrecio'][0] | sort(attribute='PrecioProducto') %}
            {% if loop.index == 1 %}
              {{ item.Rótulo, item.PrecioProducto, item.Dirección }}
            {% endif %}
          {% endfor %}

And changing the {% if loop.index == 1 %} for {% if loop.index == 2 %} but I get always the first result, and I tested it on the template editor, and there is it working…

Someone help me on stackoverflow, with this problem, and I am posting the solution in order that for someone may be usefull.

{% set item = (ListaEESSPrecio | sort(attribute='PrecioProducto'))[0] %}
{{ item['Rótulo'], item['PrecioProducto'], item['Dirección'] }}