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
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
I hope this is helpful for anyone else trying to achieve something similar.
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 🙂
Oh that’s awesome! I didn’t see the Islamic Prayer Times sensor. I will convert my automation to use that instead! 🙂
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 ;).
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.
Thanks for the London Unified Prayer Times tip. It’s a great initiative.
I fell into a bit of a rabbit hole trying to find out where ELM and LCM (and subsequently my local) get their times from. I’ll approach the Unified guys to see how they’re calculating their times with a view to building a local implementation of that too (one day).
It would be great if we could get a local implementation of it.
I’m not entirely happy with the London Unified Prayer Times API as they expose the prayer times in 12-hour format, and as far as I can tell there isn’t any way to get it to expose in a 24-hour format.
In order to get around this, I had to write a very poorly written PHP script which acts as a middleman between HA and the API. It converts the times to 24-hour times, so it’s easier to handle in HA.
After an initial look it seems that it’s actually a curated list of times from various sources and manually adapted to cope with London-centric exceptions (one of the updates say that they’ve added times for 2020!). ELM has more info, moonlighting.com also has methods.
So not really a reproducible bunch of times unfortunately. One approach would therefore be to download the times (annually? Monthly?) and store them locally in a more appropriate format.
It’s a bit unfortunate actually, but at least it’s known and consistent.
Salaam, are you able to share your code with the London Unified Prayer Times?
many thanks
Walikum Salam,
I actually had to do it in a way that I’m not really happy with.
The London Unified Prayer Times API returns the times in 12 hour format, and I couldn’t figure out how to covert them to 24 hour in my HA automation, so instead I have a PHP script which, when accessed, fetches the prayer times, and converts the times to be in 24 hour format.
Then the rest is exactly the same as how I have done it in the post, except the URL is pointing at the PHP script.
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
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.
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.
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
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.
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
Brother Humza would be great to see this with Prayer Time Sensor and code uploaded to Git.
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.
# 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
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?
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
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
Yes, I did something like this…http://www.khanzone.com/adhan/ it a open source project,
Assalamu’Alaikum
Any chance you can build me one?
Walikum Salam,
I wouldn’t be able to build it for you, but I’m more than happy to try and help get it working if you have any problem.
Regards,
Hamzah
if you need to just install something without any technical knowledge, then try my application: http://www.khanzone.com/adhan/
As-salamu alaykum,
That’s very useful for people who don’t use home-assistant. Thank you for sharing.
Salam brother
I’m in UK and want to have something similar
Any clue what to do and where to start or any video showing step by step
Totally new to all this
Walikum Salam,
I am also in the UK.
I don’t think there is any specific video showing step by step how to do this specifically for Athan, but there are quite a few of getting started video guides for Home Assistant on YouTube.
I would advise starting with those, so you can become familiar with how this works.
salam
can you please advise what raspberry pi module will be best suited for this application? I don’t plan on anything more than automate the call to prayers and maybe some basic home automation here and there. With that said, I rather not buy the latest and greatest pi if I don’t have to.
I personally use a Raspberry Pi 3 for HomeAssistant and haven’t had any issues so far and it hardly breaks a sweat. That said, I don’t have a huge amount of automation at the moment.
If you are only using it for the Athan, it might be OK on a Raspberry Pi 2. I’m not really sure, to be honest. It would probably be best to check on the HomeAssistant forums.
The Athan automation isn’t really a resource-intensive operation so if the base installation of HomeAssistant runs the Pi smoothly, it would probably be fine for adding just the Athan automation and related sensors.
@M. Hamzah Khan. i am using your automation. everything is perfect. and triggers on time.
I just want to know that, If i want little time offset, then how to do that.
i tried
– platform: template
value_template: ‘{{ states.sensor.time.state == states(“sensor.asr”) +300 }}’
like this. but not working.
Please reply.
thanks
Salam guys,
it will be very helpful if you guys share some basic details how to get start with this? where to put this code? I am not a professional programmer but I know some stuff so with starting tips I can get through.
Jazakallah.
Thanks for this – works very well.
question: is volume action still working for you? I have it set but it doesn’t seem to be changing volume when executing automation,..
Assalamu’Alaikum,
Yes the volume action still seems to work for me. Have you tried changing the logging to a bit more verbose so you can see if the volume control action was sent?
I am trying to convert the prayer timings from 24 hr to 12 hrs, I am simply displaying the prayer time entities in Lovelace. How can I convert the timings on the frontend to 12 hours ?
Recently received the below error:
“The islamic_prayer_times platform for the sensor integration does not support platform setup. Please remove it from your config.”
Has anyone else run into this?Any suggested solutions?
Brother, Thanks for this utility. I’m new to Home Assistant and just added it to my Synology NAS. Could you help in what section do I need to place this code?
Also, I’m based out of Toronto, Canada. Is there any other changes I have to made in the above script to make it working for me?