Adhan Automation (الأذان التلقائي للصلاة) using google speaker

Salam all, thanks for all of the information posted by previous posters. I just recently got into Home Assistant and was working on getting this setup (previously was using Tasker on Android which worked OK, but was not 100% reliable). In the end, I ended up using a combination of the first post here and a post on another guide as well with some additional tweaks.

  1. I have 3 devices in my house - fortunately after adding them to a speaker group in the Google Home App, Home Assistant recognized that entity and I’m able to have it directly broadcast to that group to hit all of the speakers at the same time.

  2. For Hanafi timing, rather than using the built in prayer time sensor I instead use the Al-Adhan API (as mentioned by someone else here). You can customize it to your calculation method and school via this guide - https://aladhan.com/prayer-times-api.

  3. I have one automation for Fajr specifically (plays at a lower volume) and one for the remaining times. The sequence in each automation is set speakers to volume for athan, play athan, stop playing media (it takes the display back to the photo frame then), and then set volume back to baseline levels. I’ve also made some changes to pause TVs when athan is playing automatically.

After doing the above, it was triggering manually just fine but it was having trouble with the timed trigger so I made a couple of changes for that as well.

In the end, my configuration file looked like this:

default_config:
mobile_app:
tts:
  - platform: google_translate
group: !include groups.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
automation: !include automations.yaml

sensor:
  - platform: rest
    name: "Prayer Times"
    json_attributes:
      - data
    resource: "http://api.aladhan.com/v1/timings?latitude=INSERT_LATITUDE&longitude=INSERT_LONGITUDE&method=2&school=1"
    value_template: '{{ value_json["data"]["meta"]["method"]["name"].title() }}'
    scan_interval: 43200

  - platform: template
    sensors:
      fajr:
        friendly_name: 'Fajr'
        entity_id: sensor.time
        value_template: '{{ states.sensor.prayer_times.attributes.data.timings["Fajr"] | timestamp_custom("%H:%M") }}'
      dhuhr:
        friendly_name: 'Dhuhr'
        entity_id: sensor.time
        value_template: '{{ states.sensor.prayer_times.attributes.data.timings["Dhuhr"] | timestamp_custom("%H:%M") }}'
      asr:
        friendly_name: 'Asr'
        entity_id: sensor.time
        value_template: '{{ states.sensor.prayer_times.attributes.data.timings["Asr"] | timestamp_custom("%H:%M") }}'
      magrib:
        friendly_name: 'Magrib'
        entity_id: sensor.time
        value_template: '{{ states.sensor.prayer_times.attributes.data.timings["Maghrib"] | timestamp_custom("%H:%M") }}'
      isha:
        friendly_name: 'Isha'
        entity_id: sensor.time
        value_template: '{{ states.sensor.prayer_times.attributes.data.timings["Isha"] | timestamp_custom("%H:%M") }}'
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'time_date'

And my automation file looks like:

- id: '1517693010922'
  alias: Fajr Adhan
  trigger:
  - platform: template
    value_template: '{{ states.sensor.fajr.state == states.sensor.time.state }}'
  condition: []
  action:
  - data:
      entity_id: media_player.homespeakers
      volume_level: '0.2'
    service: media_player.volume_set
  - alias: ''
    data:
      entity_id: media_player.homespeakers
      media_content_id: WEB_ADDRESS_OF_DESIRED_ATHAN
      media_content_type: music
    service: media_player.play_media
  - delay: 00:05:00
  - data: {}
    entity_id: media_player.homespeakers
    service: media_player.turn_off
  - data:
      entity_id: media_player.homespeakers
      volume_level: '0.7'
    service: media_player.volume_set
  mode: restart
- id: '1517693010923'
  alias: Adhan
  trigger:
  - platform: template
    value_template: '{{ states.sensor.dhuhr.state == states.sensor.time.state }}'
  - platform: template
    value_template: '{{ states.sensor.asr.state == states.sensor.time.state }}'
  - platform: template
    value_template: '{{ states.sensor.magrib.state == states.sensor.time.state }}'
  - platform: template
    value_template: '{{ states.sensor.isha.state == states.sensor.time.state }}'
  condition: []
  action:
  - data:
      entity_id: media_player.homespeakers
      volume_level: '0.6'
    service: media_player.volume_set
  - alias: ''
    data:
      entity_id: media_player.homespeakers
      media_content_id: WEB_ADDRESS_OF_DESIRED_ATHAN
      media_content_type: music
    service: media_player.play_media
  - delay: 00:05:00
  - data: {}
    entity_id: media_player.homespeakers
    service: media_player.turn_off
  - data:
      entity_id: media_player.homespeakers
      volume_level: '0.7'
    service: media_player.volume_set
  mode: restart
1 Like

Salam brother,

I was having trouble with these value_templates as well. What seems to work well for me is replacing them with

value_template: ‘{{ states.sensor.maghrib_prayer.state == states.sensor.time.state }}’

In your case, your code would be:

id: ‘1597107500790’
alias: adhan
description: adhan
trigger:
platform: template
value_template: ‘{{ states.sensor.fajr_prayer.state == states.sensor.time.state }}’
platform: template
value_template: ‘{{ states.sensor.duhur_prayer.state == states.sensor.time.state }}’
platform: template
value_template: ‘{{ states.sensor.asr_prayer.state == states.sensor.time.state }}’
platform: template
value_template: ‘{{ states.sensor.maghrib_prayer.state == states.sensor.time.state }}’
platform: template
value_template: ‘{{ states.sensor.isha_prayer.state == states.sensor.time.state }}’
condition: []
action:
data: {}
service: script.adhan
mode: single

For this to work consistently, I also made sure to add the time_date sensor to the configuration file to ensure it would work by copying this into the configuration

  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'time_date'

You can see copies of my code below that has been working consistently for me for the past couple of days. Apologies if this doesnt help.

Hi,

Did you had any luck playing Azan with the integration method. Please reply.

Thanks,

Salam,
i had everything working up until the last update (integration of Prayer Times). now i see the sensors however they are never triggered.

here is my config and automation file.

  alias: Athan
  description: ''
  trigger:
  - platform: template
    value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.dhuhr_prayer"), "%Y-%m-%dT%H:%M:%S")) }}'
  condition:
  action:
  - data:
      media_content_id: http://1.2.3.4:8123/local/07_Moluki432210.mp3
      media_content_type: audio/mp3
    entity_id: media_player.family_room_speaker
    service: media_player.play_media
  - data:
      volume_level: '0.5'
    entity_id: media_player.family_room_speaker
    service: media_player.volume_set
homeassistant:
  latitude: X
  longitude: Y
  unit_system: metric
  time_zone: America/<>

# Example configuration.yaml entry
logger:
  default: error
  logs:
    homeassistant.components.yamaha: critical
    custom_components.my_integration: critical
    
# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:
discovery:
media_extractor:
sun:

sensor:
    - platform: time_date
      display_options:
        - 'time'
        - 'date'
        - 'date_time'
        - 'time_date'
        - 'time_utc'
        - 'beat'

islamic_prayer_times:
    calculation_method: isna

binary_sensor:
  - platform: workday
    country: Canada # Change your country if you are not in Canada
    workdays: [fri]
    
# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
#automation: !include automations.yaml
automation: !include_dir_list automation
script: !include scripts.yaml
scene: !include scenes.yaml

i removed the prayer times sensor, and put it back, no change.
sensors:
- fajr
- sunrise
- dhuhr
- asr
- maghrib
- isha
- midnight

The error i keep getting is:

2020-09-02 13:05:01 ERROR (MainThread) [homeassistant.helpers.event] Error while processing state changed for sensor.time_date
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 173, in _async_state_change_dispatcher
    hass.async_run_job(action, event)
  File "/usr/src/homeassistant/homeassistant/core.py", line 384, in async_run_job
    target(*args)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 117, in state_change_listener
    hass.async_run_job(
  File "/usr/src/homeassistant/homeassistant/core.py", line 384, in async_run_job
    target(*args)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 303, in template_condition_listener
    template_result = condition.async_template(hass, template, variables)
  File "/usr/src/homeassistant/homeassistant/helpers/condition.py", line 400, in async_template
    value = value_template.async_render(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 230, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

it is related to the following line, when i remove it i no longer get the error msg but still no trigger of Athan.

- 'time_date'

Please let me know if you have any suggestions.
Thanks

Forgot to say, im able to manually trigger the Athan successfully!

Thank you for this, i have it working in a Pi. I am new to HASS, One thing i do not get it the boxes that show the Adhan under Automation on the dashboard, like shown here: https://youtu.be/XyoQgvedZgo?t=626 , i would like to test the configuration after i make minor changes to it (like volume), how do i do this? right now i have to wait until Adhan time for it play the mp3 so i can hear what it sounds like. any help much appreciated, this is what my dash looks like now: https://i.postimg.cc/WbPXnLm9/dash.jpg

Thanks

alsalam alekoum ,
after recent homeasistant update I got an error

Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected ‘%’) for dictionary value @ data[‘action’][0][‘data’]. Got None. (See /config/configuration.yaml, line 71).

This my current setup works well , but this error stops my auto homeassistant restart
Please let my know if you found any error

  • id: ‘1578755762336’
    alias: Azan notifying
    description: ‘’
    trigger:
    • platform: template
      value_template: ‘{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M,
      %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.fajr_prayer”), “%Y-%m-%dT%H:%M:%S”))
      }}’
    • platform: template
      value_template: ‘{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M,
      %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.dhuhr_prayer”), “%Y-%m-%dT%H:%M:%S”))
      }}’
    • platform: template
      value_template: ‘{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M,
      %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.asr_prayer”), “%Y-%m-%dT%H:%M:%S”))
      }}’
    • platform: template
      value_template: ‘{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M,
      %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.maghrib_prayer”), “%Y-%m-%dT%H:%M:%S”))
      }}’
    • platform: template
      value_template: ‘{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M,
      %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.isha_prayer”), “%Y-%m-%dT%H:%M:%S”))
      }}’

i would like to know if there is a fix for the same issue reported here.
basically day light saving for Summer/Winter time.

Thanks

my adzan and tarhim stop working since hassio new update

It also stoped with me , but now I just stoped my auto 3 am home assistant restart and checked my automation was good , now it working perfectly
With echo Azan also

Salam, I have one problem, I can not off setting my azan can someone hlp me please

value_template: ‘{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M, %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.dhuhr_prayer”), “%Y-%m-%dT%H:%M:%S”))+3600 }}’

It effect only table adhan remaining, but the other table does not ofset azan by 60 minutes, so the azan still playin one hour earlier

did you find already solution for that

Salam,

First, thank you for the wonderful efforts. I’m trying to setup the Adhaan in Singapore. i’ve followed all the instructions and everything seems to be working except the timing. i’ve reached my wits end to resolve this and don’t seem to be getting any headway. Any help would be greatly appreciated.

The time forprayers is off by about 8 hours.

The configuration.yaml is below.

homeassistant:
  latitude: 1.2896700
  longitude: 103.8500700
  elevation: 24
  unit_system: metric
  time_zone: Asia/Singapore
  
default_config:
discovery:
media_extractor:

# Example configuration.yaml using a non-default calculation method
islamic_prayer_times:
    calculation_method: mwl

    
# Sensors
sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'time_date'
  - platform: template
    sensors:
      salah_fajr:
        friendly_name: "Salah Fajr"
        value_template: >
           {%- set a = states("sensor.fajr_prayer").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.fajr_prayer").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}
      salah_dhuhr:
        friendly_name: "Salah Dhuhr"
        value_template: >
           {%- set a = states("sensor.dhuhr_prayer").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.dhuhr_prayer").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}
      salah_asr:
        friendly_name: "Salah Asr"
        value_template: >
           {%- set a = states("sensor.asr_prayer").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.asr_prayer").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}
      salah_maghrib:
        friendly_name: "Salah Maghrib"
        value_template: >
           {%- set a = states("sensor.maghrib_prayer").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.maghrib_prayer").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}
      salah_isha:
        friendly_name: "Salah Isha"
        value_template: >
           {%- set a = states("sensor.isha_prayer").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.isha_prayer").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}
           
# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml 

and the automation.yaml is below.

# Automation for Sehri Dua
- action:
  - alias: 'Sehri Dua'
    data:
      entity_id: media_player.family_room_display
      media_content_id: https://www.thesufi.com/Islamic-Collection/Islamic_Audio_Section/67-Islamic-Masnoon-Dua-Arabic-with-Urdu-Translation-MP3/Dua-and-Supplications--22-.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.family_room_display
      volume_level: '0.6'
    service: media_player.volume_set
  alias: Sehri_Dua
  trigger:
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.fajr_prayer"), "%Y-%m-%dT%H:%M:%S"))-35 }}'

# Automation for Iftari Dua
- action:
  - alias: 'Aftari Dua'
    data:
      entity_id: media_player.family_room_display
      media_content_id: https://www.thesufi.com/Islamic-Collection/Islamic_Audio_Section/67-Islamic-Masnoon-Dua-Arabic-with-Urdu-Translation-MP3/Dua-and-Supplications--23-.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.family_room_display
      volume_level: '0.6'
    service: media_player.volume_set
  alias: Aftari_Dua
  trigger:
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.maghrib_prayer"), "%Y-%m-%dT%H:%M:%S"))-45 }}'


# Automation for  Fajr Azan
- action:
  - alias: 'FajrAzan'
    data:
      entity_id: media_player.family_room_display
      media_content_id: https://archive.org/details/MakkahFajrAdhan6913SheikhAliMullah
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.family_room_display
      volume_level: '0.6'
    service: media_player.volume_set
  alias: Fajr_Adhan
  trigger:
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.fajr_prayer"), "%Y-%m-%dT%H:%M:%S")) }}'

# Automation for 4 times Azan
- action:
  - alias: 'Azan'
    data:
      entity_id: media_player.family_room_display
      media_content_id: http://praytimes.org/audio/adhan/Sunni/Adhan%20Makkah.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.family_room_display
      volume_level: '0.8'
    service: media_player.volume_set
  alias: Adhan
  trigger:
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.dhuhr_prayer"), "%Y-%m-%dT%H:%M:%S")) }}'
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.asr_prayer"), "%Y-%m-%dT%H:%M:%S")) }}'
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.maghrib_prayer"), "%Y-%m-%dT%H:%M:%S")) }}'
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.isha_prayer"), "%Y-%m-%dT%H:%M:%S")) }}'

### Restart HA to force update Islamic Time Sensors at 1AM ###
- alias: Restart HA
  trigger: 
    platform: time
    at: "01:00:00"
  action:
    - service: homeassistant.restart

This my setup, am using Alexa speaker and Spotify, I live in Emirates, Dubai

I hope I could help , like you helped me

Configuration:

islamic_prayer_times:
calculation_method: makkah

sensors:

  • platform: time_date
    display_options:
    • ‘date’
    • ‘time’
    • ‘date_time’
    • ‘time_date’
    • ‘time_utc’
    • ‘beat’

automations

  • id: ‘1578755762336’
    alias: Azan notifying
    description: ‘’
    trigger:
    • platform: template
      value_template: ‘{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M,
      %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.fajr_prayer”),"%Y-%m-%dT%H:%M:%S"))}}’
    • platform: template
      value_template: ‘{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M,
      %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.dhuhr_prayer”), “%Y-%m-%dT%H:%M:%S”))}}’
    • platform: template
      value_template: ‘{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M,
      %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.asr_prayer”), “%Y-%m-%dT%H:%M:%S”))}}’
    • platform: template
      value_template: ‘{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M,
      %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.maghrib_prayer”), “%Y-%m-%dT%H:%M:%S”))}}’
    • platform: template
      value_template: ‘{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M,
      %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.isha_prayer”), “%Y-%m-%dT%H:%M:%S”))}}’
      condition: []
      action:
    • service: media_player.play_media
      data:
      media_content_type: SPOTIFY
      media_content_id: Azan Makah 3
      entity_id: media_player.saud_echo_dot
    • data:
      message: حان وقت الآذان
      title: ({{ now().strftime("%H:%M")}})
      service: notify.all_devices
      mode: single

Alsalam ALykom,
I have a question about prayer time.
can I load the Praying times from csv file or excel file?
u know that some cities use there own timing not the calculated ones
It will realy be very hepful to see it. also I have Alexa not google mini and I would like to have a visual cart of the week time like on lovelace?

Brother, Could you tell me how can I implement the same for Nest Mini? I’m new to this and just got the Home Assistant Installed on Synology NAS.

I live in Toronto, Canada. Could you explain where I have to put these codes and what configuration level changes to do for Toronto Adhaan timing?

Hello,

Please can you explain how you play the adhan on spotify? I have spotify integration on HA, how to you map adhan from spotify to the alexa echo ?

Thanks.

Wa alykoum elsalam
Loading from csv or excel , I have no idea to be honest.
Am from UAE,Dubai but I follow Makkah um alqorr, I would recommend to do some search .

To map your speaker you will have to add Alexa media player integration you will find it in HACS , rest will be same as I posted

Thanks for your quick response.

Yes, I already have alexa media player installed through HACS.

What I’m having difficulty is 1) automation.yaml indentation of your config 2) understanding of how
media_content_type: SPOTIFY and media_content_id: Azan Makkah 3 would make alexa ecoh play Adhan?

below I’ve pasted the code but its having issue with last 5 lines indentation.

please can you help?

- id: '1578755762336'
  alias: Azan notifying
  description: ''
  trigger:
  - platform: template
    value_template: '{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M, %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.fajr_prayer”),"%Y-%m-%dT%H:%M:%S"))}}'
  - platform: template
    value_template: '{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M, %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.dhuhr_prayer”), “%Y-%m-%dT%H:%M:%S”))}}'
  - platform: template
    value_template: '{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M, %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.asr_prayer”), “%Y-%m-%dT%H:%M:%S”))}}'
  - platform: template
    value_template: '{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M, %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.maghrib_prayer”), “%Y-%m-%dT%H:%M:%S”))}}'
  - platform: template
    value_template: '{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M, %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.isha_prayer”), “%Y-%m-%dT%H:%M:%S”))}}'
  condition: []
  action:
   service: media_player.play_media
   data:
    media_content_type: SPOTIFY
    media_content_id: Azan Makah 3
    entity_id: media_player.master_room_echo_dot
 data:
 message: حان وقت الآذان
 title: ({{ now().strftime("%H:%M")}})
 service: notify.all_devices
mode: single