Adhan automation (أذان التلقائي) using Home Assistant and Google Home Mini

Tags: #<Tag:0x00007f7c653b5248> #<Tag:0x00007f7c653b5068>

This guide is written especially for newbies who have just a google-home-mini and are trying to make it call for adhan. A lot of people have already done this and thanks goes to @porankazi for his efforts. However even reading all posts on this forum related to adhan automation, I honestly admit that I remained unable to follow instruction and couldn’t get any success. I watched a lot of tutorials on Home Assistant and made myself familiarize with HassIO platform. By doing so I got to a point where I can install HassIO platform on my Raspberry Pi 3 and automate adhan using my google home mini.

To follow this tutorial, you must have;

  1. Raspberry Pi (3/4)
  2. SD card (8/16 GB)
  3. Access to home router
  4. Any google speaker (google home mini/nest/alexa)

Please follow the steps in order.

  1. Download Home Assistant Platform HassOS image from official website. Download the correct image against your Raspberry Pi model. The downloaded image has an extension .gz which means it is a highly compressed archive. Right-click on it and extract .img from it. To extract it I’d recommend 7zip or WinRar. The end result is you will have an image with .img extension.

  2. Download blena etcher, a program to burn HassIO image on SD card, and install it.

  3. Put your SD card in an SD card reader and connect it to your laptop/PC.

  4. Open blenaEtcher, click on select image, and choose the installation image that you have. Next click on change under the drive icon and select your SD card and click continue. Next click Flash and wait for the process to get completed. Once you get notification Flast Complete, close the blenaEtcher and look for a drive in your PC with name hassos-boot. At this point, windows will ask you to format the SD card. Just cancel/ignore this request. Open hassos-boot drive and make a new folder in it with name CONFIG. Open this folder and create another folder in it with name network. Again open this folder and create a file in it with name my-network. Open this file with notepad and paste the following code in it. After this, save and close the file. This information will help your raspberry-Pi to connect to internet via WiFi. Don’t forget to replace SSID and password in the code below with correct values of your WiFi.

WiFi Settings File

[connection]
id=my-network
uuid=72111c67-4a5d-4d5c-925e-f8ee26efb3c3
type=802-11-wireless

[802-11-wireless]
mode=infrastructure
ssid=NameOfYourWiFi

[802-11-wireless-security]
auth-alg=open
key-mgmt=wpa-psk
psk=YourPassword

[ipv4]
method=auto

[ipv6]
addr-gen-mode=stable-privacy
method=auto

If your google home mini is not already connected to the internet, now set it up before proceeding further.

  1. Remove the SD card from PC, put it in Raspberry-Pi and power-on you raspberry-pi. Wait for a couple of minutes (in my case I waited for 10 minutes) and then visit your router homepage. For example, my router homepage can be accessible at 192.168.0.1. The address of your router may be different. Since every router has a different setup, unfortunately, I can not provide much detail on this. Find the IP address of your Raspberry-Pi on your router homepage and note it somewhere. Then create a port forwarding rule and associate port 8123 with the IP address of your raspberry-Pi.

Further instructions are also available in the video- VIDEO

  1. Now open an internet browser and type the IP address of your raspberry-Pi along with :8123. In my case, it becomes something like http://192.168.0.25:8123. At this point, you will be presented with the home assistant homepage with information that it will be ready in 20 minutes. In my case, it took more than 30 minutes to get ready. The homepage refreshes itself and it will ask you to set your name, username and password. Supply this information and go ahead. In the next step, it will detect your time zone. Just go head as we will set this in configuration.yaml file. At this point, if your Google home mini is already connected to the internet, it will be detected automatically. See my video.

  2. On the home-assistant main page, look on the right-hand sidebar and click on Hass.io. Then click on ADD-ON-STORE. Under official add-ons, look for configurator. Click on it and then install it. After installation, you need to start it by pressing start on the same page. Once it starts open web UI will appear. Click on it and you will be presented with an editor where we can modify configuration files.

  3. On this page, look on the top for a folder icon in the blue bar. Click on it and a list of files will appear. Click on configuration.yaml. Now copy the code below and paste it in the editor on the right-hand side.

WARNING

Don’t forget to change latitude, longitude, elevation and time zone to match your location. To find these values, go to this website and enter your address. Copy your values and repace the corresponding values from the code below. If you don’t know your time zone you can check it on this website

configuration.yaml

homeassistant:
  latitude: -28.003696
  longitude: 153.4236469
  unit_system: metric
  time_zone: Australia/Brisbane
  
default_config:
discovery:
media_extractor:
sun:

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'
  - platform: islamic_prayer_times
    calculation_method: mwl
    sensors:
      - fajr
      - sunrise
      - dhuhr
      - asr
      - maghrib
      - isha
      - midnight

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
  1. Save the file by clicking on the floppy drive icon on the top blue bar and then close it by clicking X icon.

  2. Again click on folder icon and then click on automation.yaml file. Copy and paste the code below in the editor on the right-hand side.
    Caution: In the code below, against entity_id, don’t forget to replace sahar_google_mini with your Google Home Mini ID.

If you don’t remember your google home mini name, just open Google Home app on your android phone (sorry guys I have an android phone and don’t know how to do this on ios) and on the bottom bar, tap on home icon. You will see the device name under your device icon.

In the code below you have to enter your google home mini name in small letters. If the name is more than a one-word name, you have to put underscore after every word. Let say the name of your device is My Home, it should be written as my_home. If it is one-word name only, just type it in small letters.

automation.yaml

You need to replace values at TWO such places in code below.

# Automation for 5 times Azan
- action:
  - alias: 'Azan'
    data:
      entity_id: media_player.sahar_google_mini
      media_content_id: https://www.islamcan.com/audio/adhan/azan2.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.sahar_google_mini
      volume_level: '0.9'
    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.islamic_prayer_time_fajr"), "%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_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")) }}'

### Restart HA to force update Islamic Time Sensors at 1AM ###
- alias: Restart HA
  trigger: 
    platform: time
    at: "01:00:00"
  action:
    - service: homeassistant.restart
  1. Save the file by clicking on the floppy drive icon on the top blue bar and then close it by clicking X icon.
  2. Now on the very left-hand side in the HomeAssistant vertical bar, click on Configuration and on the right-hand side, click on server control. Now under server management, click on restart.
  3. Wait for at least 5 minutes. Close the webpage/home assistant page. Open new browser page, type in your raspberry-Pi IP address along with 8213 as you did previously. HomeAssistant page will appear. Key in your credentials and you will see a dashboard with an automation card.
  4. On this card, you will see Adhan. Click on Adhan and then click on TRIGGER. Wait a minute and if you hear adhan from your google home mini that’s mean everything is successfully setup.

Hurrah !!! You have made it.
Remember me in your prayers. جزاک اللہ

3 Likes

Assalaamualaikum brother. Jazaak Allah Khairan. Alhamdulillah was successfully able to set up everything. May Allah reward you.

1 Like

Brother,
Thank you very much for the code. I got it to work but there is one issue. The sensor has the data built on within it. That means the azan only works for one day. the next day would be say 14th but the sensor is still displaying 13th so it doesn’t work the next day.

Any suggestions?

1 Like

OKay I figured out a way to solve this…
I scheduled a HA restart everyday. The the athan times sensor will reset the dates to match the dates of today.

The sensor data update automatically however it takes a lot time at first. If your configuration and automation files are the same as mine, azan should be heared every day at it’s correct time. I reckon your HA would update the sensor even with out restarting it on daily basis now. Just turn off daily restating and check it’s behaviour again.

Thank you so much for the tutorial. I have a few questions:

  • Is there a way to play multiple files after the adhan?
  • Can i upload the files to hassio to play them locally? if so, how do i state that in configuration that it is local?
  • How can I add offset timing per adhan?
  • Can I adjust volume per google home mini?
  • Also, i just realized, since i’ve got multiple Google mini(s), how do you state multiple speakers to play the adhan

Thank you so much again.

1 Like

Sorry, didn’t get this question.

Yes, you can do it. Make a new folder with name www in config/ and put your desired audio file in mp3 format in it. e.g. The path of this file should be config/www/myAudioFile.mp3
Then in your automation.yaml file change the following line as;

media_content_id: https://www.islamcan.com/audio/adhan/azan2.mp3

to

media_content_id: http://Pi-IP-address:8123/local/myAudioFile.mp3

You can do this by adding time in seconds at the end of a template of your desired azan in automation.yaml file. In the example below, I have offset Fajar azan by 5 minutes.

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")) }}'

to

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")) + 3600}}'

You can adjust the volume by playing around volume_level option in your automation.yaml file.

volume_level: '0.8'

Here 0.8 means 80% of your full volume. You can decrease it to a minimum of 0.1.

Add another entity ID in your automation.yaml file. Let say you have 4 home minis. Add all like shown below. WARNING: Untested

entity_id: media_player.my_home_mini_1

to

entity_id: media_player.my_home_mini_1
entity_id: media_player.my_home_mini_2
entity_id: media_player.my_home_mini_3
entity_id: media_player.my_home_mini_4

So basically right after adhan i would like to play a short du’ah. Do i just add the another data block after the existing one?

Please test it and report it back here whether it works or not? Else i will do when i get back to home.

I didn’t get that far… I am stuck at uploading a file. I was able to create ‘www’ directory under ‘/config’ but when i choose to upload the mp3 file it doesn’t do anything.

Update:
I managed to upload the file using scp. I installed ssh server and then just scp’d the files to /config/www directory.

ok so i tried using the file from www dir., and it didn’t work:
media_content_id: http://hassio.local:8123/local/adhan_fajr_makkah.mp3
seems a bit flaky…

Replace this with the IP address of your raspberry pi. If your www folder is at the correct place, there is no reason for it not to work.

A simple trick to know whether your mp3 file is accessible on your network or not. Paste this url in any browser and see whether you get a notification to download adhan_fajr_makkah.mp3.

Hi @msahar, I need help, please. I have been following guides and so far I have managed to trigger the azan manually. But it seems it is not working via automation. I have Google Home. Home Assistant running on Raspberry Pi through Python3. As I said, the manual trigger works okay. Here are my configuration and automation yaml.

homeassistant:
  # Name of the location where Home Assistant is running
  name: Home
  # Location required to calculate the time the sun rises and sets
  latitude: 22.30691315
  longitude: 113.918213569653
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: 0
  # metric for Metric, imperial for Imperial
  unit_system: metric
  # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  time_zone: Asia/Shanghai
  # Customization file
  #customize: !include customize.yaml
  #whitelist_external_dirs:
   # - '/config/www'
# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:
# Show the introduction message on startup.
#introduction:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

# Discover some devices automatically
discovery:

media_extractor:
sun:
# Sensors
sensor:
  # Weather prediction
  - platform: yr

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

  - platform: islamic_prayer_times
    calculation_method: karachi
    sensors:
       - fajr
       - sunrise
       - dhuhr
       - asr
       - maghrib
       - isha
       - midnight
# Text to speech
tts:
- platform: google_translate
    service_name: google_say

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

I commented
#customize: !include customize.yaml #whitelist_external_dirs # - '/config/www' as I dont know what these do.

My automation is:

- 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_fajr"),
      "%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_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:
  - alias: ''
    data:
      entity_id: media_player.living_room_speaker
      media_content_id: https://www.islamcan.com/audio/adhan/azan2.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
 entity_id: media_player.living_room_speaker
      volume_level: '0.9'
    service: media_player.volume_set

All the aza sensors are listed. The worrying thing I notice (though not sure if it is normal) is that in States the Google Home says:


When I manually trigger, though Google Home will blip and start azan.

Hi @atique I have modified your both configuration and automation file. Please open your configuration.yaml file, delete all entries from it and then paste contents of configuration.yaml file that I have provided.

configuration.yaml

homeassistant:
  latitude: 22.30691315
  longitude: 113.918213569653
  elevation: 0
  unit_system: metric
  time_zone: Asia/Shanghai
  
default_config:
discovery:
media_extractor:
sun:

sensor:
  - platform: yr
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'
  - platform: islamic_prayer_times
    calculation_method: karachi
    sensors:
      - fajr
      - dhuhr
      - asr
      - maghrib
      - isha

# Text to speech
tts:
  - platform: google_translate

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

After this, open your automation.yaml file, delete all entries from it and then paste contents of automation.yaml file that I have provided.

Automation.yaml

# Automation for 5 times Azan
- action:
  - alias: ''
    data:
      entity_id: media_player.living_room_speaker
      media_content_id: https://www.islamcan.com/audio/adhan/azan2.mp3
      media_content_type: audio/mp3
      #media_content_type: audio/youtube
    service: media_extractor.play_media
  - data:
      entity_id: media_player.living_room_speaker
      volume_level: '0.9'
    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_fajr"), "%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_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")) }}'
 

Then restart your raspberry pi. I hope it will work.

Thank you. I will give it a try and see if it works. Later I would like to have separate azan for Fajar to accommodate specific morning additional wordings Inshallah.

From your automation example, example and also from the youtube video I watched, I wonder how this below number is generated/obtained? Or is it any random number?
id: 1517693010922
id: 1517694139112

So for example, if I add separate triggers, how I sequence this number? Thank you.

These are just random numbers. You can modify. I will tell you how to modify automation.yaml for Fajar azan when you confirm about above solution whether working or not.

@msahar, thank you. It worked for Magrib azan correctly - at least for today´s date :slight_smile:. I notice that


remains to today´s date. Is it supposed to change to tomorrow´s date now after having played already or it will auto update on date switching? Hopefully it will work on 14th Nov as well.

Can you please help with Fajar Adhan as a separate trigger? I tried now but it does not list on the card. It just says one trigger. In your youtube video it shows two triggers, Adhan and Fajar Adhan. Thanks in advance.

Replace whatever is in your automation.yaml with the code given below:

# Automation for 4 times Azan
- action:
  - alias: ''
    data:
      entity_id: media_player.living_room_speaker
      media_content_id: https://www.islamcan.com/audio/adhan/azan2.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.living_room_speaker
      volume_level: '0.9'
    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 alarm for Fajar Azan

- action:
  - alias: ''
    data:
      entity_id: media_player.living_room_speaker
      media_content_id: https://www.islamcan.com/audio/adhan/azan3.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.living_room_speaker
      volume_level: '0.9'
    service: media_player.volume_set
  alias: Adhan
  condition: []
  id: '1517693034922'
  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")) }}'