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

Dear Sahar. @msahar Alhamdulihah
Thank you for the time you have put in and for the quick responses.
I like this automation the best. (
…value_template: >
{%- set a = states(“sensor.islamic_prayer_time_fajr”).split(“T”)[1].split(":")[0] -%}…)

got it set up perfectly.
Can you please assist with following:

  1. as how to do time offset in this version
  2. I am sure i can figure our Fajr as a separate action, so will do that
  3. How to implement Hanafi chool calculation (if that’s not possible, I’ll use the offset to estimate)
    Thank you
    Khaleel

@Sodz wa alikum salam,

You can modify this line for Fajr in your automation.yaml file

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

Note there is 3600 in above line, this means a delay of 3600 seconds. You can adjust it accordingly.

I am not sure about this, however you can play around this line in your configuration.yaml file. Change mwl or any other thing to karachi, like below.

calculation_method: karachi

Thank you! will look into it.

I want to schedule morning and evening adhkar eg. morning adhkar at sunrise and evening adhkar 20 minutes before sunet.

what script can i use to automate this?

Got it right. used hybrid of the 2 - original code and the one where the times appear on the overview card.
For now Hanafi asr has no option so added time offest

1 Like

Below is an example of Morning Azkar 10 seconds after sunrise.

################### Morning Azkar ###################
- action:
  - data:
      entity_id: media_player.sahar_google_mini   # replace with your Google Home's id
      media_content_id: https://myhomepi.tk:8123/local/azkaar.mp3   # replace with your choice of Adhan
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.sahar_google_mini   # replace with your Google Home's id
      volume_level: '0.2'
    service: media_player.volume_set
  alias: Evening Azkaar
  trigger:
    - platform: sun
      event: sunrise
      offset: "00:00:10"
      

Can you please post here what you did to make that right so that other people may get benefit from it? JazakAllah.

for the configuration.yaml, I used your supplementary code which included sensors showing the actual time on a card on overview:

herewith:

 # Sensors
sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'time_date'
  - platform: islamic_prayer_times
    calculation_method: karachi
    sensors:
      - fajr
      - dhuhr
      - asr
      - maghrib
      - isha
  - platform: template
    sensors:
      salah_fajr:
        friendly_name: "Salah Fajr"
        value_template: >
           {%- set a = states("sensor.islamic_prayer_time_fajr").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.islamic_prayer_time_fajr").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}
      salah_dhuhr:
        friendly_name: "Salah Dhuhr"
        value_template: >
           {%- set a = states("sensor.islamic_prayer_time_dhuhr").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.islamic_prayer_time_dhuhr").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}
      salah_asr:
        friendly_name: "Salah Asr"
        value_template: >
           {%- set a = states("sensor.islamic_prayer_time_asr").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.islamic_prayer_time_asr").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}
      salah_maghrib:
        friendly_name: "Salah Maghrib"
        value_template: >
           {%- set a = states("sensor.islamic_prayer_time_maghrib").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.islamic_prayer_time_maghrib").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}
      salah_isha:
        friendly_name: "Salah isha"
        value_template: >
           {%- set a = states("sensor.islamic_prayer_time_isha").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.islamic_prayer_time_isha").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}

For the automations.yaml,
I saw that you still had the original sensors in the configuration, so I reverted to you original code:

Herewith the code for the 4 triggers except 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_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"))+3000 }}'
    - 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"))+180 }}'
    - 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")) }}'

I offset Asr by 50min to adjust for HAnafi time. its not exact, but close enough for at least 3 months in my location
I offset Magrib by 3 min because the Magrib sensor seems to actually calculate sunset. I allowed for the usual 3 minutes to allow the sundisk to fully clear the horizon at my altitude

for the Fajr automation, the only reason I separated was to find the appropriate Azaan MP3 file. haven’t yet found one that I like, but herewith the code in the automations.yaml (same as your code):

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

but I found your supplemental code works just as well, but I cannot find a way to offset the time in this code:

trigger:
    - platform: template
      value_template: "{{ states('sensor.salah_fajr') == states('sensor.time') }}"

Hope this helps:
Its all your work though, Thank you

so on raspberry pi u can turn that thing off after all have been set correctly?

No. Anything you installed hassio on should need to be running all the time. Raspberry pi consume less electricity that why many people prefer it. You can buy raspberry pi zero W which only costs few bucks .

Jazakallah khayr

One more request, probably an easy one but I’m new to hassio so getting my head around it.

I want to automate Surah kahf every Friday at 11am etc

Possibly play Surah baqarah after Isha adhan to protect the house

Would appreciate if you can provide code. May Allah increase you and accept your khayr. I’m delighted as been looking into this for so long.

There’s is a USA company called masjidal who have created a system based on adhan playing at home, all cloud based. Worth looking into

Worked great for me. May Allah Bless you with health, wealth and happiness. for those interested i setup hassio on a Ubuntu Server within Docker.

Ubuntu and Docker Guide

Installing Hassio on Ubuntu/Docker Guide

My config

homeassistant:
  latitude: 53.4333542
  longitude: -2.1790891
  unit_system: metric
  time_zone: Europe/London
  
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
````Preformatted text`

My automation

# Automation for 5 times Azan
- action:
  - alias: 'Azan'
    data:
      entity_id: media_player.family_room_speaker
      media_content_id: https://sndup.net/63kw/Azan.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.family_room_speaker
      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

Great work @Atif_Javaid
JazakAllah

Amended automation to take account of Hanafi Asr time calculation (set to an hour offset). I’m am not sure if this has worked yet as it is not time for Asr yet in the UK. I will update if it worked. Also the amended automation has now been amended to play a different Adhaan for Fajr.

# Automation for 4 times Azan
- action:
  - alias: 'Azan'
    data:
      entity_id: media_player.family_room_speaker
      media_content_id: https://sndup.net/63kw/Azan.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.family_room_speaker
      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_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"))+3600 }}'
    - 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")) }}'

- action:
  - alias: 'Azan Fajr'
    data:
      entity_id: media_player.family_room_speaker
      media_content_id: https://sndup.net/7wzz/adhanfajr.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.family_room_speaker
      volume_level: '0.9'
    service: media_player.volume_set
  alias: AdhanFajr
  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")) }}'

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

setting the offset worked great.

Jazakallah Kheir to all for their wonderfull work with the automation of the athan.

I have set the automation to run on a number of Google home mini and google displays.

My question is, isnt the audio suppose to be synced between them all? I mean when the athan starts which is running as an mp3 file from the www there is a nano sec delay between the devices so the are out of syncs. Something I am doing wrong?

@taimur

the automation’s run in sequence. the delay you are seeing is a result of this. i.e. if it takes a nano second between running each automation then the delay between each speaker will be a nano second. you could try adding a delay. for example
device 1 delay 0.3s
device 2 delay 0.2s
device 3 delay 0.3s

i don’t have multiple devices so cannot check this.

How many devices are you running? Please share your automation file.

there is 3 in total, I wanna add more but only if its synced.

# Automation for 5 times Azan
- action:
  - alias: 'Azan'
    data:
      entity_id: media_player.bedroom_speaker
      media_content_id: http://www.taimur.co.uk/software/Athan.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.bedroom_speaker
      volume_level: '0.9'
    service: media_player.volume_set
    data:
      entity_id: media_player.living_room_display
      media_content_id: http://www.taimur.co.uk/software/Athan.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.living_room_display
      volume_level: '0.9'
    service: media_player.volume_set
    data:
      entity_id: media_player.kitchen_speaker
      media_content_id: http://www.taimur.co.uk/software/Athan.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.kitchen_speaker
      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