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

Author is actively editing this guide. If you find error, plz report it.

Last Update: 2024-10-21T19:00:00Z

This guide assumes that you already have.

  • A running instance of Home Assistant and accessible at: http://homeassistant.local:8123
  • Any google speaker (Google Home Mini/Nest)

Follow the following steps in same order.

  1. Go to :gear: Settings tab in left side menu.

  2. Click on :control_knobs: Devices & Services.

  3. Click on :heavy_plus_sign: ADD INTEGRATION.

  4. Search for Islamic Prayer Times and install it. Configure it and provide your coordinates (latitude and longitude) correctly. You will see an integration card like this:

image

  1. Click on this integration card, and then configure it.
  1. Choose options as you please and then click SUBMIT button.
  1. Then using :wrench: file editor edit your configuration.yaml file and paste the following code at the end and save :floppy_disk: configuration.yaml file.
sensor:
  - platform: template
    sensors:
      salah_fajr:
        friendly_name: "Fajr (with offset)"
        value_template: "{{ (as_timestamp(states('sensor.islamic_prayer_times_fajr_prayer')) + 1140) | timestamp_custom('%Y-%m-%dT%H:%M:%S+00:00', false) }}"
        device_class: timestamp
      salah_dhuhr:
        friendly_name: "Dhuhr (with offset)"
        value_template: "{{ (as_timestamp(states('sensor.islamic_prayer_times_dhuhr_prayer')) + 3840) | timestamp_custom('%Y-%m-%dT%H:%M:%S+00:00', false) }}"
        device_class: timestamp
      salah_asr:
        friendly_name: "Asr (with offset)"
        value_template: "{{ (as_timestamp(states('sensor.islamic_prayer_times_asr_prayer')) + 60) | timestamp_custom('%Y-%m-%dT%H:%M:%S+00:00', false) }}"
        device_class: timestamp
      salah_maghrib:
        friendly_name: "Maghrib (with offset)"
        value_template: "{{ (as_timestamp(states('sensor.islamic_prayer_times_maghrib_prayer')) + 1) | timestamp_custom('%Y-%m-%dT%H:%M:%S+00:00', false) }}"
        device_class: timestamp
      salah_isha:
        friendly_name: "Isha (with offset)"
        value_template: "{{ (as_timestamp(states('sensor.islamic_prayer_times_isha_prayer')) + 300) | timestamp_custom('%Y-%m-%dT%H:%M:%S+00:00', false) }}"
        device_class: timestamp
  1. Then edit your automation.yaml file and paste the following code, and then save your :floppy_disk: automation.yaml file.
- id: '1726512717238'
  alias: Four Azans Automation
  description: ''
  trigger:
  - platform: time
    at: sensor.salah_dhuhr
  - platform: time
    at: sensor.salah_asr
  - platform: time
    at: sensor.salah_maghrib
  - platform: time
    at: sensor.salah_isha
  condition: []
  action:
  - action: media_player.play_media
    target:
      entity_id: media_player.google_home_mini
    data:
      media_content_id: https://www.islamcan.com/audio/adhan/azan1.mp3
      media_content_type: audio/mpeg
    metadata:
      title: Adhan.mp3
      thumbnail:
      media_class: music
      children_media_class:
      navigateIds:
      - {}
      - media_content_type: app
        media_content_id: media-source://media_source
  mode: single
- id: '1726513324106'
  alias: Fajr Azan Automation
  description: ''
  trigger:
  - platform: time
    at: sensor.salah_fajr
  condition: []
  action:
  - action: media_player.play_media
    target:
      entity_id: media_player.google_home_mini
    data:
      media_content_id: https://download.tvquran.com/download/TvQuran.com__Athan/TvQuran.com__01.athan.mp3
      media_content_type: audio/mpeg
    metadata:
      title: FajrAdhan.mp3
      thumbnail:
      media_class: music
      children_media_class:
      navigateIds:
      - {}
      - media_content_type: app
        media_content_id: media-source://media_source
  mode: single
  1. :repeat: Restart Home Assistant.

  2. Then go to your DASHBOARD and EDIT DASHBOARD.

  3. Click Add Card, and choose an Entities Card.

image
image


  1. Then configure Entities Card and click on SHOW CODE EDITOR.
  1. Remove all the entries from the card.

image

  1. Paste the following code here.
type: entities
entities:
  - entity: sensor.salah_fajr
    format: time
    name: Fajr
  - entity: sensor.salah_dhuhr
    format: time
    name: Dhuhr
  - entity: sensor.salah_asr
    format: time
    name: Asr
  - entity: sensor.salah_maghrib
    format: time
    name: Maghrib
  - entity: sensor.salah_isha
    format: time
    name: Isha
title: Prayer Times

15. Done. انشاءاللہ you will hear Adhan at its specified time.

Caution:

In automation.yaml file, do not forget to change entity_id, matching with your Google Home Mini/Nest/Speaker ID.

Last Updated: 2024-10-22T17:24:00Z

13 Likes

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

2 Likes

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
1 Like

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.

1 Like

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