Json parse (value_template)

Hello, I need some help in parsing out json out from restful. Platform is rest.

I was wondering what value template i can use to get values like London, Fajr, Dhuhr.

Any help will be highly appreciated. Thanks

{“city”:“london”,“date”:“2020-10-13”,“fajr”:“05:48”,“fajr_jamat”:“06:18”,“sunrise”:“07:19”,“dhuhr”:“12:52”,“dhuhr_jamat”:“01:30”,“asr”:“03:35”,“asr_2”:“04:19”,“asr_jamat”:“04:45”,“magrib”:“06:14”,“magrib_jamat”:“06:21”,“isha”:“07:37”,“isha_jamat”:“08:00”}

value_template: "{{ value_json.london }}"

To get “london”, since it’s the “city” in this JSON you’d use:

sensor:
  - platform: rest
    resource: XXX
    name: XXX
    value_template: '{{ value_json.city }}'

To get the time for fajr you’d use:

sensor:
  - platform: rest
    resource: XXX
    name: XXX
    value_template: '{{ value_json.fajr }}'

I’d need to know specifically what you’re trying to do to give any more information, but it’s all this same basic pattern.

“london” is not a key in this JSON, therefore, what you’ve suggested won’t work.

You’re right. I did not look close enough. Just saw that it was a flat structure and went with the first example mk123 wanted without checking.

Hi, thanks for your response. I am querying a web api to get prayer times. I then need to extract that prayer times and display in UI as a sensor. I have tried to ammend yaml file but not sure how to extract this e.g.FAJR. See my code below please.

  - platform: rest
    name: "Prayer Times 2"
    json_attributes:
      - data
      
    resource: 'https://www.londonprayertimes.com/api/times/?format=json&key=XYZ'
    scan_interval: 3600
    
  - platform: template
    sensors:
      fajr:
        friendly_name: 'Fajr'
        value_template: '{{ value_json.fajr }}'

I don’t use any REST sensors, and I don’t have a KEY for this API to test it myself, but I think you want this:

sensors:
  - platform: rest
    name: prayer_times
    resource: 'https://www.londonprayertimes.com/api/times/?format=json&key=XYZ'
    scan_interval: 3600
    json_attributes:
      - fajr
      - asr

  - platform: template
    sensors:
      fajr:
        value_template: "{{ state_attr('sensor.prayer_times', 'fajr') }}"
      asr:
        value_template: "{{ state_attr('sensor.prayer_times', 'asr') }}"

Also note, while using this API is totally fine if that’s what you prefer, and I don’t know enough about Islamic Prayer Times to know what matters in this regard…

BUT, Home Assistant has a built-in Islamic Prayer Times integration that seems to provide quite a few prayer times based on your longitude and latitude. So, if all you’re after is a few prayer times for your location, this might do the trick more easily. If you just want an exercise in writing REST sensors or you want the times provided by this API specifically (because they are different or something) then continue on and my last comment should get what you want.

You can do that without creating Template Sensors for each prayer time.

Follow swiftlyfalling’s example and create a RESTful Sensor with an attribute for each prayer time you want to report:

sensors:
  - platform: rest
    name: prayer_times
    resource: 'https://www.londonprayertimes.com/api/times/?format=json&key=XYZ'
    scan_interval: 3600
    value_template: '{{value_json.date}}'
    json_attributes:
      - fajr
      - asr
      - dhuhr

If you use a Lovelace Entities card, you can display the sensor’s three attributes directly like this:

  - type: entities
    title: Prayer Times
    show_header_toggle: false
    entities:
      - entity: sensor.prayer_times
        name: Fajr
        type: attribute
        attribute: fajr
      - entity: sensor.prayer_times
        name: Asr
        type: attribute
        attribute: asr
      - entity: sensor.prayer_times
        name: Dhuhr
        type: attribute
        attribute: dhuhr

NOTE

I added this line to the RESTful Sensors configuration:

    value_template: '{{value_json.date}}'

Without it, the entire JSON string will be stored as the sensor’s state. An entity’s state cannot store a string greater than 255 characters in length. The JSON data you posted is currently 255 characters long. If it grows by just one more character, it will fail to be stored.

Hello, thanks for your response. I was actually trying to make use of this rest api. Thanks for your suggestions, which have worked flawlessly. I am adding my final code in case if someone needs it.

  - platform: rest
    name: prayer_times
    resource: 'https://www.londonprayertimes.com/api/times/?format=json&24hours=true&key=123'
    value_template: '{{value_json.date}}'
    scan_interval: 3600
    json_attributes:
      - fajr
      - asr_2
      - isha
      - magrib
      - dhuhr

  - platform: template
    sensors:
      fajr:
        value_template: "{{ state_attr('sensor.prayer_times', 'fajr') }}"
      dhuhr:
        value_template: "{{ state_attr('sensor.prayer_times', 'dhuhr') }}"
      asr:
        value_template: "{{ state_attr('sensor.prayer_times', 'asr_2') }}"
      magrib:
        value_template: "{{ state_attr('sensor.prayer_times', 'magrib') }}"
      isha:
        value_template: "{{ state_attr('sensor.prayer_times', 'isha') }}"
  
1 Like

Thank you sir, i have tried your suggestion and it works great. Cheers

1 Like

Happy Cake Day!

Just wanted to take a moment and say Thank You! I see you on this forum quite a bit, always offering help and doing so with grace and clarity.

1 Like