🕋

Diyanet Ezan in Home Assistant

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.
 
💡
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.
📧
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!
 
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:
  1. Sensor for ramadan: to check if it is ramadan
  1. Sensor for sahur, to wake you up 40 minutes before it is imsak time
  1. Sensor to return the next prayer time (sensor “Next prayer”) so it displays the HH:MM time of the next prayer.
  1. Sensor to return the time remaining to next prayer
 
You can use all kinds of automations, note that I make use of a boolean (you can make it via the UI, search and you will find) to be able to disable all of these automations. As sometimes it is not really desirable to have my speakers making sound:
 
My personal Namaz automation, I recommend to host all media files locally. Otherwise it is not always reliable. Below is a WIP as I change things from time to time. Use it as insipration. You most likely cannot copy paste this for your own setup. At the very least the name of your speakers will be different. And, I assume your name is not Salih ;)
 
automations.yaml:
- id: '1649060070229' alias: Namaz Automation V4 description: '' trigger: - platform: state #Check if sensor is TRUE. As explained on the website entity_id: - sensor.imsak_vakti - sensor.ogle_namazi - sensor.ikindi_namazi - sensor.yatsi_namazi id: namaz regular times # set ID here, this is for regular times. I defined also other triggers to: 'True' - platform: state # Set specifically if GUNES turns to true. I seperate this for Ramadan entity_id: - sensor.gunes_vakti id: Gunes vakti to: 'True' - platform: state # Again, seperate AKSAM for Ramadan entity_id: - sensor.aksam_namazi id: Aksam vakti to: 'True' - platform: state # If I have a call in teams, set disable prayer to ON. entity_id: sensor.teams_activity to: In a call id: teams activity - platform: state entity_id: input_boolean.next_prayer # for this to work create boolean "next_prayer" see next prayer below id: next prayer to: 'on' - platform: state # Sahur vakti for ramadan entity_id: - sensor.sahur_vakti id: Sahur Vakti to: 'True' - platform: state # For this to work create a boolean "time_left" see time left below entity_id: input_boolean.time_left id: time left to: 'on' condition: # Me or my wife at home? If yes, we continue the automation. If not, we don't . - condition: or conditions: - condition: state entity_id: person.salih_ergezen state: home - condition: state entity_id: person. state: home action: - choose: - conditions: - condition: trigger # specifically for regular times id: namaz regular times sequence: - service: notify.lg_webos_smart_tv # specific to my setup. Send a notification to my tv data: message: Namaz Vakti enabled: true - service: notify.mobile_app_iphone_van_salih # send notification to my iPhone data: message: Helmond'da Namaz Vakti title: Namaz - condition: state entity_id: input_boolean.disableprayer state: 'off' - service: media_player.volume_set data: volume_level: 0.5 target: entity_id: media_player.home_group # play on my home speakers enabled: true - service: media_player.play_media data: media_content_id: media-source://media_source/local/bismillah_ir_rahman.mp3 media_content_type: music/mp3 announce: true target: entity_id: media_player.home_group enabled: true - conditions: - condition: trigger # second trigger. IF ramadan is FALSE we use Gunes vakti for morning prayer id: Gunes vakti - condition: state entity_id: sensor.ramadan state: 'False' sequence: - service: notify.lg_webos_smart_tv data: message: Namaz Vakti enabled: true - service: notify.mobile_app_iphone_van_salih data: message: Helmond'da Namaz Vakti title: Namaz - condition: state entity_id: input_boolean.disableprayer state: 'off' - service: media_player.volume_set data: volume_level: 0.5 target: entity_id: media_player.huiskamer enabled: true - service: media_player.play_media data: media_content_id: media-source://media_source/local/bismillah_ir_rahman.mp3 media_content_type: music/mp3 announce: true target: entity_id: media_player.slaapkamer enabled: true - conditions: - condition: trigger # Aksam vakti, choose if RAMADAN adjust messages and potentially sound played id: Aksam vakti sequence: - choose: - conditions: - condition: state entity_id: sensor.ramadan state: 'True' sequence: - service: notify.lg_webos_smart_tv data: message: Iftar! enabled: true - service: notify.mobile_app_iphone_van_salih data: message: Iftar! title: Iftar! - condition: state entity_id: input_boolean.disableprayer state: 'off' - service: media_player.volume_set data: volume_level: 0.5 enabled: true target: entity_id: media_player.nestmini8137 - service: media_player.play_media data: media_content_id: media-source://media_source/local/bismillah_ir_rahman.mp3 media_content_type: music/mp3 announce: true enabled: true target: entity_id: media_player.nestmini8137 # I play this specifically only in my living room - conditions: - condition: state entity_id: sensor.ramadan # If not ramadan it is just regular prayer. So no special actions state: 'False' sequence: - service: notify.lg_webos_smart_tv data: message: Namaz Vakti enabled: true - service: notify.mobile_app_iphone_van_salih data: message: Rotterdam'da Namaz Vakti title: Namaz - condition: state entity_id: input_boolean.disableprayer state: 'off' - service: media_player.volume_set data: volume_level: 0.5 enabled: true target: entity_id: media_player.av_samsung_soundbar_q990b - service: media_player.play_media data: media_content_id: media-source://media_source/local/bismillah_ir_rahman.mp3 media_content_type: music/mp3 target: entity_id: media_player.huiskamer enabled: true - conditions: - condition: trigger id: teams activity sequence: - service: input_boolean.turn_on target: entity_id: input_boolean.disableprayer data: {} - wait_for_trigger: - platform: state entity_id: sensor.teams_activity from: In a call to: Not in a call continue_on_timeout: false - service: input_boolean.turn_off target: entity_id: input_boolean.disableprayer data: {} - conditions: - condition: trigger # This trigger: I create a boolean called next prayer which I expose to google assistant. With a custom routine I can ask: "What time is next prayer" it returns the time as specified below (in dutch) id: next prayer sequence: - service: tts.google_translate_say data: entity_id: media_player.huiskamer message: Volgende namaz is om {{ states('sensor.next_prayer') }} # "next prayer is at "STATE" language: nl # Define TTS google language enabled: true - service: input_boolean.turn_off data: {} target: entity_id: input_boolean.next_prayer # this works the same as previous. But returns time left. enabled: true - conditions: - condition: trigger id: time left sequence: - service: tts.google_translate_say data: entity_id: media_player.home_group message: '{% if is_state("sensor.namaz_time_remaing", "00:00") %} Het is yatsi geweest {% else %} Nog {%- set ct = today_at(states(''sensor.next_prayer'')) %} {{ ''00:00'' if now() > ct else (ct - now()).total_seconds() | timestamp_custom(''%-H'', false) }} uur en {%- set ct = today_at(states(''sensor.next_prayer'')) %} {{ ''00:00'' if now() > ct else (ct - now()).total_seconds() | timestamp_custom(''%-M'', false) }} minuten {% endif %}' # The message is a bit complicate: If it has been yatsi already. Say "It is yatsi already". Else say "Still HH hours and MM minutes remaining". language: nl enabled: true - service: input_boolean.turn_off data: {} target: entity_id: input_boolean.time_left - conditions: - condition: trigger # Specifically to wake me up for Sahur. id: Sahur Vakti - condition: state entity_id: sensor.ramadan state: 'True' sequence: - service: media_player.volume_set data: volume_level: 0.5 target: entity_id: media_player.googlehome3117 - service: media_player.play_media data: media_content_id: media-source://media_source/local/Allah-hu-Allah-New-ringtone.mp3 media_content_type: music/mp3 announce: true target: entity_id: media_player.googlehome3117music
 
For The front end I created this card to show the correct times. It uses the mushroom-shadow theme and some specific styling.
At the very least you need to have the mushroom theme and card_mod,stack-in-card and layout-card installed
notion image
type: custom:stack-in-card cards: - type: custom:layout-card layout_type: custom:grid layout: grid-template-columns: auto 120px margin: '-4px 0px -10px -4px;' cards: - type: custom:mushroom-template-card primary: Namaz secondary: >- {% if is_state("sensor.namaz_time_remaing", "00:00") %} {{ states('sensor.next_prayer') }} {% else %} {{ states('sensor.next_prayer') }} | {{ states('sensor.namaz_time_remaing') }} tot volgend gebed {% endif %} icon: mdi:hands-pray entity: sensor.next_prayer tap_action: action: none hold_action: action: none icon_color: grey fill_container: false multiline_secondary: false card_mod: style: | ha-card { --ha-card-box-shadow: 0px ; }``` - type: custom:stack-in-card mode: horizontal alignment: end card_mod: style: | ha-card { --ha-card-box-shadow: 0px ; }``` cards: - type: custom:mushroom-template-card entity: input_boolean.disableprayer primary: '' secondary: '' icon: >- {{ 'mdi:volume-off' if is_state(entity, 'on') else 'mdi:volume-high' }} icon_color: '{{ ''red'' if is_state(entity, ''on'') else ''green'' }}' hold_action: action: none - type: custom:mushroom-template-card entity: input_boolean.prayer_dropdown primary: '' secondary: '' icon: >- {{ 'mdi:chevron-up' if is_state(entity, 'on') else 'mdi:chevron-down' }} icon_color: black hold_action: action: none - type: conditional conditions: - entity: input_boolean.prayer_dropdown state: 'on' card: type: glance entities: - type: custom:mushroom-template-card entity: sensor.imsak icon: mdi:hands-pray name: İmsak tap_action: none hold_action: none - type: custom:mushroom-template-card entity: sensor.ogle name: Öğle tap_action: none hold_action: none icon: mdi:hands-pray - type: custom:mushroom-template-card entity: sensor.ikindi name: İkindi tap_action: none hold_action: none icon: mdi:hands-pray card_mod: null - type: custom:mushroom-template-card entity: sensor.aksam name: Akşam tap_action: none hold_action: none icon: mdi:hands-pray - type: custom:mushroom-template-card entity: sensor.yatsi name: Yatsı tap_action: none hold_action: none icon: mdi:hands-pray card_mod: style: | ha-card { --ha-card-box-shadow: 0px ; }```