Athan on Google Home (via Home Assistant)

I have a Google Home which I have been using for various things as I very slowly build my collection of “smart” devices.

One thing I was very interested in making my Google Home do is to have the Athan play when it is time for prayer. Unfortunately, there isn’t any native way to do this with a Google Home at the moment.

I have seen people do it using IFTTT, but as I am already using Home Assistant as my automation platform, I wanted to keep everything within it.

What is very interesting about doing it using Home Assistant is that while I can get the basic functionality of the Athan playing, I can also perform other automation that may be useful.

For example, I can switch off or pause what ever is on the TV, switch on the lights on dimly for Fajr prayer, even maybe switch on the Ambi Pur 3volution air freshener that I have plugged into an Espurna flashed Sonoff S20, so my flat smells nice during salah time!.

The way I have implemented this is as follows:

  •  Add a REST sensor which fetches the Athan time using the Al Adhan Service’s API:
  • Add template which extracts the timings for each prayer
  • Create an automation to play the Athan

I haven’t put my Home Assistant configuration on GitHub, so I’ll put it all here for now in case anyone else wants to do something similar.

sensor:
  - platform: rest
    name: "Prayer Times"
    json_attributes:
      - data
    resource: "http://api.aladhan.com/v1/timings?latitude=52.587904&longitude=-0.1458179&method=3"
    value_template: '{{ value_json["data"]["meta"]["method"]["name"].title() }}'
    scan_interval: 86400

  - platform: template
    sensors:
      fajr:
        friendly_name: 'Fajr Prayer Time'
        value_template: '{{ states.sensor.prayer_times.attributes.data.timings["Fajr"] | timestamp_custom("%H:%M") }}'
      dhuhr:
        friendly_name: 'Dhuhr Prayer Time'
        value_template: '{{ states.sensor.prayer_times.attributes.data.timings["Dhuhr"] | timestamp_custom("%H:%M") }}'
      asr:
        friendly_name: 'Asr Prayer Time'
        value_template: '{{ states.sensor.prayer_times.attributes.data.timings["Asr"] | timestamp_custom("%H:%M") }}'
      magrib:
        friendly_name: 'Magrib Prayer Time'
        value_template: '{{ states.sensor.prayer_times.attributes.data.timings["Maghrib"] | timestamp_custom("%H:%M") }}'
      isha:
        friendly_name: 'Isha Prayer Time'
        value_template: '{{ states.sensor.prayer_times.attributes.data.timings["Isha"] | timestamp_custom("%H:%M") }}'

automation:
  - alias: "Fajr Athan"
    initial_state: true
    hide_entity: true
    trigger:
      - condition: template
        value_template: '{{ states.sensor.time.state == states("sensor.fajr") }}'
    action:
      - service: media_player.volume_set
        data_template:
          entity_id: media_player.living_room_speaker
          volume_level: 0.75
      - service: media_player.play_media
        data:
          entity_id: media_player.living_room_speaker
          media_content_id: https://s3.intahnet.co.uk/athan/fajr.mp3
          media_content_type: audio/mp3

  - alias: "Athan"
    initial_state: true
    hide_entity: true
    trigger:
      - platform: template
        value_template: '{{ states.sensor.time.state == states("sensor.dhuhr") }}'
      - platform: template
        value_template: '{{ states.sensor.time.state == states("sensor.asr") }}'
      - platform: template
        value_template: '{{ states.sensor.time.state == states("sensor.maghrib") }}'
      - platform: template
        value_template: '{{ states.sensor.time.state == states("sensor.isha") }}'
    action:
      - service: media_player.volume_set
        data_template:
          entity_id: media_player.living_room_speaker
          volume_level: 0.75
      - service: media_player.play_media
        data:
          entity_id: media_player.living_room_speaker
          media_content_id: https://s3.intahnet.co.uk/athan/normal.mp3
          media_content_type: audio/mp3

This is just a basic automation that sets the volume and plays the Athan. I will expand this so that it only plays the Athan when someone is home, and use input booleans so it can be disabled if needed (for example, during Ramadan when we switch on Islam Channel for the Maghrib Athan). Now that I think of it, it’s also possible to make Home Assistant automatically switch the TV on, and over to Islam Channel during Ramadan!

One annoyance I have found is that before anything is casted to the Google Home, it makes a “blomp” sound. Unfortunately, there isn’t any way to disable this, but there are some tricks on the Home Assistant forums which allow you to get around it. I opted to just live with it as the using those methods would require Home Assistant to keep the casting connection active, and in doing so, would stop the Google Home from entering its low power sleep mode.

I hope this is helpful for anyone else trying to achieve something similar.

16 thoughts on “Athan on Google Home (via Home Assistant)”

  1. Thanks for the write up, will be using your automations for the athan. There is also an islamic prayer times sensor offered by HA (starting in 0.85) so we don’t have to go through aladhan.com anymore 🙂

      1. Actually… it looks like the component is actually just a wrapper around the Aladhan.com API anyway. And since it hides a lot of the options available there we’re probably better off sticking to the direct implementation in this post for now.

        I did look into creating a true local HA component for prayer times but as the existing Python local library is a bit of a mess it would be better to start from scratch by porting over the original JS library (here: http://praytimes.org/code/v2/js/PrayTimes.js). I’ll do it one day, but since I challenged myself to have automatic sahoor warnings done by the end of Ramadhan I’ll lean on the Aladhan API for now ;).

        1. Oh you are absolutely right, the python module that the HA component uses (both written by the same author) is just a wrapper for Aladhan.com.

          The current HA component is still cleaner than how I have done it at the moment, but I’ve stopped using Aladhan, and now use the API provided by the London Unified Prayer Times website just because my local Masjid uses the same timings.

          A true, local HA component would be the best option though.

    1. Anyway above can be explained for a non tech person?? Above went over my head with all the tech jargon, better yet put a YouTube video if possible. Lastly keep it short and simple

      Please and Thanks

  2. Salamualikom,

    Thank you brother for taking the time to find a solution for what many of us who are looking for this for a while now.

    However, could you please post the detailed step by step to replicate for the non tech savvy like me?

    Jazaka laho khayran in advance.

    1. Wa-alaykumu as-salam,

      Unfortunately, Home-Assistant has a bit of a learning curve if you aren’t really familar with configuration files and Linux, but if you are interested I would highly advise starting with these two videos:
      https://www.youtube.com/watch?v=G8XWsXlfGFQ
      https://www.youtube.com/watch?v=m24oiBUMlS4

      They are both very good videos that visually show you how to install Home-Assistant on a Raspberry Pi and also start configuring some basic automations.

      I believe it’s possible to do something similar with IFTTT which is a bit more non-techy friendly, but unfortunately I don’t really use it.

      When I have some time to play with IFTTT, I will try to figure out how to replicate it with that InshAllah.

  3. Salamualikom,

    Thank you brother for your good work i am using Hassiso 9.0 and i have setting like this but i can not Athan playing, I can also perform ca not automation
    any help will be good thanks
    sensor:
    – platform: islamic_prayer_times
    calculation_method: makkah
    sensors:
    – fajr
    – sunrise
    – dhuhr
    – asr
    – maghrib
    – isha
    – midnight

    1. Wa-alaykumu as-salam,

      I actually haven’t converted my automation to work with the islamic_prayer_times sensor.
      Are you getting any error? It would help in trying to debug the issue you are seeing.

      1. Asassalamualaikum,
        no bother no error but i can get the Athan on Google Home to play.
        see below for code
        i am also trying to use node-red too
        any help
        my phone :646-543-2204

    1. Asassalamualaikum,

      It has been on my to-do list for quite some time, but I’ve been a bit lazy about it as it’s been working. The Prayer Time sensor would definitely be better as it doesn’t rely on an external service to function. I’ll try to get my configuration moved over, and publish it on Git sometime this week.

  4. # Automation for Dhuhr, Asr, Maghrib, and Isha Adhan
    – action:
    – alias: ”
    data:
    entity_id: media_player.home # replace with your Google Home’s id
    media_content_id: http://192.168.1.76:8123/local/Adhan1.mp3 # replace with your choice of Adhan
    media_content_type: music
    service: media_extractor.play_media
    – data:
    entity_id: media_player.home # replace with your Google Home’s id
    volume_level: ‘0.7’
    service: media_player.volume_set
    alias: Adhan
    condition: []
    id: ‘1517693010922’
    trigger:
    – platform: template
    value_template: ‘{{ states.salat.dhuhr.state == states.sensor.time.state }}’
    – platform: template
    value_template: ‘{{ states.salat.asr.state == states.sensor.time.state }}’
    – platform: template
    value_template: ‘{{ states.salat.maghrib.state == states.sensor.time.state }}’
    – platform: template
    value_template: ‘{{ states.salat.isha.state == states.sensor.time.state }}’

    # Separate Automation for Fajr Adhan
    – action:
    – data:
    entity_id: media_player.home # replace with your Google Home’s id
    media_content_id: http://192.168.1.76:8123/local/Adhan2.mp3 # replace with your choice of Fajr Adhan
    media_content_type: music
    service: media_extractor.play_media
    – data:
    entity_id: media_player.home # replace with your Google Home’s id
    volume_level: ‘0.7’
    service: media_player.volume_set
    alias: Fajr Adhan
    condition: []
    id: ‘1517694139112’
    trigger:
    – platform: template
    value_template: ‘{{ states.salat.fajr.state == states.sensor.time.state }}’

    this is my code

  5. Salaam,

    Thank you for posting your code. I actually prefer this over the Islamic Prayer Times sensor as it allows for a greater choice of calculation methods.

    I have spent about a week to make this work but the commands do not trigger as expected. The prayer times are downloaded and if I manually trigger the automation, it works but for some reason it is not automatically triggered.

    Do you have any suggestions?

    1. Walikum Salam,

      I apologise for the delay in my reply, I didn’t see the comment until now!

      Did you manage to solve your issue? If not, if you can post the relevant parts of your config in GitHub or something, I can try to assist.

      Regards,

      Hamzah

  6. Salam Alaikum,

    Jazak Allah for this blog on creating a home automation.

    I’m a developer myself but fairly new to the HA world and IFTTT. Just a bit of background on how your page inspired me.

    Currently I build Raspberry Pi Athan and Qur’an players for friends, family and other members at my mosque (done 87 builds so far). Essentially it’s all developed in python, hits an API, grabs the prayer times and generates Cron entries to play the Athan at the specified times. Schedule in Cron is recreated every morning. The Qur’an player is another python script which I’ve wired some media player buttons for the functionality of playing, pausing, stopping.

    Given the Google Home Mini is now super cheap, I bought one and fell in love with how easy it is to use. I then started thinking about how I can migrate the functionality from my existing python scripts and over to my Google Home Mini.

    My question is, do you have this project setup in Git? My intention is to fully automate the build of HA and a similar type of Athan automation using Ansible and documenting installation. That way, any non-tech user can simply run one script and be on their way.

    Please let me know inshallah. It would be greatly beneficial to my community.

    Salam alaikum

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.