Get json data from weather oficial chile

Hello, I am not a programmer but I usually solve it by reading, I have been using Home Assistant for 4 years but the first time I am stuck, I want to acquire the official live meteorological data from the official mereology institute from a Json page that is the following:

https://climatologia.meteochile.gob.cl/application/productos/datosRecientesEma/330020

But it won’t let me… the truth is that I’ve been trying for days and nothing, surely it’s nonsense, excuse my ignorance but I swear it’s the first time I’ve asked for help, my code, which I think is where the thing goes, I would like it to be updated every 20 minutes

rest:
  - resource: "https://climatologia.meteochile.gob.cl/application/productos/datosRecientesEma/330020"
    sensor:
      - name: "Mateo temp"
        value_template: "{{ value_json.datos[0].temperatura }}"
        unit_of_measurement: "°C"
      - name: "Mateo Minima"
        value_template: "{{ value_json.datos[0].temperaturaMinima12Horas }}"
        unit_of_measurement: "°C"
      - name: "Mateo Maxima"
        value_template: "{{ value_json.datos[0]temperaturaMaxima12Horas }}"
        unit_of_measurement: "°C"
        scan_interval: 35

If you help me capture the latest published data (every ten minutes) I would appreciate it…

THX

Welcome!

Don’t get too frustrated, parsing these JSON outputs can be a real pain in the :peach:

This is a helpful first stop, paste your raw JSON in this web page, or similar. Stop, get a large glass of :wine_glass: and/or :beer:, and peruse your data, see picture below.

https://jsongrid.com/json-parser

The data that is being returned is fairly large (1/2 megabyte) and not ephemeral (700+ readings) set, both of which add to the the challenge of the ‘basic challenge’ of creating a sensor in Home Assistant from JSON data.

At first glance, the URL API endpoint you are trying to read is returning 24 hours of data for the geographic location you have requested. That is going to be a challenge, as Home Assistant sensors for the most part are a ‘point in time’. So, I think you are going to have to either find a URL API end point that is ‘just now’ values. Or do some more complex extraction of the returned data in order to get a value for the temperature at a specific time for example.

Another tool that can be useful, however frustrating to learn, is the command line tool ‘jq’.

For example, I think, this is the first sets of reading that is returned from the query you posted is shown below. The first issue that you should note is that the 700+ reading are not easily indexed/extracted via a date/time value. They are just a sequence of rows that are difficult to access in JSON via a ‘key’ such as data and time.

dproffer@graymini ~ % cat z.txt | jq '.datosEstaciones.datos[0]'
{
  "momento": "2023-07-22 01:10:00",
  "temperatura": "11.4 °C",
  "temperatura02Mts": null,
  "temperatura10Mts": null,
  "temperatura30Mts": null,
  "puntoDeRocio": "10.8 °C",
  "temperaturaMinima12Horas": "9.5 °C",
  "temperaturaMaxima12Horas": "15.1 °C",
  "humedadRelativa": "95 %",
  "radiacionGlobalInst": " 0.000 Watt/m2",
  "presionEstacion": "954.7 hPas",
  "presionNivelDelMar": "1017.0 hPas",
  "presionNivelEstandar": "1016.6 hPas",
  "aguaCaidaDelMinuto": "0.0 mm",
  "aguaCaida6Horas": "0.0 mm",
  "aguaCaida24Horas": "0.0 mm",
  "direccionDelViento": "167 °",
  "fuerzaDelViento": "1.0 kt",
  "direccionDelVientoPromedio2Minutos": "172 °",
  "fuerzaDelVientoPromedio2Minutos": "0.3 kt",
  "direccionDelVientoPromedio10Minutos": "205 °",
  "fuerzaDelVientoPromedio10Minutos": "0.3 kt",
  "direccionDelViento02MinutosMax": null,
  "fuerzaDelViento02MinutosMax": null,
  "direccionDelViento10MinutosMax": null,
  "fuerzaDelViento10MinutosMax": null
}

Good hunting!

Thanks for answering… and thanks for helping me understand a little better! I still can’t solve it, but I solved the main problem that you mentioned and I got a link with only the recent data

https://climatologia.meteochile.gob.cl/application/productos/emaResumenDiario/330020

Now I’m going to proceed if I solve it hahahaha Thank you!

I solved it… Thank you!!! You enlightened me I was on the wrong route!!! It was another URL with less data… I solved it, thanks for your time

rest:
  - resource: "https://climatologia.meteochile.gob.cl/application/productos/emaResumenDiario/330020"
    sensor:
      - name: "Mateo temp"
        value_template: "{{ value_json.datos.valoresMasRecientes.temperatura }}"
        unit_of_measurement: "°C"
1 Like

Awesome!
I am not an expert here, however, I would make the ‘state’ of the sensor the ‘date and time’ and one or more of the attributes of the sensor the temperature and other readings. This way regardless of whether (weather :wink: ) the sensor’s value change Home Assistant will capture all readings. Although, if your weather service can specific temperature to 4 decimal places, they are pretty awesome ;-).

Glad you are now a JSON pro :slight_smile:

dproffer@graymini ~ % cat z1.txt | jq '.datos.valoresMasRecientes.temperatura'
"10.5587"
dproffer@graymini ~ % cat z1.txt | jq '.datos.valoresMasRecientes.momento'
"2023-07-22 02:35:00"

if you can mark the thread as ‘solved’ I think that will help other folks.

1 Like