Noob help - Create sensor and use JSON results

Hi Guys,

I’m new to Home Assistant and haven’t coded for over 10 years and am trying to get into starting my own project. I’m trying to create a few sensors which would display prayer times (which change daily).

The data is to be retrieved from a URL that I have which provides this data in JSON format.

An example of the results from the URL they supply is like so:

{"city":"london","date":"2019-05-06","fajr":"03:41","fajr_jamat":"03:56","sunrise":"05:21","dhuhr":"01:02","dhuhr_jamat":"01:30","asr":"05:04","asr_2":"06:09","asr_jamat":"06:24","magrib":"08:34","magrib_jamat":"08:49","isha":"09:45","isha_jamat":"10:15"}

How would I go about, for example, creating a sensor called ‘Fajr’ which retrieves the time “03:41” from this example as it’s value? I would need the sensor to get these values daily from the URL.

I was looking at the Restful sensor platform, but have no idea whether that is relevant?

ANY help would be most appreciated in helping me get started on my small project.

Thank you!

You’re on the way with the Restful sensor.
Look at the examples in the docs.
Then, to get the values, you can copy and paste the results in Dev Tools/Templates.

{% set value_json =  {"city":"london","date":"2019-05-06","fajr":"03:41","fajr_jamat":"03:56","sunrise":"05:21","dhuhr":"01:02","dhuhr_jamat":"01:30","asr":"05:04","asr_2":"06:09","asr_jamat":"06:24","magrib":"08:34","magrib_jamat":"08:49","isha":"09:45","isha_jamat":"10:15"} %}
{{ value_json.fajr }}

Returns ‘03:41’

1 Like

Thank you VDRainer! I have managed to create the sensors using the RESTful and template sensors. All working now and I’m actually quite impressed :slight_smile:

I now just need to make a few tweaks to make a trigger activate when the current time matches the sensor time.

How can I add an extra 12 hours to the “sensor.elm_zuhr” value, because the time is currently in a 12-hour format and I need it to be a 24-hr format to compare against a 24-hr time sensor. So far I have this:

- id: '1517693010922' alias: Adhan trigger: - platform: template value_template: '{{ as_timestamp(strptime(states("sensor.time"), "%H:%M")) == as_timestamp(strptime(states("sensor.elm_zuhr"), "%H:%M")) }}'

Any help would again be most appreciated from anyone.

This was an interesting challenge and i played a bit.

Saw that you edited your post, but i think adding 12 hours is not a good solution.

I assume the times in the json response are in ascending order, so when the items time is lower than that before, the following must be PM.

Here’s the jinja that works in the Template editor, hope it also works with the Restful sensor.

{% set value_json = {"city":"london","date":"2019-05-06","fajr":"03:41","fajr_jamat":"03:56","sunrise":"05:21","dhuhr":"01:02","dhuhr_jamat":"01:30","asr":"05:04","asr_2":"06:09","asr_jamat":"06:24","magrib":"08:34","magrib_jamat":"08:49","isha":"09:45","isha_jamat":"10:15"} %}
{% set key = 'dhuhr' %} # <- set the wanted key here and remove this comment
{% set ns = namespace(str='AM') %}
{% for item in value_json.items() -%}
  {% if not loop.last -%}
    {% set val1 = item[1] -%}
    {% set val2 = loop.nextitem[1] -%}
    {% if ':' in val1 and ':' in val2 -%}
      {% set val1num = val1.replace(':','') | int -%}
      {% set val2num = val2.replace(':','') | int -%}
      {% if item[0] == key %}
        {{ item[1] }} {{ ns.str }}
      {% endif -%}
      {% if val1num > val2num and ns.str == 'AM' -%}
        {% set ns.str = 'PM' -%}
      {% endif -%}
    {% endif -%}
  {% else -%}
    {% if item[0] == key %}
      {{ item[1] }} {{ ns.str }}
    {% endif -%}
  {% endif -%}
{% endfor -%}

Returns: 01:02 PM

Now, to compare with the sensor.time, you need to add the date_time_iso to your Date&Time sensor

{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%I:%M %p') }}

Returns: 04:55 PM

Hope that works and ‘Happy praying’ (hope i can say that :slightly_smiling_face:)