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

Assalm u alikum, I hope someone can help me. I am trying to setup the automation script so that it mutes my Samsung soundbar before playing the Adhan on the google home mini (for Duhur, Asr, Magrib and Isha).

I am very new to all this.

# Automation for 5 times Azan
# Automation for Zuhur, Asr, Magrib, and Isha
- action:
service: media_player.volume_mute
  - data:
      entity_id: media_player.home_theater_speaker
      is_volume_muted: true

  - alias: 'Azan'
    data:
      entity_id: media_player.living_room_speaker
      media_content_id: https://www.islamcan.com/audio/adhan/azan1.mp3
      media_content_type: audio/mp3

    service: media_extractor.play_media
  - data:
      entity_id: media_player.living_room_speaker
      volume_level: '0.6'
    service: media_player.volume_set

#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 delay is based on how long the actual Adhan audio is, for this Adhan, it is just around 2 minutes and 14 seconds
  - delay: '00:02:14'

  alias: Adhan
  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")) +2400 }}'
    - 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")) }}'

- service: media_player.volume_mute
    data:
      entity_id: media_player.home_theater_speaker
      is_volume_muted: false

#Seperate Automation for Fajr 
- action:
  - alias: 'Azan'
    data:
      entity_id: media_player.living_room_speaker
      media_content_id: https://download.tvquran.com/download/TvQuran.com__Athan/TvQuran.com__01.athan.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      entity_id: media_player.living_room_speaker
      volume_level: '0.4'
    service: media_player.volume_set
  alias: Fajr_Adhan
  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")) }}'


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

I am getting the following parse error.

YAMLException: end of the stream or a document separator is expected at line 4, column 1:
    service: media_player.volume_mute
    ^

Could some one please help.

Jazzakallah

Update

  1. I am based in Sydney Australia.

  2. Hardware: Using VM through Virtualbox and following the instructions on
    Windows - Home Assistant.

  3. Windows PC is always running

  4. Integrated Google Home Mini successfully.

  5. Followed the instructions (installing file editor, updating configuration.yaml and automation.yaml as advised in this article)

Configuration.yaml

homeassistant:
  latitude: -33.907028
  longitude: 151.022690
  unit_system: metric
  time_zone: Australia/Sydney
  
default_config:
discovery:
media_extractor:
sun:

# Islamic prayer times integration
islamic_prayer_times:
    calculation_method: karachi

# Sensors
sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'time_date'
  - platform: template
    sensors:
      salah_fajr:
        friendly_name: "Salah Fajr"
        value_template: >
           {%- set a = states("sensor.fajr_prayer").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.fajr_prayer").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}
      salah_dhuhr:
        friendly_name: "Salah Dhuhr"
        value_template: >
           {%- set a = states("sensor.dhuhr_prayer").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.dhuhr_prayer").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}
      salah_asr:
        friendly_name: "Salah Asr"
        value_template: >
           {%- set a = states("sensor.asr_prayer").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.asr_prayer").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}
      salah_maghrib:
        friendly_name: "Salah Maghrib"
        value_template: >
           {%- set a = states("sensor.maghrib_prayer").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.maghrib_prayer").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}
      salah_isha:
        friendly_name: "Salah Isha"
        value_template: >
           {%- set a = states("sensor.isha_prayer").split("T")[1].split(":")[0] -%}
           {%- set b = states("sensor.isha_prayer").split("T")[1].split(":")[1] -%}
           {{ a + ":" + b }}

# Text to speech
tts:
  - platform: google_translate

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

automations.yaml

# Automation for 4 times Azan
- action:
  - alias: ''
    data:
      entity_id: media_player.dining_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.dining_room_speaker
      volume_level: '0.4'
    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.dining_room_speakerr
      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.dining_room_speaker
      volume_level: '0.4'
    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")) }}'
- alias: Restart HA
  trigger: 
    platform: time
    at: "02:00:00"  
  action:
    - service: homeassistant.restart

Results / Outcome

  1. Automations does not work, as per the states in developer tool, the did not trigger, snip is attached. One Observation is that the name of YAML file for automation in HA is named as automations.yaml


    snip of entities states in developer tools

  1. Dashboard widget looks like this, although the timings of Adhan are not as per my timezone. Please advise how can I show the Adhan timings as per my timezone?

Looking forward hearing from you.
JazzahAllah

Salam @msahar , really appreciate your effort in setting adhan automation. I have been following the thread and found myself bit lost after an integration of Islamic Prayer Time was introduced in Home Assistant.

Is it possible to have specific instructions to configure and automate with Islamic Prayer Time. You probably have already done that but wanted to segregate it from current topic.

JazzakAllah.
Mohsin

One Error I was able to find was the naming of entity (entity_id: media_player.**dining_room_speakerr**) was wrong for Fajr prayers resulting not in playing this. I will wait for Dhuhur prayers to see if it works and update the thread.

JazzakAllah

Update:
Alhamdulillah, I was able to play Asr Adhan … There was change in sensor naming convention in automation that caused the issue, I have corrected it as it was advised in the comments. Following is the correct automations.yaml file

automations.yaml

- 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.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"))
      }}'
  - 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"))
      }}'
  condition: []
  action:
  - alias: ''
    data:
      entity_id: media_player.dining_room_speaker
      media_content_id: https://www.islamcan.com/audio/adhan/azan2.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      volume_level: 0.4
    service: media_player.volume_set
    target:
      entity_id: media_player.dining_room_speaker
  mode: single
- id: '1517693034922'
  alias: Fajar Azaan
  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:
  - alias: Fajar
    data:
      entity_id: media_player.dining_room_speaker
      media_content_id: https://www.islamcan.com/audio/adhan/azan3.mp3
      media_content_type: audio/mp3
    service: media_extractor.play_media
  - data:
      volume_level: 0.4
    service: media_player.volume_set
    target:
      area_id: living_room
      device_id: 83982c1f80fda986dd93cfbee8f335d7
      entity_id: media_player.dining_room_speaker
  mode: single
- alias: Restart HA
  trigger:
    platform: time
    at: 02:00:00
  action:
  - service: homeassistant.restart

JazzakAllah

The information presented in the first post of this thread is almost 3 years old. Some of it is outdated and there are now simpler, easier ways to do the same things.

For example, I posted a revised version of the automation here and it demonstrates that the original Template Triggers can be replaced with a far more compact Time Trigger:

  trigger:
  - platform: time
    at: 
    - sensor.fajr_prayer
    - sensor.dhuhr_prayer
    - sensor.asr_prayer
    - sensor.maghrib_prayer
    - sensor.isha_prayer

If we apply this enhancement to your two automations, we get this:

- id: '1517693010922'
  alias: Adhan
  trigger:
  - platform: time
    at:
    - sensor.dhuhr_prayer
    - sensor.asr_prayer
    - sensor.maghrib_prayer
    - sensor.isha_prayer
  condition: []
  action:
  - service: media_extractor.play_media
    target:
      entity_id: media_player.dining_room_speaker
    data:
      media_content_id: https://www.islamcan.com/audio/adhan/azan2.mp3
      media_content_type: audio/mp3
  - service: media_player.volume_set
    target:
      entity_id: media_player.dining_room_speaker
    data:
      volume_level: 0.4
  mode: single
- id: '1517693034922'
  alias: Fajar Azaan
  trigger:
  - platform: time
    at: sensor.fajr_prayer
  condition: []
  action:
  - service: media_extractor.play_media
    target:
      entity_id: media_player.dining_room_speaker
    data:
      media_content_id: https://www.islamcan.com/audio/adhan/azan3.mp3
      media_content_type: audio/mp3
  - service: media_player.volume_set
    target:
      area_id: living_room
      device_id: 83982c1f80fda986dd93cfbee8f335d7
      entity_id: media_player.dining_room_speaker
    data:
      volume_level: 0.4
  mode: single

In addition, the Islamic Prayer integration can now be configured via the UI:

image

Therefore it is no longer necessary to configure it via configuration.yaml

# This is not needed because it can now be done via the UI
islamic_prayer_times:
    calculation_method: karachi

The optional five Template Sensors, showing the current day’s prayer times, can be reduced to this:

  - platform: template
    sensors:
      salah_fajr:
        friendly_name: "Salah Fajr"
        value_template: "{{ states('sensor.fajr_prayer') | as_timestamp | timestamp_custom('%H:%M') }}"
      salah_dhuhr:
        friendly_name: "Salah Dhuhr"
        value_template: "{{ states('sensor.dhuhr_prayer') | as_timestamp | timestamp_custom('%H:%M') }}"
      salah_asr:
        friendly_name: "Salah Asr"
        value_template: "{{ states('sensor.asr_prayer') | as_timestamp | timestamp_custom('%H:%M') }}"
      salah_maghrib:
        friendly_name: "Salah Maghrib"
        value_template: "{{ states('sensor.maghrib_prayer') | as_timestamp | timestamp_custom('%H:%M') }}"
      salah_isha:
        friendly_name: "Salah Isha"
        value_template: "{{ states('sensor.isha_prayer') | as_timestamp | timestamp_custom('%H:%M') }}"
1 Like

Thank you a lot for the updated information! I was wondering if it was possible to only show the highlighted information instead of all of that data:

@123

Could you please help me out configuring the configuration.yaml? I’m using the version you posted above. I now get this error:

2021-12-06 01:05:20 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'as_timestamp' got invalid input 'unknown' when rendering template '{{ states('sensor.fajr_prayer') | as_timestamp | timestamp_custom('%H:%M') }}' but no default was specified. Currently 'as_timestamp' will return 'None', however this template will fail to render in Home Assistant core 2022.1

Any help is very welcome! Thank you!

It’s not an error, it’s a warning.

WARNING (MainThread) [homeassistant.helpers.template]

The message clearly explains why:

‘as_timestamp’ got invalid input ‘unknown’

The result of this:

states('sensor.fajr_prayer')

was unknown.

That means the value of sensor.fajr_prayer is unknown. Did you install the Islamic Prayer Times integration? Do you see five sensors in Configuration > Entities related to the Prayer Times integration?

1 Like

Thank you a lot! That was indeed the problem: I did not have the Islamic Prayer Times installed. Thanks again!

Guys, the times which is gives to me is not really correct, some of 10 minutes after or before. How can i setup some + or - to let me see the correct times on lovelace?

pryaer

type: glance
entities:
  - 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

Same problem did you just find the problem? See my question up maybe a workaround if that is possible to let see the lovelace the time with some +10 min -10 min as example.

Can someone post an updated version of this, I cannot get this working correctly. the original post is outdated as of today and does not function correctly.

Currently, I am getting the following error in the logs, It does execute when I run the automation manually but does not execute on its own or on the correct time.

Thank you

Scroll up about 7 posts and you’ll find an updated version posted on July 29. That post also contains a link to the following simplified version:

https://community.home-assistant.io/t/adhan-automation-using-home-assistant-and-google-home-mini/135622/290?u=123

Still not working below is the error

That’s not one of the simplified versions I suggested.

@123
Thank you, after I reapplied the automation using your simplified version, everything worked as expected.

Below is my configuration for anyone who wishes to use it.

Configuration.yaml

homeassistant:
  latitude: xxxx
  longitude: xxxxx
  elevation: xx
  unit_system: metric
  time_zone: Asia/Kolkata

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:
discovery:
media_extractor:
sun:
updater:

# Text to speech
tts:
  - platform: google_translate

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

mqtt:
  discovery: true
  discovery_prefix: homeassistant
  broker: 192.168.xxx.xxx
  port: 18xx
  client_id: xxxxxxx
  keepalive: 60
  username: xxxxxxxxx
  password:xxxxxxxxxx

http:
  use_x_forwarded_for: true
  trusted_proxies:
    - xxx

sensor:
  - platform: time_date
    display_options:
      - "time"
      - "date"
      - "date_time"
      - "date_time_iso"
      - "time_date"
      - "time_utc"
      - "beat"

Automations.yaml

- id: "1517693010922"
  alias: Adhan
  trigger:
    - platform: time
      at:
        - sensor.fajr_prayer
        - sensor.dhuhr_prayer
        - sensor.asr_prayer
        - sensor.maghrib_prayer
        - sensor.isha_prayer
  condition: []
  action:
    - service: media_extractor.play_media
      target:
        entity_id: media_player.living_room_speaker
      data:
        media_content_id: https://www.islamcan.com/audio/adhan/azan2.mp3
        media_content_type: audio/mp3
    - service: media_player.volume_set
      target:
        entity_id: media_player.living_room_speaker
      data:
        volume_level: 1.0
  mode: single

Sensors.yaml

sensors:
  salah_fajr:
    friendly_name: "Salah Fajr"
    value_template: "{{ states('sensor.fajr_prayer') | as_timestamp | timestamp_custom('%H:%M') }}"
  salah_dhuhr:
    friendly_name: "Salah Dhuhr"
    value_template: "{{ states('sensor.dhuhr_prayer') | as_timestamp | timestamp_custom('%H:%M') }}"
  salah_asr:
    friendly_name: "Salah Asr"
    value_template: "{{ states('sensor.asr_prayer') | as_timestamp | timestamp_custom('%H:%M') }}"
  salah_maghrib:
    friendly_name: "Salah Maghrib"
    value_template: "{{ states('sensor.maghrib_prayer') | as_timestamp | timestamp_custom('%H:%M') }}"
  salah_isha:
    friendly_name: "Salah Isha"
    value_template: "{{ states('sensor.isha_prayer') | as_timestamp | timestamp_custom('%H:%M') }}"

Glad to hear it works for you but why did you feel the need to re-post the example that I had already posted?

This thread is already exceedingly long and filled with duplications; it’s preferable to just link to a post.

Because you don’t have everything in a single place, plus it’s not user-friendly because you don’t mention what goes in which file. If I had to guess these things from your post, I am sure others who are new would also have to guess the same, therefore I mentioned the complete information in 1 single post instead of going back and forth to different posts.

@Himdola and others looking for Diyanet times:

I’ve managed to make this work using scrape. Go to the diyanet website and change the location based on your current location. My example below is set to Rotterdam, The Netherlands: Diyanet İşleri Başkanlığı | Rotterdam İçin Namaz Vakti

You should change the URL in below and copy paste this in your configuration.yaml

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'
  - platform: scrape
    resource: https://namazvakitleri.diyanet.gov.tr/tr-TR/13980/rotterdam-icin-namaz-vakti
    select: "#today-pray-times-row > div:nth-child(1) > div > div.tpt-time"
    name: İmsak
  - platform: scrape
    resource: https://namazvakitleri.diyanet.gov.tr/tr-TR/13980/rotterdam-icin-namaz-vakti
    name: Güneş
    select: "#today-pray-times-row > div:nth-child(2) > div > div.tpt-time"
  - platform: scrape
    resource: https://namazvakitleri.diyanet.gov.tr/tr-TR/13980/rotterdam-icin-namaz-vakti
    name: Öğle
    select: "#today-pray-times-row > div:nth-child(3) > div > div.tpt-time"
  - platform: scrape
    resource: https://namazvakitleri.diyanet.gov.tr/tr-TR/13980/rotterdam-icin-namaz-vakti
    name: İkindi
    select: "#today-pray-times-row > div:nth-child(4) > div > div.tpt-time"
  - platform: scrape
    resource: https://namazvakitleri.diyanet.gov.tr/tr-TR/13980/rotterdam-icin-namaz-vakti
    name: Akşam
    select: "#today-pray-times-row > div:nth-child(5) > div > div.tpt-time"
  - platform: scrape
    resource: https://namazvakitleri.diyanet.gov.tr/tr-TR/13980/rotterdam-icin-namaz-vakti
    name: Yatsı
    select: "#today-pray-times-row > div:nth-child(6) > div > div.tpt-time"
  - platform: template
    sensors:
     imsak_vakti:
      friendly_name: "İmsak Vakti"
      value_template: "{{ states('sensor.imsak') == states('sensor.time') }}"
  - platform: template
    sensors:
     gunes_vakti:
      friendly_name: "Güneş Vakti"
      value_template: "{{ states('sensor.gunes') == states('sensor.time') }}"      
  - platform: template
    sensors:
     ogle_namazi:
      friendly_name: "Öğle Vakti"
      value_template: "{{ states('sensor.ogle') == states('sensor.time') }}"   
  - platform: template
    sensors:
     ikindi_namazi:
      friendly_name: "İkindi Vakti"
      value_template: "{{ states('sensor.ikindi') == states('sensor.time') }}" 
  - platform: template
    sensors:
     aksam_namazi:
      friendly_name: "Akşam Vakti"
      value_template: "{{ states('sensor.aksam') == states('sensor.time') }}" 
  - platform: template
    sensors:
     yatsi_namazi:
      friendly_name: "Yatsı Vakti"
      value_template: "{{ states('sensor.yatsi') == states('sensor.time') }}"

Go to your automations.yaml and add this:

- alias: Namaz
  trigger:
  - platform: state
    entity_id: 
    - sensor.imsak_vakti
    - sensor.ogle_namazi
    - sensor.ikindi_namazi
    - sensor.aksam_namazi
    - sensor.yatsi_namazi
    to: "True"
  action:
  - service: media_player.play_media
    data:
      entity_id: media_player.home_group
      media_content_id: https://www.naataudio.com/downloads/IslamicRingtones/bismillah_ir_rahman.mp3
      media_content_type: music
  - service: media_player.volume_set
    data:
      entity_id: media_player.home_group
      volume_level: '0.2'

Lovelace would look like this:

type: glance
entities:
  - entity: sensor.imsak
    icon: mdi:clock-time-five-outline
  - entity: sensor.ogle
    icon: mdi:clock-time-five-outline
  - entity: sensor.ikindi
    icon: mdi:clock-time-five-outline
  - entity: sensor.aksam
    icon: mdi:clock-time-five-outline
  - entity: sensor.yatsi
    icon: mdi:clock-time-five-outline
state_color: false
show_state: true
show_icon: true
show_name: true
columns: 5
4 Likes

It’s this kind of attitude that makes me regret having providing assistance in this topic.

Given that my contributions have been so terribly difficult to comprehend, I have decided to cease participation to avoid confusing anyone else.

From now on, you can answer any questions about my examples you have re-posted. Good luck.