Azan or Adhan automation Islamic Prayer Times

Dear All,
Since I have started working with Home Assistant, I started with a small project to automate the Azan (Muslim Prayer Call) with my Nest Mini. I have done my research and first tried to run the configuration available on the forum. Unfortunately it did not worked for me at all. Then in the discord group someone told me to add integration and then automate. I did that but still it is not running.
Whenever I reload the integration it start playing the Azan but it is not as per the time it’s like randomly I tried to reload the integration and it started playing the azan. I request if anyone can help. I will be grateful.
The automation code I found on the forum is as follows:

- id: '2020042901'
  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.fajr_prayer"), "%Y-%m-%dT%H:%M:%S"))
      }}'
  condition: []
  action:
  - data:
      volume_level: 0.3
    entity_id: media_player.living_room_speaker
    service: media_player.volume_set
    target:
      entity_id: 
      - media_player.living_room_speaker
  - service: media_player.play_media
    data:
      entity_id: media_player.living_room_speaker
      media_content_id: http://x.x.x.x:8123/local/Mishary_Rashid_Fajr.mp4
      media_content_type: audio/mp4
  - delay: 00:00:00
- id: '2020042902'
  alias: Adhan Dhuhr and Asr
  trigger:
  - platform: template
    value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M,
      %Y-%m-%d")) == as_timestamp(strptime(states("sensor.dhuhr_prayer"), "%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.asr_prayer"), "%Y-%m-%dT%H:%M:%S"))
      }}'
  condition: []
  action:
  - data:
      volume_level: 0.4
    service: media_player.volume_set
    target:
      entity_id: media_player.living_room_speaker
  - data:
      media_content_id: http://x.x.x.x:8123/local/Mishary_Rashid_Azan.mp4
      media_content_type: video/mp4
    service: media_player.play_media
    target:
      entity_id: media_player.living_room_speaker
  - delay: 00:00:00
  mode: single
- id: '151769301093'
  alias: Adhan Maghrib and Isha
  trigger:
  - platform: template
    value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M,
      %Y-%m-%d")) == as_timestamp(strptime(states("sensor.maghrib_prayer"), "%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.isha_prayer"), "%Y-%m-%dT%H:%M:%S"))-1800
      }}'
  condition: []
  action:
  - data:
      volume_level: 0.4
    service: media_player.volume_set
    target:
      entity_id: media_player.living_room_speaker
      device_id: b19748c5c11121ca67ade94f9ce4ea12
  - data:
      media_content_id: http://x.x.x.x:8123/local/Mishary_Rashid_Azan.mp4
      media_content_type: video/mp4
    service: media_player.play_media
    target:
      entity_id: media_player.living_room_speaker
      device_id: b19748c5c11121ca67ade94f9ce4ea12
  - delay: 00:00:00
  mode: single

This Automation works manually but its automation it should work automatically :grinning_face_with_smiling_eyes: please help if someone can.

Big thank you.

1 Like

That’s a needlessly complicated Template Trigger.

You can create a single automation that handles all five prayer times using a Time Trigger.

- alias: Call to prayer example
  trigger:
  - platform: time
    at: 
    - sensor.fajr_prayer
    - sensor.dhuhr_prayer
    - sensor.asr_prayer
    - sensor.maghrib_prayer
    - sensor.isha_prayer
  action:
  ... etc ...
1 Like

Automations have a fine debugging tool now.

1 Like

Thank you Taras, at least someone answered :), appreciate that. I am new to this and learning…please would that be fine:
Automation:


# AZAN TIMES
- id: '2021061301'
- alias: Azan Fajr
  trigger:
  - platform: time
    at:
	- sensor.fajr_prayer
  action: 
  - data:
      volume_level: 0.3
    entity_id: media_player.living_room_speaker
    service: media_player.volume_set
    target:
      entity_id: 
      - media_player.living_room_speaker
  - service: media_player.play_media
    data:
      entity_id: media_player.living_room_speaker
      media_content_id: http://10.0.110.111:8123/local/Mishary_Rashid_Fajr.mp4
      media_content_type: audio/mp4
  - delay: 00:00:00

- id: '2021061302'
- alias: Azan
  trigger:
  - platform: time
    at:
	- sensor.dhuhr_prayer
    - sensor.asr_prayer
    - sensor.maghrib_prayer
    - sensor.isha_prayer
  action: 
  - data:
	  volume_level: 0.3
	entity_id: media_player.living_room_speaker
	service: media_player.volume_set
	target:
	  entity_id: 
	  - media_player.living_room_speaker
  - service: media_player.play_media
	data:
	  entity_id: media_player.living_room_speaker
	  media_content_id: http://10.0.110.111:8123/local/Mishary_Rashid_Azan.mp4
	  media_content_type: audio/mp4
  - delay: 00:00:00

It can be done with a single automation:

# AZAN TIMES
- id: '2021061302'
  alias: Azan
  trigger:
  - platform: time
    at:
    - sensor.fajr_prayer
    - sensor.dhuhr_prayer
    - sensor.asr_prayer
    - sensor.maghrib_prayer
    - sensor.isha_prayer
  action:
  - service: media_player.volume_set
    target:
      entity_id: media_player.living_room_speaker
    data:
      volume_level: 0.3
  - service: media_player.play_media
    target:
      entity_id: media_player.living_room_speaker
    data:
      media_content_id: "http://10.0.110.111:8123/local/Mishary_Rashid_{{'Fajr' if trigger.to_state.object_id == 'fajr_prayer' else 'Azan'}}.mp4"
      media_content_type: audio/mp4
2 Likes

Big thank you, it was Asr time and the Azam played itself alhumdulillah. Thank you so so much.

This is what I got now, would appreciate if you have another look, thank for your time:


- id: '2020042901'
  alias: Adhan Fajr
  trigger:
  - platform: time
    at:
    - sensor.fajr_prayer
    - sensor.dhuhr_prayer
    - sensor.asr_prayer
    - sensor.maghrib_prayer
    - sensor.isha_prayer
  
  action:
  - service: media_player.volume_set
    target:
      entity_id: media_player.living_room_speaker
    data:
      volume_level: 0.5
  - service: media_player.play_media
    target:
    entity_id: media_player.living_room_speaker
    data:
      media_content_id: http://x.x.x.x:8123/local/Mishary_Rashid_{{'Fajr' if trigger.to_state.object_id == 'fajr_prayer' else 'Azan'}}.mp4
      media_content_type: audio/mp4

Thank you so much once again.

Fix the indentation of the second entity_id. Shift it to the right by two spaces.

1 Like

Thank you so much again. Done that.

Hi @wsb now you fixed can you help me doing same thing. I tried different approach that others suggested for some reason the prayer times i have are UTC. can you please your configuration i would like to replicate. Thanks

Hi om3r, sorry I was away couldn’t provide you the code earlier, please put your own IP address:
e.g:
media_content_id: http://x.x.x.x:8123/local/Mishary_Rashid_Azan.mp4

# FAJR PRAYER CALCULATION METHOD | HANFI
- id: '2020042901'
  alias: AZAN FAJR
  trigger:
  - platform: time
    at:
    - sensor.fajr_prayer
  action:
  - data:
      volume_level: 0.4
    service: media_player.volume_set
    target:
      entity_id: media_player.living_room_speaker
  - data:
      media_content_id: http://x.x.x.x:8123/local/Mishary_Rashid_Fajr.mp4
      media_content_type: video/mp4
    service: media_player.play_media
    target:
      entity_id: media_player.living_room_speaker
  - delay: 00:00:00
  mode: single
# DHUHR | MAGRIB | ISHA PRAYER CALCULATION METHOD | HANFI
- id: '2020042902'
  alias: AZAN
  trigger:
  - platform: time
    at:
    - sensor.dhuhr_prayer
    - sensor.maghrib_prayer
    - sensor.isha_prayer
  action:
  - data:
      volume_level: 0.5
    service: media_player.volume_set
    target:
      entity_id: media_player.living_room_speaker
  - data:
      media_content_id: http://x.x.x.x:8123/local/Mishary_Rashid_Azan.mp4
      media_content_type: video/mp4
    service: media_player.play_media
    target:
      entity_id: media_player.living_room_speaker
  - delay: "00:00:00"
  mode: single
# ASR PRAYER CALCULATION METHOD | HANFI  
- id: '2020042903'
  alias: AZAN ASR
  trigger:
  - platform: time
    at:
    - sensor.asr_prayer
  action:
  - data:
      volume_level: 0.5
    service: media_player.volume_set
    target:
      entity_id: media_player.living_room_speaker
  - data:
      media_content_id: http://x.x.x.x:8123/local/Mishary_Rashid_Azan.mp4
      media_content_type: video/mp4
    service: media_player.play_media
    target:
      entity_id: media_player.living_room_speaker
  - delay: "00:48:00"
  mode: single

Hope that will help, remember us in your prayers.
For timing on the card edit the home and click show code editor
the paste the below code before the title

cards:
  - entities:
      - entity: automation.adhan_fajr_2
      - entity: automation.adhan_dhuhr_and_asr
      - entity: automation.azan_asr
      - entity: sensor.sunrise_time
        format: time
      - entity: sensor.fajr_prayer
        format: time
      - entity: sensor.dhuhr_prayer
        format: time
      - entity: sensor.asr_prayer
        format: time
      - entity: sensor.maghrib_prayer
        format: time
      - entity: sensor.isha_prayer
        format: time

I’m using the “Islamic Prayer Times” integration with the Google mini just fine. I have a question about the dashboard though. I have added the azan timing sensors to the dashboard but it shows the remaining or elapsed time for each azan. e.g. here
Is there any way to show the the exact time of each azan in lovelace?
Thanks

Hello I am trying make automation same like in single all prayer
But getting error .find below my code

alias: Prayer Time New
trigger:
  - platform: time
    at:
      - sensor.fajr_prayer
      - sensor.dhuhr_prayer
      - sensor.asr_prayer
      - sensor.maghrib_prayer
      - sensor.isha_prayer
condition: []
action:
  - service: media_player.media_stop
    target:
      entity_id: media_player.ytube_music_player
    data: {}
  - service: media_player.volume_set
    data:
      volume_level: 0.5
    target:
      entity_id:
        - media_player.bedroom_speaker
  - data:
      media_content_id: >-
        /local/Prayer_{{'Fajr' if trigger.to_state.object_id == 'fajr_prayer'
        else 'Azan'}}.mp3
      media_content_type: audio/mp3
    service: media_player.play_media
    target:
      entity_id:
        - media_player.bedroom_speaker
  - type: turn_on
    device_id: aea2a8c949351712c46de6ed0dcd31c5
    entity_id: switch.table_lamp_switch_1
    domain: switch
  - service: notify.mobile_app_my_oppo
    data:
      message: Prayer Time
      title: Prayer Time
      data:
        image: /media/local/Images/Rasulullah.jpeg
mode: single

this is the errors

Logger: homeassistant.helpers.template
Source: helpers/template.py:1834
First occurred: 9:34:09 AM (1 occurrences)
Last logged: 9:34:09 AM

Template variable error: 'dict object' has no attribute 'to_state' when rendering '/local/Prayer_{{'Fajr' if trigger.to_state.object_id == 'fajr_prayer' else 'Azan'}}.mp3'
Logger: homeassistant.components.automation.prayer_time_new
Source: components/automation/__init__.py:514
Integration: Automation ([documentation](https://www.home-assistant.io/integrations/automation), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+automation%22))
First occurred: 9:34:09 AM (1 occurrences)
Last logged: 9:34:09 AM

Error while executing automation automation.prayer_time_new: Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'

May i know how an i fix this erros ,
i hope some one will help me to fix this error

The cause of the problem is identified here:

Template variable error: 'dict object' has no attribute 'to_state'
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^

Your automation uses a Time Trigger. When it triggers, the trigger variable is created but a Time Trigger does not supply it with a to_state. This is shown in the documentation here.

I suggest you do the following:

alias: Prayer Time New
trigger:
  - id: 'Fajr'
    platform: time
    at: sensor.fajr_prayer
  - id: 'Azan'
    platform: time
    at:
      - sensor.dhuhr_prayer
      - sensor.asr_prayer
      - sensor.maghrib_prayer
      - sensor.isha_prayer
condition: []
action:
  - service: media_player.media_stop
    target:
      entity_id: media_player.ytube_music_player
    data: {}
  - service: media_player.volume_set
    target:
      entity_id: media_player.bedroom_speaker
    data:
      volume_level: 0.5
  - service: media_player.play_media
    target:
      entity_id:  media_player.bedroom_speaker
    data:
      media_content_id: '/local/Prayer_{{ trigger.id }}.mp3'
      media_content_type: audio/mp3
  - service: switch.turn_on
    target:
      entity_id: switch.table_lamp_switch_1
  - service: notify.mobile_app_my_oppo
    data:
      message: Prayer Time
      title: Prayer Time
      data:
        image: /media/local/Images/Rasulullah.jpeg
mode: single
1 Like

Thank you Dear
It working for me .but I have tried other method that also working .

media_content_id: >-
        /local/Prayer_{{'Fajr' if trigger.entity_id == 'sensor.fajr_prayer'
        else 'Azan'}}.mp3

I used trigger.entity_id

That will certainly work as well. The main difference is that what I proposed doesn’t have to perform a comparison. The correct value is automatically provided via trigger.id.

thanks,

how about if i want to switch off the AC 30mins before sensor.fajr prayer

Hello Taras, how could i play the audio 30 minutes before each sensor time
i have this template below didnt work with auto anymore

{{ as_timestamp(strptime(states(“sensor.time_date”), “%H:%M, %Y-%m-%d”)) == as_timestamp(strptime(states(“sensor.maghrib_prayer”), “%Y-%m-%dT%H:%M:%S”)) - 1800 }}

thank you for help

If used in a Template trigger, this will trigger 30 minutes before maghrib_prayer.

{{ now().timestamp() | int(0)  == states('sensor.maghrib_prayer') | as_timestamp - 1800 }}

If you want a Template Trigger that triggers 30 minutes before any of the five prayer times, you can use this:

{{ now().timestamp() | int(0) + 1800 in expand('sensor.fajr_prayer', 'sensor.dhuhr_prayer', 'sensor.asr_prayer', 'sensor.isha_prayer', 'sensor.maghrib_prayer')
    | map(attribute='state') | map('as_timestamp') | list }}
1 Like

Sir 123

how about this template?
{% set x = states(‘sensor.maghrib’) %}
{{ now() >= today_at(strptime(x, “%I:%M %p”).time()) }}

where should i placed the time?
Thank you for your assistance.