Automate Islamic Adhan (also called Azan or Azzan) or other prayer calls

Tags: #<Tag:0x00007f7c52719938>

This post maybe very specific to certain group of people but wanted to share it anyway. At the very least, you may find this helpful to leverage for other similar scenarios.

In most middle eastern countries, the Adhan can be heard from everyone’s homes. I have elder parents who prays all the time and that is the only home automation they requested from me. Over the past few years I have tried various methods, but nothing worked all the time until I came across the amazing Home Assistant platform and the great community.

A big shout out to OJ7 (https://gist.github.com/OJ7/7f773927d99fe77933e8c7d96a70b6cd ). This was my starting point but I had to make a lot of tweaks to have the perfect automation that has made my parents extremely happy. Please feel free to ask any questions if you are trying to implement this or something similar.

The below setup is not plug and play, you will have to tweak it based on your setup, i hope this gives you a good starting point and guidance if you decide to use it

What This Automation Does: When it is time for the prayer, in the below sequence ->

  • All the media in the house is muted/paused
  • The volume of the Google Home devices is adjusted (super low for early morning ~5am prayer)
  • The Adhan plays 5 times a day at the correct prayer time. All at the same time through all the speakers (synchronized)
  • All the media in the house is un-muted/un-paused to resume previous activity and state

What you will need:

  • Home Assistant Setup (I am using HASSIO v.0.87)
  • At least one Google Home Speaker (I am using 2 Google Mini and 2 Google Home) configured– Couldn’t get it to work with my Alexa devices

High Level Instruction:

Detailed Instructions: I am fairly new to Home Assistant and YAML, please excuse my messy code. There is likely easier and more compressed way to write the automation scripts, modified suggestions would be appreciated. I am just happy I got it to work :wink:

configuration.yaml

homeassistant:
  name: Home
  latitude: !secret lat
  longitude: !secret long
sensor:
     - platform: time_date
        display_options:
        - 'time'
        - 'date'
        - 'date_time'
        - 'time_date'
        - 'time_utc'
        - 'beat'
    - platform: islamic_prayer_times
      calculation_method: makkah
      sensors:
        - fajr
        - sunrise
        - dhuhr
        - asr
        - maghrib
        - isha
        - midnight
#Google Home
cast:
  media_player:
    - host: 10.0.0.36
    - host: 10.0.0.53
    - host: 10.0.0.51
    - host: 10.0.0.50
#Input Boolean - used to synchronize between multiple speakers
input_boolean:  
  adhan:
    name: Adhan
    initial: off 
#Play media from URL - this is another way to play the Adhan from Youtube
media_extractor:

For the below automation, please note that I have a lot of speakers and media devices which are mostly controlled via Harmony Hubs and via Bose SoundTouch component. Please see my comments embedded in the automation script.

automations.yaml

# Automation for Dhuhr, Asr, Maghrib, and Isha Adhan
- id: '1517693010922'
  alias: Adhan
  trigger:
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.islamic_prayer_time_dhuhr"), "%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.islamic_prayer_time_asr"), "%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.islamic_prayer_time_maghrib"), "%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.islamic_prayer_time_isha"), "%Y-%m-%dT%H:%M:%S")) }}'
  condition: []
  action:
  #Basement Media Pause/Mute
  - service: remote.send_command
    data:
      entity_id: remote.basement_remote 
      command:
        - Pause
      device: 17712304
  - service: remote.send_command
    data:
      entity_id: remote.basement_remote 
      command:
        - Pause
      device: 52012635
  - service: media_player.volume_mute
    data:
      entity_id: media_player.home_theater_speaker
      is_volume_muted: true
  - service: media_player.volume_set
    data:
      entity_id: media_player.gym_speaker
      volume_level: 0.0
 #Living Room Media Pause/Mute
  - service: remote.send_command
    data:
      entity_id: remote.living_room_remote 
      command:
        - Pause
      device: 47973307
  - service: remote.send_command
    data:
      entity_id: remote.living_room_remote 
      command:
        - Pause
      device: 48904860
  - service: remote.send_command
    data:
      entity_id: remote.living_room_remote 
      command:
        - Mute
      device: 47973310
  - service: media_player.volume_set
    data:
      entity_id: media_player.kitchen_speaker
      volume_level: 0.0
  - service: media_player.volume_set
    data:
      entity_id: media_player.living_room_speaker
      volume_level: 0.0
  #Bedroom Media Pause/Mute
  - service: remote.send_command
    data:
      entity_id: remote.bedroom_remote 
      command:
        - Pause
      device: 49622856
  - service: remote.send_command
    data:
      entity_id: remote.bedroom_remote 
      command:
        - Pause
      device: 52828759
  - service: remote.send_command
    data:
      entity_id: remote.bedroom_remote 
      command:
        - Mute
      device: 49622863
#Set Desired Volume for Google Home
  - service: media_player.volume_set
    data:
      entity_id: media_player.basement_google 
      volume_level: 0.6
  - service: media_player.volume_set
    data:
      entity_id: media_player.living_room_google
      volume_level: 0.7
  - service: media_player.volume_set
    data:
      entity_id: media_player.green_room_google
      volume_level: 0.6
  - service: media_player.volume_set
    data:
      entity_id: media_player.bedroom_google
      volume_level: 0.5
#Turning on the input boolean to trigger the Google Homes to start playing the Adhan
  - service: input_boolean.turn_on
    entity_id: input_boolean.adhan
#The daly is based on how long the actual Adhan audio is, for this Adhan, it is just around 2 minutes and 14 seconds
  - delay: '00:02:14'
#Basement Media Resume/Un-Mute after Adhan is completed
  - service: remote.send_command
    data:
      entity_id: remote.basement_remote 
      command:
        - Play
      device: 17712304
  - service: remote.send_command
    data:
      entity_id: remote.basement_remote 
      command:
        - Play
      device: 52012635
  - service: media_player.volume_mute
    data:
      entity_id: media_player.home_theater_speaker
      is_volume_muted: false
  - service: media_player.volume_set
    data:
      entity_id: media_player.gym_speaker
      volume_level: 0.55
#Living Room Media Resume/Un-Mute after Adhan is completed
  - service: remote.send_command
    data:
      entity_id: remote.living_room_remote 
      command:
        - Play
      device: 47973307
  - service: remote.send_command
    data:
      entity_id: remote.living_room_remote 
      command:
        - Play
      device: 48904860
  - service: remote.send_command
    data:
      entity_id: remote.living_room_remote 
      command:
        - Mute
      device: 47973310
  - service: media_player.volume_set
    data:
      entity_id: media_player.kitchen_speaker
      volume_level: 0.6
  - service: media_player.volume_set
    data:
      entity_id: media_player.living_room_speaker
      volume_level: 0.7
#Bedroom Media Resume/Un-Mute after Adhan is completed
  - service: remote.send_command
    data:
      entity_id: remote.bedroom_remote 
      command:
        - Play
      device: 49622856
  - service: remote.send_command
    data:
      entity_id: remote.bedroom_remote 
      command:
        - Play
      device: 52828759
  - service: remote.send_command
    data:
      entity_id: remote.bedroom_remote 
      command:
        - VolumeUp
      device: 49622863

#Seperate automation for the Fajr prayer (~5 AM)
- id: '151769301092'
  alias: Adhan Fajr
  trigger:
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.islamic_prayer_time_fajr"), "%Y-%m-%dT%H:%M:%S")) }}'
  condition: []
  action:
  #Set the volume of Google Home Speaker to very low, too early for loud audio :)
  - service: media_player.volume_set
    data:
      entity_id: media_player.basement_google 
      volume_level: 0.3
  - service: media_player.volume_set
    data:
      entity_id: media_player.living_room_google
      volume_level: 0.3
  - service: media_player.volume_set
    data:
      entity_id: media_player.green_room_google
      volume_level: 0.3
  - service: media_player.volume_set
    data:
      entity_id: media_player.bedroom_google
      volume_level: 0.1
  #Turn on the input boolean to trigger playing the Adhan on Google Home Speaker
  - service: input_boolean.turn_on
    entity_id: input_boolean.adhan
#The daly is based on how long the actual Adhan audio is, for this Adhan, it is just around 2 minutes and 14 seconds
  - delay: '00:02:14'
#After the delay, set the volume back to higher level for Google Home devices
  - service: media_player.volume_set
    data:
      entity_id: media_player.basement_google 
      volume_level: 0.6
  - service: media_player.volume_set
    data:
      entity_id: media_player.living_room_google
      volume_level: 0.7
  - service: media_player.volume_set
    data:
      entity_id: media_player.green_room_google
      volume_level: 0.6
  - service: media_player.volume_set
    data:
      entity_id: media_player.bedroom_google
      volume_level: 0.6
  - service: media_extractor.play_media

#Below 4 automation simply plays the Adhan on 4 of my speakers at the same time
- id: '1013100'
  alias: Play Adhan Bedroom
  trigger:
  - entity_id: input_boolean.adhan
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  #Below commented section is an exmaple of how you can use mmedia_extractor component
  # - service: media_extractor.play_media
  #   data:
  #     entity_id: media_player.bedroom_google   # replace with your Google Home's id
  #     media_content_id: https://www.youtube.com/watch?v=ADLO-Hm_nCQ  # replace with your choice of Adhan
  #     media_content_type: audio
  - service: media_player.play_media
    data:
      entity_id: media_player.bedroom_google   # replace with your Google Home's id
      media_content_id: https://kazihome.duckdns.org:8123/local/Adhan1.mp3  # replace with your choice of Adhan
      media_content_type: music
  - delay: '00:00:10'
  - service: input_boolean.turn_off
    entity_id: input_boolean.adhan

- id: '1013101'
  alias: Play Adhan Greeen Room
  trigger:
  - entity_id: input_boolean.adhan
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - service: media_player.play_media
    data:
      entity_id: media_player.green_room_google   # replace with your Google Home's id
      media_content_id: https://kazihome.duckdns.org:8123/local/Adhan1.mp3  # replace with your choice of Adhan
      media_content_type: music
  - delay: '00:00:10'
  - service: input_boolean.turn_off
    entity_id: input_boolean.adhan

- id: '1013101'
  alias: Play Adhan Living Room
  trigger:
  - entity_id: input_boolean.adhan
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - service: media_player.play_media
    data:
      entity_id: media_player.living_room_google   # replace with your Google Home's id
      media_content_id: https://kazihome.duckdns.org:8123/local/Adhan1.mp3  # replace with your choice of Adhan
      media_content_type: music
  - delay: '00:00:10'
  - service: input_boolean.turn_off
    entity_id: input_boolean.adhan

- id: '1013101'
  alias: Play Adhan Basement
  trigger:
  - entity_id: input_boolean.adhan
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - service: media_player.play_media
    data:
      entity_id: media_player.basement_google   # replace with your Google Home's id
      media_content_id: https://kazihome.duckdns.org:8123/local/Adhan1.mp3  # replace with your choice of Adhan
      media_content_type: music
  - delay: '00:00:10'
  - service: input_boolean.turn_off
    entity_id: input_boolean.adhan
16 Likes

Thanks for this buddy. My al fajr clock is tempermental at best. Really appreciate the effort!

Salamualikom,

Thank you brother for taking the time to find a solution for what many of us who are looking for this for a while now. i am using Hassiso 9.0 and i have setting like this but i can get Athan to play,
any help will be good thanks see below my config

# Automation for Dhuhr, Asr, Maghrib, and Isha Adhan
- action:
  - alias: ''
    data:
      entity_id: media_player.living_room   # replace with your Google Home's id
      media_content_id: http://192.168.1.76:8123/local/Adhan1.mp3   # replace with your choice of Adhan
      media_content_type: music
    service: media_extractor.play_media
  - data:
      entity_id: media_player.living_room   # replace with your Google Home's id
      volume_level: '0.7'
    service: media_player.volume_set
  alias: Adhan
  condition: []
  id: '1517693010922'
  trigger:
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.islamic_prayer_time_dhuhr"), "%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.islamic_prayer_time_asr"), "%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.islamic_prayer_time_maghrib"), "%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.islamic_prayer_time_isha"), "%Y-%m-%dT%H:%M:%S")) }}'

# Separate Automation for Fajr Adhan
- action:
  - data:
      entity_id: media_player.living_room   # replace with your Google Home's id
      media_content_id: http://192.168.1.76:8123/local/Adhan2.mp3   # replace with your choice of Fajr Adhan
      media_content_type: music
    service: media_extractor.play_media
  - data:
      entity_id: media_player.living_room   # replace with your Google Home's id
      volume_level: '0.7'
    service: media_player.volume_set
  alias: Fajr Adhan
  condition: []
  id: '1517694139112'
  trigger:
  - platform: template
    value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.islamic_prayer_time_fajr"), "%Y-%m-%dT%H:%M:%S")) }}'Preformatted text

Automate Islamic Adhan-1.yaml (2.1 KB)

# Automation for Dhuhr, Asr, Maghrib, and Isha Adhan
- action:
  - alias: ''
data:
  entity_id: media_player.living_room   # replace with your Google Home's id
  media_content_id: http://192.168.1.76:8123/local/Adhan1.mp3   # replace with your choice of Adhan
  media_content_type: music
service: media_extractor.play_media
  - data:
  entity_id: media_player.living_room   # replace with your Google Home's id
  volume_level: '0.7'
service: media_player.volume_set
  alias: Adhan
  condition: []
  id: '1517693010922'
  trigger:
- platform: template
  value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.islamic_prayer_time_dhuhr"), "%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.islamic_prayer_time_asr"), "%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.islamic_prayer_time_maghrib"), "%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.islamic_prayer_time_isha"), "%Y-%m-%dT%H:%M:%S")) }}'

# Separate Automation for Fajr Adhan
- action:
  - data:
  entity_id: media_player.living_room   # replace with your Google Home's id
  media_content_id: http://192.168.1.76:8123/local/Adhan2.mp3   # replace with your choice of Fajr Adhan
  media_content_type: music
service: media_extractor.play_media
  - data:
  entity_id: media_player.living_room   # replace with your Google Home's id
  volume_level: '0.7'
service: media_player.volume_set
  alias: Fajr Adhan
  condition: []
  id: '1517694139112'
  trigger:
  - platform: template
value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.islamic_prayer_time_fajr"), "%Y-%m-%dT%H:%M:%S")) }}'`Preformatted text`

Hi, can you please share your configuration.yaml file entry? Also do you see the below sensors? Thank you.

thanks bro
i just figure out now every this is working
thanks for good work

Glad you got it to work. You’re welcome.

Salam Aleikum, thank you so much for this.
I’m having trouble getting the athan to fire when i use the template as a condition.
I can manually trigger the media_extractor to play the athan on my google mini, however it’s not firing when i use the template.
Could you please take a look at my setup and see what i’m doing wrong?

- id: '1554439831482'
  alias: Athan_Fajr
  trigger:
  - platform: template
    value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.islamic_prayer_time_fajr"), "%Y-%m-%dT%H:%M:%S")) }}'
  condition: []
  action:
  - data:
      message: Allah Yet2abal <3
      title: Time To Pray Fajr Fam
    service: notify.fam
  - data:
      entity_id: ' media_player.living_room_speaker'
      volume_level: '0.50'
    service: media_player.volume_set
  - data:
      entity_id: media_player.living_room_speaker
      media_content_id: https://www.youtube.com/watch?v=VHcRTIcRvN8
      media_content_type: audio/youtube
    service: media_extractor.play_media

Here's a snapshot of the islamic sensors
![islamic_prayer|690x496](upload://jOqKCFBP4YV2dlY31YxCQAxQuI7.jpeg) 

Here’s my islamic_prayer_times setup in the config file

- platform: islamic_prayer_times
    calculation_method: isna
    sensors:
      - fajr
      - sunrise
      - dhuhr
      - asr
      - maghrib
      - isha
      - midnight

Thanks in advance, i appreciate all the help i can get.

Salam, I am not able to see the screenshot for your state sensors. can you please try uploading it? Also I have stopped using the media extractor component and just downloaded the youtube audio file and uploaded to my www folder. But the below service call worked for me when I used the media extractor. Another question, does the automation trigger at all and the audio doesn’t play? or the automation doesn’t trigger?

  - service: media_extractor.play_media
    data:
      entity_id: media_player.bedroom_google   # replace with your Google Home's id
      media_content_id: https://www.youtube.com/watch?v=ADLO-Hm_nCQ  # replace with your choice of Adhan
      media_content_type: audio

Media extractor works fine. it’s just the automation doesn’t trigger at all when i use the
template trigger type. if i use any type of trigger like the time template for instance, then it will trigger.
Here’s the picture, sorry about that.

Assallam. Thanks for this! Will be putting this in place ASAP.

Your code for trigger seems similar to mines except for one thing. Can you manually trigger the automation to see if it works?

Please note that you may need an extra indent before the word trigger and the dash. Sometimes those simple spaces and tabs make a difference. Please copy my code and do a side by side compares and try again.

Yes i can trigger the automation manually using a different trigger method.
The code looks fine, it was generated using the automation menu on hassio.
I have sensor.time_date configured in my config file. i wonder if it has anything to do with why this trigger is not working.

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

I had to add ‘time_date’ to my setup also.

You are right, I completely forgot that it’s not a default configuration. I’ll update my original post. Thanks.

I wonder if it’s also worth doing a documentation update for the component on the site? Although not a requirement to use it, most use cases will most probably require it.

That was it, the time_date platform options had to be included for this to work… Thank you all very much for your help. waking up to the Athan of this automation at fajr for the first time was priceless. Thanks again!

Asslamu Alaikum brothers,

Thanks all for valuable work, I have followed all instructions but yet can trigger manually but Adnan does not work automatically.

Can someone share their final configuration and automation file details.

Regards,
Bilal

Asslamu Alaikum,

Can you please share your final configuration and automation file details. I am having difficulty to automate these triggers.

Regards,
Bilal