Help with REST API parsing

Guys,

I would like to monitor the time for Shabbat Candle lithing. there is a rest api for that at the address: http://www.hebcal.com/shabbat/?cfg=json&geonameid=293397 which return the following string value:

{"location":{"longitude":34.78057,"city":"Tel Aviv","title":"Tel Aviv, Israel","geo":"geoname","geonameid":293397,"latitude":32.08088,"admin1":"Tel Aviv","country":"Israel","tzid":"Asia/Jerusalem"},"date":"2017-08-28T07:00:09-00:00","items":[{"title":"Candle lighting: 6:46pm","category":"candles","date":"2017-09-01T18:46:00+03:00","hebrew":"הדלקת נרות"},{"leyning":{"7":"Deuteronomy 24:14 - 25:19","6":"Deuteronomy 24:5 - 24:13","maftir":"Deuteronomy 25:17 - 25:19","2":"Deuteronomy 21:22 - 22:7","5":"Deuteronomy 23:25 - 24:4","torah":"Deuteronomy 21:10 - 25:19","3":"Deuteronomy 22:8 - 23:7","haftarah":"Isaiah 54:1 - 54:10","4":"Deuteronomy 23:8 - 23:24","1":"Deuteronomy 21:10 - 21:21"},"date":"2017-09-02","category":"parashat","hebrew":"פרשת כי־תצא","link":"http://www.hebcal.com/sedrot/kiteitzei","title":"Parashat Ki Teitzei"},{"title":"Havdalah (50 min): 7:53pm","category":"havdalah","date":"2017-09-02T19:53:00+03:00","hebrew":"הבדלה - 50 דקות"}],"link":"http://www.hebcal.com/shabbat/?geonameid=293397;maj=on;min=on;mod=on;mf=on;ss=on;nx=on;m=50;c=on;i=on;geo=geoname","title":"Tel Aviv, Israel"}

I would like to catch the value “2017-09-01T18:46:00+03:00” from items.date (I tried items[0].date without success and also items.date which bring nothing.
Please your assistant :slight_smile:

The API returns JSON encoded strings - probably the first thing you should do is convert it from JSON to native python data structures using the JSON module:

Then you can parse the time string into a python DATETIME with the

import json

...

data = json.loads(json)

Then it should be data["date"], and you can convert it to a python DATETIME with the iso8601 library.

Hi @robin.lior, you can play with json in Dev Tools/Templates.

{% set json = {"location":{"longitude":34.78057,"city":"Tel Aviv","title":"Tel Aviv, Israel","geo":"geoname","geonameid":293397,"latitude":32.08088,"admin1":"Tel Aviv","country":"Israel","tzid":"Asia/Jerusalem"},"date":"2017-08-28T07:00:09-00:00","items":[{"title":"Candle lighting: 6:46pm","category":"candles","date":"2017-09-01T18:46:00+03:00","hebrew":"הדלקת נרות"},{"leyning":{"7":"Deuteronomy 24:14 - 25:19","6":"Deuteronomy 24:5 - 24:13","maftir":"Deuteronomy 25:17 - 25:19","2":"Deuteronomy 21:22 - 22:7","5":"Deuteronomy 23:25 - 24:4","torah":"Deuteronomy 21:10 - 25:19","3":"Deuteronomy 22:8 - 23:7","haftarah":"Isaiah 54:1 - 54:10","4":"Deuteronomy 23:8 - 23:24","1":"Deuteronomy 21:10 - 21:21"},"date":"2017-09-02","category":"parashat","hebrew":"פרשת כי־תצא","link":"http://www.hebcal.com/sedrot/kiteitzei","title":"Parashat Ki Teitzei"},{"title":"Havdalah (50 min): 7:53pm","category":"havdalah","date":"2017-09-02T19:53:00+03:00","hebrew":"הבדלה - 50 דקות"}],"link":"http://www.hebcal.com/shabbat/?geonameid=293397;maj=on;min=on;mod=on;mf=on;ss=on;nx=on;m=50;c=on;i=on;geo=geoname","title":"Tel Aviv, Israel"} %}

{{ json['items'][0]['date'] }}

I think there’s a problem with the items keyword, cause json.items returns
<built-in method items of dict object at 0x7f7724a52b08>

No problem with the items keyword.
https://docs.python.org/3/library/stdtypes.html#dict.values
Below also works with the same set json as above.

   {% for key,value in json.items() %}
   {% if key == "items" %}
   {{ value[2].date }}
   {%endif%}
   {% endfor %}

Dear all,

Thanks for the insights, but I’ll appreciate it if someone can assist me how to implement it.
Likes how do I get at the final one sensor with the related date?

Thank you guys,
Lior

Not sure what you mean. Both @VDRainer and my answers provide a solution that return the date you were asking for.

Try

sensor:
  - platform: rest
    resource: http://www.hebcal.com/shabbat/?cfg=json&geonameid=293397
    method: GET
    value_template: '{{ json['items'][0]['date'] }}'

Thanks, i got the follow errors:

2017-08-30 21:02:12 ERROR (Thread-2) [homeassistant.util.yaml] while parsing a block mapping
  in "/home/pi/.homeassistant/sensors.yaml", line 79, column 3
expected <block end>, but found '<scalar>'
  in "/home/pi/.homeassistant/sensors.yaml", line 82, column 29
2017-08-30 21:02:12 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/pi/.homeassistant/configuration.yaml: while parsing a block mapping
  in "/home/pi/.homeassistant/sensors.yaml", line 79, column 3
expected <block end>, but found '<scalar>'
  in "/home/pi/.homeassistant/sensors.yaml", line 82, column 29

try:

value_template: "{{ value_json['items'][0]['date'] }}"

It’s works like a charms :slight_smile:
The sensor value gets “2017-09-08T18:37:00+03:00”

my question is there any way to translate it to date_time fomat ?

the goal is to have an automation that will run at the spesific time.

@VDRainer, I’ll appreciate your assistance :slight_smile:

Well, post what you have tried here and we will have a look.