Help with creating a Restful sensor - InfoDGT

Hello everyone, I have been using Home Assistant for a couple of years now, but I haven’t been very successful with the Restful integration and I can’t get the data I need.

I’m trying to get traffic incident descriptions from the website.

I had this code where I want to obtain all the descriptions from the link as I mentioned and have them delivered in a Home Assistant sensor:

URL: https://infocar.dgt.es/etraffic/BuscarElementos…

rest:
  - resource: https://infocar.dgt.es/etraffic/BuscarElementos?latNS=40.1&longNS=3.6&latSW=39.0&longSW=2.1&zoom=9&accion=getElementos&Camaras=true&SensoresTrafico=true&SensoresMeteorologico=true&Paneles=true&Radares=true&IncidenciasRETENCION=true&IncidenciasOBRAS=false&IncidenciasMETEOROLOGICA=true&IncidenciasPUERTOS=true&IncidenciasOTROS=true&IncidenciasEVENTOS=true&IncidenciasRESTRICCIONES=true&niveles=true&caracter=acontecimiento
    scan_interval: 60  
    sensor:
      - name: traffic_data_test
        json_attributes_path: "$"
        json_attributes:
          - "precision"
          - "poblacion"
          - "fecha"
          - "alias"
          - "suceso"
          - "sentido"
          - "descripcion"
          - "fechaFin"
          - "lng"
          - "pkFinal"
          - "provincia"
          - "codEle"
          - "causa"
          - "carretera"
          - "hora"
          - "estado"
          - "autonomia"
          - "pkIni"
          - "icono"
          - "tipo"
          - "horaFin"
          - "lat"
          - "nivel"

Thank you very much in advance.

An entity’s state value cannot store more than 255 characters (the first descripcion value is 410 characters) so the only place you can store the data is in the entity’s attributes (which can hold about 16K).

Hello, I modified the code to see if I could get something, but there’s no way I understand how to do it. Could you please share how the code should be? I would really appreciate it.

The received data is a list that looks like this:

Do you want the keys (precision, poblacion, fechs, etc) from the list’s first item or the keys from all of the list’s items?

I’d like to get from everyone. :slightly_smiling_face:

If it teaches me, for example, how to get all the “descripcion” and “causa” out, for example, it would be a good start, and from that base I think I could move forward if it’s a lot of work for you.

I don’t think that’s possible.

It’s my understanding that the combination of json_attributes_path and json_attributes lets you get one or more keys from value_json but there are limitations if value_json contains a list. It can get keys from any one list item but not all keys from all list items.

I may be wrong but that’s been my understanding.

For example, try this which only gets the keys from the first list item.

rest:
  - resource: https://infocar.dgt.es/etraffic/BuscarElementos?latNS=40.1&longNS=3.6&latSW=39.0&longSW=2.1&zoom=9&accion=getElementos&Camaras=true&SensoresTrafico=true&SensoresMeteorologico=true&Paneles=true&Radares=true&IncidenciasRETENCION=true&IncidenciasOBRAS=false&IncidenciasMETEOROLOGICA=true&IncidenciasPUERTOS=true&IncidenciasOTROS=true&IncidenciasEVENTOS=true&IncidenciasRESTRICCIONES=true&niveles=true&caracter=acontecimiento
    scan_interval: 60  
    sensor:
      - name: traffic_data_test
        value_template: "{{ value_json | count }}"
        json_attributes_path: "$[0]"
        json_attributes:
          - "precision"
          - "poblacion"
          - "fecha"
          - "alias"
          - "suceso"
          - "sentido"
          - "descripcion"
          - "fechaFin"
          - "lng"
          - "pkFinal"
          - "provincia"
          - "codEle"
          - "causa"
          - "carretera"
          - "hora"
          - "estado"
          - "autonomia"
          - "pkIni"
          - "icono"
          - "tipo"
          - "horaFin"
          - "lat"
          - "nivel"
1 Like

It works! And that is much more than I had before I thank you very much for your help, for now I leave it unresolved in case anyone could clarify the doubts you said. But thank you again. :yum:

1 Like