Home assistant is a great tool to automate your house. As we have recently move to our own apartment I wanted to integrate the muslim prayer times in Home assistant so that I could get automatic reminders when it is time to pray.
Unfortunatly, the official integration for these prayer times does not support Diyanet (Turkey) Ezan times. Additionally, all integrations or API’s that do provide times based on the Diyanet calculation method always seemed to give slightly different times than the ones I get when looking it up on the official website.
<aside> 💡 originally I wrote this guide to scrape the data from the original website. But the diyanet website currently blocks this method. The scrape sensor is therefore not recommended. Furkan Tektas created an API that shows the same exact times as the official diyanet website.
</aside>
<aside> 📧 If you have any questions or issues with the Diyanet Ezan integration in Home Assistant, please feel free to send me an email. My email address can be found on my website. Thank you for your interest in this project!
</aside>
For this to work we need to get the diyanet prayer times from: https://ezanvakti.herokuapp.com. You will have to change the number in the end of the url (see below) to match your local city. You can find this number via the https://namazvakitleri.diyanet.gov.tr/. Change the country and city. The number in the URL can be used to change the script below:
Configuration.yaml :
rest:
method: GET
scan_interval: 172800
resource: "<https://ezanvakti.herokuapp.com/vakitler/13978>"
sensor:
- name: Imsak
value_template: >-
{% for entry in value_json %} {% if entry.MiladiTarihKisa ==
now().strftime('%d.%m.%Y') %} {{ entry.Imsak }} {% endif %} {% endfor %}
- name: Ogle
value_template: >-
{% for entry in value_json %} {% if entry.MiladiTarihKisa ==
now().strftime('%d.%m.%Y') %} {{ entry.Ogle }} {% endif %} {% endfor %}
- name: Ikindi
value_template: >-
{% for entry in value_json %} {% if entry.MiladiTarihKisa ==
now().strftime('%d.%m.%Y') %} {{ entry.Ikindi }} {% endif %} {% endfor
%}
- name: Aksam
value_template: >-
{% for entry in value_json %} {% if entry.MiladiTarihKisa ==
now().strftime('%d.%m.%Y') %} {{ entry.Aksam }} {% endif %} {% endfor %}
- name: Yatsi
value_template: >-
{% for entry in value_json %} {% if entry.MiladiTarihKisa ==
now().strftime('%d.%m.%Y') %} {{ entry.Yatsi }} {% endif %} {% endfor %}
- name: hicri
value_template: >-
{% for entry in value_json %} {% if entry.MiladiTarihKisa ==
now().strftime('%d.%m.%Y') %} {{ entry.HicriTarihUzun }} {% endif %} {%
endfor %}
- name: Gunes
value_template: >-
{% for entry in value_json %} {% if entry.MiladiTarihKisa ==
now().strftime('%d.%m.%Y') %} {{ entry.Gunes }} {% endif %} {% endfor %}
The scan_interval is set to 48 hours. You do not have to change this as the API shows the times for the complete month.
The above snipped will create a couple of sensors (sensor.imsak etc) that display HH:MM timestamp for each prayer. As the API returns data for the complete month we need to match the day of the month with the data returned from the API. This is done in the value template.
Use below code to check if the data returned by the sensors created in above code matches the current time:
- platform: template
sensors:
imsak_vakti:
friendly_name: İmsak Vakti
value_template: "{{ states('sensor.imsak') == states('sensor.time') }}"
- platform: template
sensors:
gunes_vakti:
friendly_name: Güneş Vakti
value_template: >-
{%- set time = today_at(states('sensor.gunes')) %} {%- set delta =
timedelta(minutes=35) %} {%- set start = time - delta if time >
today_at('00:00') + delta else time + timedelta(days=1) - delta %}
{%- set end = time + timedelta(days=1) if time <= today_at('00:00')
+ delta and not today_at('00:00') <= now() <= time else time %} {{
start <= now() <= end }}
- platform: template
sensors:
ogle_namazi:
friendly_name: Öğle Vakti
value_template: "{{ states('sensor.ogle') == states('sensor.time') }}"
- platform: template
sensors:
ikindi_namazi:
friendly_name: İkindi Vakti
value_template: "{{ states('sensor.ikindi') == states('sensor.time') }}"
- platform: template
sensors:
aksam_namazi:
friendly_name: Akşam Vakti
value_template: "{{ states('sensor.aksam') == states('sensor.time') }}"
- platform: template
sensors:
yatsi_namazi:
friendly_name: Yatsı Vakti
value_template: "{{ states('sensor.yatsi') == states('sensor.time') }}"
- platform: template
sensors:
sahur_vakti:
friendly_name: Sahur Vakti
value_template: >-
{%- set time = today_at(states('sensor.imsak')) %} {%- set delta =
timedelta(minutes=40) %} {%- set start = time - delta if time >
today_at('00:00') + delta else time + timedelta(days=1) - delta %}
{%- set end = time + timedelta(days=1) if time <= today_at('00:00')
+ delta and not today_at('00:00') <= now() <= time else time %} {{
start <= now() <= end }}
- platform: template
sensors:
namaz_time_remaing:
friendly_name: Namaz time remaining
value_template: >-
{%- set ct = today_at(states('sensor.next_prayer')) %} {{ '00:00' if
now() > ct else (ct - now()).total_seconds() |
timestamp_custom('%H:%M', false) }}
- platform: template
sensors:
ramadan:
friendly_name: Ramadan
value_template: >-
{% if 'Ramazan' in states('sensor.hicri') %} True {% else %} False {%
endif %}
- platform: template
sensors:
next_prayer:
friendly_name: Next Prayer
value_template: >-
{% set state = states('sensor.time') %} {% if '00:00' <= state <
states('sensor.imsak') %} {{ states('sensor.imsak') }} {% elif
states('sensor.imsak') <= state < states('sensor.gunes') %} {{
states('sensor.gunes') }} {% elif states('sensor.gunes') <= state <
states('sensor.ogle') %} {{ states('sensor.ogle') }} {% elif
states('sensor.ogle') <= state < states('sensor.ikindi') %} {{
states('sensor.ikindi') }} {% elif states('sensor.ikindi') <= state <
states('sensor.aksam') %} {{ states('sensor.aksam') }} {% elif
states('sensor.aksam') <= state < states('sensor.yatsi') %} {{
states('sensor.yatsi') }} {% elif states('sensor.yatsi') <= state <
'23:59' %} {{ states('sensor.imsak') }} {% endif %}
As stated previously the above code checks if the time now matches the time we got from the API. If this is true the sensor created will return true. This happens for each prayer. For the morning prayer I make the sensor trigger 35 minutes before the sunrise time (gunes). Additionally above creates: