Help understanding RESTful / JSON & template sensors

My first experience with Rest/API and JSON and template sensors. I’m finding the documentation for the restful sensor and the documentation for Jinja just a tad too advanced for my current experience level. I’d be super grateful if someone could explain what’s going on with the example below. I’m struggling to relate the returned JSON and how it parses into the template sensor yaml.
From the restful integration documentation there’s an example using OpenWeatherMap (which I want to adapt later):

sensor:
  - platform: rest
    name: OWM_report
    json_attributes:
      - main
      - weather
    value_template: "{{ value_json['weather'][0]['description'].title() }}"
    resource: https://api.openweathermap.org/data/2.5/weather?zip=80302,us&APPID=VERYSECRETAPIKEY
  - platform: template
    sensors:
      owm_weather:
        value_template: "{{ state_attr('sensor.owm_report', 'weather')[0]['description'].title() }}"
        entity_picture_template: "{{ 'https://openweathermap.org/img/w/' + state_attr('sensor.owm_report', 'weather')[0]['icon'].lower() + '.png' }}"
        entity_id: sensor.owm_report
      owm_temp:
        friendly_name: "Outside temp"
        value_template: "{{ state_attr(['sensor.owm_report', 'main')['temp'] - 273.15 }}"
        unit_of_measurement: "°C"
        entity_id: sensor.owm_report
      owm_pressure:
        etc.

The resource returns this JSON:

{
   "coord":{
      "lon":-0.6154,
      "lat":51.3189
   },
   "weather":[
      {
         "id":803,
         "main":"Clouds",
         "description":"broken clouds",
         "icon":"04d"
      }
   ],
   "base":"stations",
   "main":{
      "temp":287.5,
      "feels_like":287.18,
      "temp_min":286.67,
      "temp_max":288.95,
      "pressure":1019,
      "humidity":84
   },
   "visibility":10000,
   "wind":{
      "speed":2.24,
      "deg":202,
      "gust":7.6
   },
   "clouds":{
      "all":75
   },
   "dt":1635321930,
   "sys":{
      "type":2,
      "id":2008262,
      "country":"GB",
      "sunrise":1635317205,
      "sunset":1635353140
   },
   "timezone":3600,
   "id":6693245,
   "name":"Knaphill",
   "cod":200
}

I’ve tried playing with the yaml to pull out other data from the JSON but I’m fundamentally not understanding something. How do the elements of the YAML relate to the JSON (or vice versa)?
I’ve had a go in the template editor but I’m not making progress with that either :cry:

What does each part of this line actually do and how does it relate to the JSON?

value_template: “{{ value_json[‘weather’][0][‘description’].title() }}”

(Is there a way to view the contents of the value_template?)

This line for the temp sensor parses values from sensor.owm_report and does maths on the value, yes?:

value_template: “{{ state_attr([‘sensor.owm_report’, ‘main’)[‘temp’] - 273.15 }}”

Why doesn’t my additional sensor for “dt” render any data?:

owm_dt:
  friendly_name: "Timestamp"
  value_template: "{{ state_attr('sensor.owm_report', 'dt')['dt'] }}"
  unit_of_measurement: "unit"
  entity_id: sensor.owm_dt

I’ve also tried without success:

  value_template: "{{ state_attr('sensor.owm_report', 'dt') }}"
  value_template: "{{ state_attr('sensor.owm_report')['dt'] }}"

Any and all tutoring greatly appreciated!

Paste the following code into the Template Editor and experiment with it.

Show code
{% set value_json = 
{
   "coord":{
      "lon":-0.6154,
      "lat":51.3189
   },
   "weather":[
      {
         "id":803,
         "main":"Clouds",
         "description":"broken clouds",
         "icon":"04d"
      }
   ],
   "base":"stations",
   "main":{
      "temp":287.5,
      "feels_like":287.18,
      "temp_min":286.67,
      "temp_max":288.95,
      "pressure":1019,
      "humidity":84
   },
   "visibility":10000,
   "wind":{
      "speed":2.24,
      "deg":202,
      "gust":7.6
   },
   "clouds":{
      "all":75
   },
   "dt":1635321930,
   "sys":{
      "type":2,
      "id":2008262,
      "country":"GB",
      "sunrise":1635317205,
      "sunset":1635353140
   },
   "timezone":3600,
   "id":6693245,
   "name":"Knaphill",
   "cod":200
}
%}

{{ value_json.weather[0].description.title() }}


Screenshot of Template Editor:

3 Likes

@123 Thank you!
jsonpathfinder is super helpful, and now I know how to get json into the template editor too.