Automate Islamic Adhan (also called Azan or Azzan) or other prayer calls - Updated 3_24_2023 for Node-RED

Those are my configurarions:
Automation_adan.yaml


  alias: Salah nuevo
  initial_state: "on"
  trigger:

    - platform: template
      value_template: "{{ is_state('sensor.salah_fajr', states('sensor.time')) }}"
    - platform: template
      value_template: "{{ is_state('sensor.salah_dhuhr', states('sensor.time')) }}"
    - platform: template
      value_template: "{{ is_state('sensor.salah_asr', states('sensor.time')) }}"
    - platform: template
      value_template: "{{ is_state('sensor.salah_maghrib', states('sensor.time')) }}"
    - platform: template
      value_template: "{{ is_state('sensor.salah_isha', states('sensor.time')) }}"

  action:
    - service: shell_command.tts_local
      data_template:
        message: >
           "{{states('sensor.zara_greeting')}}, Esa es la Notificacíon de Salah al {{ states('sensor.salah_actual')}} , Sera mejor levantarse a REZAR!."

           
    - delay:
        seconds: 0.125
           
    - service: shell_command.adan_chooser
      data_template:
        direccion: "/home/pi/sounds/azan/azan{{'{0:d}'.format(range(1, 8)|random)}}.wav"

and my sensors.yaml

  - platform: islamic_prayer_times
    calculation_method: isna
    sensors:
      - fajr
      - sunrise
      - dhuhr
      - asr
      - maghrib
      - isha
      - midnight
      
  - platform: template
    sensors:
      salah_fajr:
        friendly_name: Sensor Salah Fajr
        value_template: "{{ (states('sensor.islamic_prayer_time_fajr').split('T')[1].split(':')[0]) + ':' + (states('sensor.islamic_prayer_time_fajr').split('T')[1].split(':')[1])  }}"

      salah_dhuhr:
        friendly_name: Sensor Salah Dhuhr
        value_template: "{{ (states('sensor.islamic_prayer_time_dhuhr').split('T')[1].split(':')[0]) +':'+ (states('sensor.islamic_prayer_time_dhuhr').split('T')[1].split(':')[1])}}"

          
      salah_asr:
        friendly_name: Sensor Salah Asr
        value_template: "{{ (states('sensor.islamic_prayer_time_asr').split('T')[1].split(':')[0]) +':'+ (states('sensor.islamic_prayer_time_asr').split('T')[1].split(':')[1]) }}"

      salah_maghrib:
        friendly_name: Sensor Salah Maghrib
        value_template: "{{ (states('sensor.islamic_prayer_time_maghrib').split('T')[1].split(':')[0]) +':'+ (states('sensor.islamic_prayer_time_maghrib').split('T')[1].split(':')[1]) }}"

      salah_isha:
        friendly_name: Sensor Salah isha
        value_template: "{{ (states('sensor.islamic_prayer_time_isha').split('T')[1].split(':')[0]) +':'+ (states('sensor.islamic_prayer_time_isha').split('T')[1].split(':')[1]) }}"

      salah_actual:
        friendly_name: "Salah Actual"
        value_template: >
           "{%- if is_state('sensor.salah_fajr', states('sensor.time')) -%}
             Fajr
           {%- elif is_state('sensor.salah_dhuhr', states('sensor.time')) -%}
             Dhuhr
           {%- elif is_state('sensor.salah_asr', states('sensor.time')) -%}
             Asr
           {%- elif is_state('sensor.salah_maghrib', states('sensor.time')) -%}
             Maghrib
           {%- elif is_state('sensor.salah_isha', states('sensor.time')) -%}
             Isha
           {%- endif -%}"

if you are using media player to play mp3 files you can change my shell_command service by the media player, here is an example of service media player:

  action:
    service: media_player.play_media
    data:
      entity_id: "media_player.google_video_cast"
      media_content_id: "https://xxx.xxx.xxx.xxx:8123/local/media/adan1.mp3"
      media_content_type: "music"

if you use local speaker on your raspberry then you can use the shell command:


  adan_chooser: /usr/bin/aplay -q "{{direccion}}"
  

on my case i have like 7 mp3 files and the system will choose randomly from the folder in the automation.
I hope this helps

1 Like

This is one of the most useful things ive seen for a while

1 Like

I am having trouble having the audio play when I trigger all I get is the sound of the Google Home turning on but no audio.

the Alan I am looking to have is the one that I have in the file its a youtube link I do not know how to save it locally or anything like that can someone please help thank you

Automation 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:

#Set Desired Volume for Google Home

  • service: media_player.volume_set
    data:
    entity_id: media_player.dining_room_speaker
    volume_level: 0.6

#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:00:14’

#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 :slight_smile:
    • service: media_player.volume_set
      data:
      entity_id: media_player.dining_room_speaker
      volume_level: 0.3

    #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:5: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.dining_room_speaker
      volume_level: 0.6

#Below 4 automation simply plays the Adhan on 4 of my speakers at the same time

  • id: ‘1013101’
    alias: Play Adhan Dining 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.dining_room_speaker # replace with your Google Home’s id
      media_content_id: https://www.youtube.com/watch?v=G96FEkkFCzg # replace with your choice of Adhan
      media_content_type: video
    • delay: ‘00:00:10’
    • service: input_boolean.turn_off
      entity_id: input_boolean.adhan

You need to use media extractor to play the youtube version of the Athan.
first add media extractor anywhere in your configuration.yaml file like this:
media_extractor:

Next use media extractor to play the audio of that youtube video, here’s my automation.

- 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
    service: notify.tg
  - data:
      entity_id: ' media_player.living_room_speaker'
      volume_level: '0.90'
    service: media_player.volume_set
  - data:
      entity_id: media_player.living_room_speaker
      media_content_id: https://www.youtube.com/watch?v=ADLO-Hm_nCQ
      media_content_type: audio/youtube
    service: media_extractor.play_media

you can ignore the first action which is just a notification i send to our phones using telegram.

I hope this hleps.

Just for any passers by. Make sure you have the right timezone in your configuration file. I’m in London and i had utc set as my time zone so this wasnt working as expected.

1 Like

Hello Aadighal, I’ve been struggling since few days to get the automation , I can manually trigger it and the Adhan plays. Possible to share your config and automation files? any help would be appreciated.

S.A Bilal, sure thing, here’s what I currently have.

Config:

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

Automation:
Fajr Prayer

- 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:
      entity_id: ' media_player.living_room_speaker'
      volume_level: '0.90'
    service: media_player.volume_set
  - data:
      entity_id: media_player.living_room_speaker
      media_content_id: https://www.youtube.com/watch?v=ADLO-Hm_nCQ
      media_content_type: audio/youtube
    service: media_extractor.play_media

Athan for the rest of prayers:

- id: '1554438581518'
  alias: Athan_4
  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_isha"),
      "%Y-%m-%dT%H:%M:%S")) }}'
  condition: []
  action:
  - data:
      message: Allah Yet2abal <3
      title: Time for Prayer
    service: notify.tg
  - 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=ADLO-Hm_nCQ
      media_content_type: audio/youtube
    service: media_extractor.play_media

Unfortunately you can’t enter a value template in the Automation editor GUI, to go around that you could create an automation and use a template trigger but leave the value template empty and fill out the rest. Then go to your automation.yaml file and add the value template there. Here’s a picture of the my automation in the Automation editor.

Thank you. I appreciate your offer. Please give us a detailed tutorial. May Allah reward all of you for making our homes filled with the voice of Azaan.

1 Like

I have written a detailed guide. Check it out. GUIDE. The video link is also available in this guide too.

Salam,

I am not able to workout this automation on the bose hs 500.
Can you please help?

Hi, Salam, where do I place this?

Salams, how do I get 12hr format instead of 24?

Al salam Alikum
this will make it Hr 12 am/pm

- platform: template
    sensors:
      salah_fajir:
        friendly_name: pray_fajir:
        value_template: "{{ as_timestamp(states('sensor.islamic_prayer_time_fajr')) | timestamp_custom('%-I:%M %P') }}"

you can create it as sensor for each Salah to display, try it in you template editor and it will display the time to pray,

1 Like

I have a question not exactly related to adhan automation but has a similar automation

Does any one know if its possible to use the media player component to play Quran for the kids at a set time and repeat for 2 hours.

As my daughter is learning a new surah, I want to be able to play surah on repeat for 2 hours then stop

I have followed the similar automation as adhan but I only know how to assign 1 mp3 file (like the adhan). I want it to repeat playing for a set time

1 Like

How can i play multiple files:
so basically after adhan i would like to play couple du’ah

Hi, you can add them to folder and then play all the mp3 files either *.mp3 or you can use Delay: (time for file to end playing) then play the next file and so on, this way you will have a playing order.
This is an example to play random files

    - service: shell_command.bienvenida
      data_template:
        tocar: "/zara/music/saludos/saludos{{ '{0:d}'.format(range(1, 15)|random) }}.mp3"

or you can do it this way:

    - service: shell_command.bienvenida
      data_template:
        tocar: "/zara/music/saludos/saludos1.mp3"
    - delay: '00:01:55' # this is the time it takes to finish playing the saludos1.mp3 file.
    - service: shell_command.bienvenida
      data_template:
        tocar: "/zara/music/saludos/saludos2.mp3"

Shell command:

bienvenida: /usr/bin/mpg123 -a hw:0,0 -q "{{tocar}}"

The “hw:0,0” is the audio hardware if you are using the raspberry and have speakers attached to the raspberry.

# Seperate Automation for Fajr Adhan
- action:
  - alias: ''
    data:
      entity_id: media_player.hall_speaker
      media_content_id: http://hassio.local:8123/local/adhan_fajr_makkah.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.hall_speaker
      volume_level: '0.7'
    service: media_player.volume_set
  alias: Fajr 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")) +960 }}'

where would I put the delay and run the other file in this?

# Seperate Automation for Fajr Adhan
- alias: Fajr Adhan
  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")) +960 }}'
  action:
    - service: media_player.volume_set
      data:
        entity_id: media_player.hall_speaker
        volume_level: '0.7'
    - service: media_extractor.play_media
      data:
        entity_id: media_player.hall_speaker
        media_content_id: http://hassio.local:8123/local/adhan_fajr_makkah.mp3
        media_content_type: audio/mp3
    - delay: '00:02:05' # time it takes to finish playing your adhan_fajr_makkah.mp3
    - service: media_extractor.play_media
      data:
        entity_id: media_player.hall_speaker
        media_content_id: http://hassio.local:8123/local/adhan_duaa.mp3
        media_content_type: audio/mp3        

i have just rearrange your automation and added delay and “another adhan_duaa.mp3” file as another mp3 file to play.
hope it works.
mjust make sure that the time on the delay its grater than the time your adhan fajir takes by lets say 3 seconds.

Im going to create a test block : is there a way to get time like it does already and then to trigger a specific time to run that test block?

currently:
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")) +960 }}'

what im thinking, not sure if this is correct:
value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == 18:30:00, "%H:%M:%S"))}}'

So i created a test block: which doesn’t give me error but doesn’t do much: im assuming the issue is the way i have entered a specific time under value_template…

- alias: TEST BLOCK
  id: '1517693010922'
  trigger:
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp("18:15:00", "%H:%M:%S") }}' #so the current time from time_date is 18:10
  action:
    - service: media_player.volume_set
      data:
        entity_id: media_player.office_speaker
        volume_level: '0.7'
    - service: media_extractor.play_media
      data:
        entity_id: media_player.office_speaker
        media_content_id: http://hassio.local:8123/local/adhan_fajr_makkah.mp3
        media_content_type: audio/mp3
    - delay: '00:03:50' # time it takes to finish playing your adhan_fajr_makkah.mp3
    - service: media_extractor.play_media
      data:
        entity_id: media_player.office_speaker
        media_content_id: http://hassio.local:8123/local/dua-e-adhan.mp3
        media_content_type: audio/mp3