Skip to content

Instantly share code, notes, and snippets.

@OJ7
Last active September 14, 2021 17:56
Show Gist options
  • Save OJ7/5de90d58b31600f6f814b0dfe0aca7b6 to your computer and use it in GitHub Desktop.
Save OJ7/5de90d58b31600f6f814b0dfe0aca7b6 to your computer and use it in GitHub Desktop.
Home Assistant Islamic Prayer Times
import json
from datetime import datetime, timedelta
try:
from urllib.request import Request, urlopen # Python 3
except ImportError:
from urllib2 import Request, urlopen # Python 2
def convertTime(salatTime, offset):
salatTime = datetime.strptime(salatTime, '%I:%M %p')
salatTime = salatTime + timedelta(hours=0, minutes=offset)
salatTime = datetime.strftime(salatTime, '%H:%M')
return salatTime
location = "baltimore,md"
apiKey = "1383220a5e440a4e2b47623ddde2b6fa91" # http://muslimsalat.com/api/#key
url = "http://muslimsalat.com/" + location + "/daily.json?key=" + apiKey
req = Request(url)
req.add_header('Accept', 'application/json')
req.add_header('Content-Type', 'application/json')
response = urlopen(req).read().decode('utf-8')
jsonString = json.loads(response)
times = jsonString['items'][0]
fajr = convertTime(times['fajr'], 0)
dhuhr = convertTime(times['dhuhr'], 0)
asr = convertTime(times['asr'], 0)
maghrib = convertTime(times['maghrib'], -5)
isha = convertTime(times['isha'], 0)
print (fajr)
print (dhuhr)
print (asr)
print (maghrib)
print (isha)
# 05:14
# 13:07
# 16:48
# 19:43
# 21:00
DOMAIN = 'salat_times'
def setup(hass, config):
hass.states.set('salat.fajr', fajr)
hass.states.set('salat.dhuhr', dhuhr)
hass.states.set('salat.asr', asr)
hass.states.set('salat.maghrib', maghrib)
hass.states.set('salat.isha', isha)
return True
@AdighaLogic
Copy link

AdighaLogic commented Apr 5, 2019

I tried the same steps but could not get into media_extractor

media_extractor works for me, I just tried it.
did you add media-extractor to your config file like so?
media_extractor:

@AdighaLogic
Copy link

Hi, were do I have to place the file? and how can I access it from Home Assistant?

HA changed the way custom components work, i'm not sure where to put this as well.
You can use this template for your automation trigger as well. However, i can't confirm if it works as i just setup up but i'll find out in a few hours at the fajr prayer

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")) }}'`

@OJ7
Copy link
Author

OJ7 commented May 15, 2019

Sorry I didn't have notifications enabled and just saw these comments.

@iBasit that is probably a better option (or even getting yearly prayer times) but opted for a quick solution with minimal parsing. :)

@ others I haven't updated my Home Assistant in a while so it may be that parts of this may be outdated. Once I get a chance to update my installation, I will go through and update this tutorial/clean up the code.

I would like to eventually create a component and submit it to the repo so anyone can just install the component directly without having to mess with any of the code. I don't have any timeline for that though.

@Alshukaily
Copy link

Salamalykum brother,
I just started using home assistant and I couldn't follow what's new and what is old so goes for adding the integration from home the UI, but I found the calculation methods missing Oman as times are different and I didn't know how to edit them. I have opened a feature request at this link https://community.home-assistant.io/t/please-add-oman-prayer-claculation-method-in-islamic-prayer-times/338650 if you could help me.
And I will be honoerd to give you any help or additional information I can get.

Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment