Reading a JSON value from a met.no data set

Hello everyone,
I am looking for a way to process a JSON provided by met.no with current weather data for a specific location as a sensor.
For example, the URL for the JSON is as follows: https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=51.17416597&lon=6.870996516&altitude=44

My goal now is to be able to read the following node from the JSON and return it as a sensor: object►properties►timeseries►0►data►next_6_hours►details►air_temperature_max

After some research, my plan was to do this via a REST call. I then put together the following. I don’t run into any errors, but the sensor is always “unknown”. Does anyone have an idea where my error lies?

Rest:
  - Ressource: https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=51.17416597&lon=6.870996516&altitude=44
    scan_interval: 60
    sensor:
      - name: "Name of my Sensor"
        value_template: "{{ value_json.properties.timeseries[0].data.next_6_hours.details.air_temperature_max }}"
        device_class: temperature
        unit_of_measurement: "°C"

rest and resource should be lower-case, and only one s in resource. If the sensor is created but unknown, I’d suggest that the YAML config you have pasted above is not exactly what is in your configuration.yaml

Careful with polling every 60s: you might get banned.

If this is your first use of the RESTful integration, you’ll need to do a full restart once you’ve corrected the rest: header.

1 Like

Thank you for your quick feedback.
I think I just made a mistake with the copying. It looks as if the “German” terms came along with the copy. The two words are in lower case and there is only one s in resource. Here is another copy of the complete code as it appears in configuration.yaml - this time I made sure that it was copied correctly.

rest:
  - resource: https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=51.17416597&lon=6.870996516&altitude=44
    scan_interval: 60
    sensor:
      - name: "Name of my Sensor"
        value_template: "{{ value_json.properties.timeseries[0].data.next_6_hours.details.air_temperature_max }}"
        device_class: temperature
        unit_of_measurement: "°C"

I also restarted the system completely after saving.
Of course I should increase the interval, you’re right. I will do that immediately afterwards.

You’re getting an HTTP 403 Forbidden error — the server must be sniffing your client.

With:

value_template: "{{ value[:250] }}"

to see what’s being returned (with the unit and device class removed), I get:

Try setting a User-Agent header:

rest:
  - resource: https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=51.17416597&lon=6.870996516&altitude=44
    scan_interval: 3600
    headers:
      User-Agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
    sensor:
      - name: "Name of my Sensor"
        value_template: "{{ value_json.properties.timeseries[0].data.next_6_hours.details.air_temperature_max }}"
        device_class: temperature
        unit_of_measurement: "°C"

Yeah, that works:

Yeah, that’s it - thank you very much for the help and also the complete solution.
In the next step, I will now familiarize myself better with the topic of “user agents” :slight_smile:

It’s no more than a header for the browser — or whatever is making the request — to identify itself.

Some sites “sniff” it to customise the content that they serve: perhaps working around browser bugs, blocking bots etc.

There’s no security around it though, hence my code above working by claiming to be a normal web browser when it isn’t.

1 Like