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://my_local_webserver/1003.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://my_local_webserver/1001.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.

2 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 🙂

Leave a Reply

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