Islamic Prayer Times
Purpose
One of the Five Pillars of Islam is the recitation of prayers 5 times a day at more or less predefined times.
These times depend on the position of the sun in the sky at your location and on the different methods used to calculate these times according to different conventions
There are 5 main prayers in the day:
Prayer | Description | |
---|---|---|
Fajr | When the sky begins to lighten (dawn). | |
Dhuhr or Zuhr | When the Sun begins to decline after reaching its highest point in the sky. | |
Asr | The time when the length of any object's shadow reaches a factor (usually 1 or 2) of the length of the object itself plus the length of that object's shadow at noon. | |
Maghrib | Soon after sunset. | |
Isha | The time at which darkness falls and there is no scattered light in the sky. |
Some of these times could be obtained from the Astro binding. However Asr is not available in the Astro binding and it’s value depends according to different Islamic schools of thought.
The times of Fajr and Isha are also not available reliably in the Astro binding at high latitudes and special calculations are required to obtain these times.
At the required times a call to prayer is performed. This is the call from the Muezzin at the top of the minaret. However this is now usually done with speakers!!
The times for the day are calculated using a python script every day after midnight.
This python script is from http://praytimes.org/
Every minute openHAB will check if one of these times is reached and perform different actions depending on your choice.
Options
In my case, I wanted the call to prayer to be played in the audio sink if the TV is off and a special video played on the TV if the TV is on. I used a spare raspberry pi with a python script mqtt client to play the video on the TV.Note that the call to prayer played at Fajr is different than the call to prayer played at the other prayer times.
It is also a tradition in some countries to close the blinds and windows and to turn on the lights in the house a few minutes before Maghrib (sunset). The lights are then turned off a few minutes after sunset to save electricity.
I also added an option to play the early prayer (Fajr) only after a predefined time in order to avoid waking up the kids in the middle of the night.
Prerequisites
-
Python is installed (The scripts are compatible with Python2 and Python3)
-
Exec binding
-
JSONPATH transform
-
Options
-
Spare Raspberry Pi
-
Television binding
-
MQTT binding
-
Audio sink setup
-
JS transform
-
Audio files:
adhan.mp3 - https://drive.google.com/open?id=1AgQQfvYerchEhArLVEhkCLPbF7D1r8uZ
adhanfajr.mp3 - https://drive.google.com/open?id=1lSVZM8KiRpWxS_2XsqEwyiUdWWKCqb_y -
Videos:
adhan.mp4 - https://drive.google.com/open?id=1bd6NP9qYwwuFixFHZclIu8EYHw7lfyA4
adhanfajr.mp4 - https://drive.google.com/open?id=1bd6NP9qYwwuFixFHZclIu8EYHw7lfyA4
-
Configuration
The files are available from:
Main Python script
Place prayertimes.py
in your conf/scripts
folder.
Unfortunately, the python script is a little buggy and I do not have the Python skills to fix it.
However there is a way around it. The bug is related to the settings for the prayer calculations.
There are many options but the main ones are the angle of the sun for the calculation of Fajr and Isha. The school or method used for the calculations and the method used for high latitudes.
I have included several examples in the script.
The settings are done at the bottom of the script.
You will need to input your latitude and longitude. (Altitude optional.)
The script will generate a JSON string with the prayer times.
Items
// *****************************
// ** Prayer Times **
// *****************************
Group PrayerTimes "Prayer Times" <islam>
// Prayer Times
String PrayerTime_Fajr "Fajr [%s]" <fajr> (PrayerTimes)
String PrayerTime_Zuhr "Zuhr [%s]" <zuhr> (PrayerTimes)
String PrayerTime_Asr "Asr [%s]" <asr> (PrayerTimes)
String PrayerTime_Isha "Isha [%s]" <isha> (PrayerTimes)
String PrayerTime_Maghrib "Maghrib [%s]" <maghrib> (PrayerTimes)
// Trigger items for prayer times (MQTT binding optional)
Switch PrayerTime_PrayerTrigger { mqtt=">[mybroker:Misc/PrayerTrigger:command:ON:ON]" }
Switch PrayerTime_PrayerTriggerFajr { mqtt=">[mybroker:Misc/PrayerTriggerFajr:command:ON:ON]" }
// Optional settings and times for Lights on and off before and after Maghrib
Number PrayerTime_MinutesLightsONBeforeMaghrib "Lights ON Before Maghrib [%02d] mins" <clock> (PrayerTimes)
Number PrayerTime_MinutesLightsOFFAfterMaghrib "Lights OFF After Maghrib [%02d] mins" <clock> (PrayerTimes)
String PrayerTime_MaghribLightsON "Maghrib Lights ON [%s]" <lighton> (PrayerTimes)
String PrayerTime_MaghribLightsOFF "Maghrib Lights OFF [%s]" <lightoff> (PrayerTimes)
Switch PrayerTime_MaghribLightsTrigger
Switch PrayerTime_MaghribPeriod
// Optional Settings for player Fajr
Number PrayerTime_EarlyAdhanTime "Earliest Adhan [%02d] o'clock" <clock> (PrayerTimes)
Switch PrayerTime_PlayEarlyAdhan "Play Fajr" <sound> (PrayerTimes)
Rules
The first rule executes the python script and retrieves the prayer times:
rule "prayer times"
when
Time cron "0 2 0 * * ?"
then
var String PT = executeCommandLine("python /etc/openhab2/scripts/prayertimes.py", 5000)
PrayerTime_Fajr.postUpdate(transform("JSONPATH", "$.fajr", PT))
PrayerTime_Zuhr.postUpdate(transform("JSONPATH", "$.dhuhr", PT))
PrayerTime_Asr.postUpdate(transform("JSONPATH", "$.asr", PT))
PrayerTime_Maghrib.postUpdate(transform("JSONPATH", "$.maghrib", PT))
PrayerTime_Isha.postUpdate(transform("JSONPATH", "$.isha", PT))
end
The second rule checks if the current time matches one of the prayer times:
rule "Prayer Time Check"
when
Time cron "0 * * * * ?" or
System started
then
var String currentTime = now.toString("HH:mm")
var Number currentHour = now.getHourOfDay() //Optional
if (currentTime == PrayerTime_Fajr.state.toString) {
if (PrayerTime_PlayEarlyAdhan.state == ON) {
if (currentHour >= (PrayerTime_EarlyAdhanTime.state as Number)) { //Optional
PrayerTime_PrayerTriggerFajr.sendCommand(ON)
} // Optional
}
}
if (currentTime == PrayerTime_Zuhr.state.toString) PrayerTime_PrayerTrigger.sendCommand(ON)
if (currentTime == PrayerTime_Asr.state.toString) PrayerTime_PrayerTrigger.sendCommand(ON)
if (currentTime == PrayerTime_Maghrib.state.toString) PrayerTime_PrayerTrigger.sendCommand(ON)
if (currentTime == PrayerTime_Isha.state.toString) PrayerTime_PrayerTrigger.sendCommand(ON)
//Optional checks for Maghrib lights routines
if (currentTime == PrayerTime_MaghribLightsON.state.toString) {
PrayerTime_MaghribPeriod.postUpdate(ON)
PrayerTime_MaghribLightsTrigger.sendCommand(ON)
}
if (currentTime == PrayerTime_MaghribLightsOFF.state.toString) {
PrayerTime_MaghribPeriod.postUpdate(OFF)
PrayerTime_MaghribLightsTrigger.sendCommand(OFF)
}
end
Optional rules
Rule to calculate the times when the lights should turn on before Maghrib and off after Magrhib
This relies on a Javascript transformation
The rule:
rule "Maghrib lights time change"
when
Item PrayerTime_Maghrib changed or
Item PrayerTime_MinutesLightsONBeforeMaghrib changed or
Item PrayerTime_MinutesLightsOFFAfterMaghrib changed
then
var String PT = PrayerTime_Maghrib.state.toString + "#-" + PrayerTime_MinutesLightsONBeforeMaghrib.state.toString
PrayerTime_MaghribLightsON.postUpdate(transform("JS", "maghriblights.js", PT))
PT = PrayerTime_Maghrib.state.toString + "#" + PrayerTime_MinutesLightsOFFAfterMaghrib.state.toString
PrayerTime_MaghribLightsOFF.postUpdate(transform("JS", "maghriblights.js", PT))
end
The Javascript transform: (Put in maghriblights.js in the transform folder)
maghriblight.js:
(function(i) {
if (i == 'NULL') { return i; }
if (i == '-') { return 'Undef'; }
var maghribtime = i.split('#')[0];
var minutes = parseInt(i.split('#')[1]);
var MM = parseInt(maghribtime.split(':')[1]);
var HH = parseInt(maghribtime.split(':')[0]);
if (minutes < 0) {
minutes = -minutes;
if ((MM - minutes) < 0) {
MM = MM + (60 - minutes);
HH = HH - 1;
} else {
MM = MM - minutes;
}
} else {
if ((MM + minutes) > 59) {
MM = MM - (60 - minutes);
HH = HH + 1;
} else {
MM = MM + minutes;
}
}
var StMM = '';
if (MM < 10) {
StMM = StMM.concat('0').concat(MM);
StMM = '0' + MM;
} else {
StMM = StMM.concat(MM);
}
var StHH = '';
StHH = StHH.concat(HH);
var returnString = StHH + ':' + StMM;
return returnString
})(input)
This first rule turns the lights on and off if we are at home
rule "Maghrib Lights"
when
Item PrayerTime_MaghribLightsTrigger received command
then
if (House_HomeAway.state.toString == "home") { //House_HomeAway item for presence
MaghribLights.sendCommand(receivedCommand)
}
end
Rule for edge case when we come home during the period around Maghrib when the lights should be on but are not because we were away.
This would have to be integrated in your own presence rules
rule "House Presence"
when
Item House_HomeAway changed
then
//General
// ... STUFF DO DO
//Coming home
if (triggeringItem.state.toString == "home") {
// Coming home during Maghrib Lights Period
if (PrayerTime_MaghribPeriod.state == ON) {
MaghribLights.sendCommand(ON)
}
//OTHER STUFF DO DO
end
Options for sound and video
Raspberry PI, python and MQTT
The main interest for me was to have the call to prayer played out at the correct times. As a bonus, when the television is on, the current program is paused, the TV input switched to the AV input to which I have connected a RaspberryPi One B. The raspberry is loaded with the Raspian Lite image. I have used the AV output but you could use the HDMI output.A small python script in the home folder is used to receive an mqtt message and play a video.
To try the script do:
python adhan.py
Some dependencies for python such as paho-mqtt may have to be installed depending on your set-up.
The script is available at:
You will need to input your mqtt broker settings in the script
To run the script automatically on start-up the following line is added at the end of the rc.local
file in the /etc
directory of the Raspberry PI
sudo -H -u pi python /home/pi/adhan.py > /tmp/adhan.out > /tmp/adhan.err &
The script will output to the file /tmp/adhan.out
and errors will be logged in /tmp/adhan.err
. This is useful for debugging.
!! DO NOT FORGET THE &
at the end of the line or your Raspberry PI will not come out of boot! The &
will instruct the script to run in the background.
Video Files
The video files:
adhan.mp4 - https://drive.google.com/open?id=1bd6NP9qYwwuFixFHZclIu8EYHw7lfyA4
adhanfajr.mp4 - https://drive.google.com/open?id=1bd6NP9qYwwuFixFHZclIu8EYHw7lfyA4
Will need to be placed in a /share
folder in the home directory of the PI. Or anywhere else. Make sure your change the Python script to reflect the location of the files.
Rules in openHAB
This the rule that make it happen.
Please adapt to your set-up:
rule "Prayer Trigger"
when
Item PrayerTime_PrayerTrigger received command or
Item PrayerTime_PrayerTriggerFajr received command
then
if (House_HomeAway.state.toString == "home") { #If we are at home
var prayerDuration = 0
var String soundFile = "adhan.mp3" #Default audio
if (triggeringItem.name.toString == "PrayerTime_PrayerTrigger") {
prayerDuration = 100 # Duration of adhan.mp4
} else {
prayerDuration = 180 # Duration of adhanfajr.mp4
soundFile = "adhanfajr.mp3" # Audio file for Fajr
}
if (LivingRoom_TVON.state == ON) { # If the TV is on
if ((LivingRoom_TVSource.state.toString == "HDMI1") || (LivingRoom_TVSource.state.toString == "HDMI-CEC")) { #if the TV is playing from satellite or chromecast
if (timer === null) {
val tvVolume = LivingRoom_TVVolume.state as Number # Save current TV volume
val tvLightColour = (LivingRoom_TVLightLeft_Colour.state as HSBType) # Save the current TV Lights colour settings
val tvLightLevel = LivingRoom_TVLightLeft_Level.state as Number # Save the current TV Lights level
SkyBox.sendCommand("pause") # Send pause to satellite box
LivingRoom_TVRemoteKey.sendCommand("KEY_PAUSE") # Send pause to TV
LivingRoom_TVVolume.sendCommand(65) # Increase TV volume to 65
Thread::sleep(1300) # Wait for Raspberry pi to run video
LivingRoom_TVSource.sendCommand("SCART") # Change TV source
LivingRoom_TVLightLeft_Colour.sendCommand(new HSBType(new DecimalType(25), new PercentType(100), new PercentType(80))) # Change TV Lights colour
LivingRoom_TVLightLeft_Level.sendCommand(100) # Change TV Lights level
timer = createTimer(now.plusSeconds(prayerDuration), [ |
LivingRoom_TVSource.sendCommand("HDMI1") # Restore TV source
LivingRoom_TVVolume.sendCommand(tvVolume) # Restore TV Volume
LivingRoom_TVLightLeft_Colour.sendCommand(tvLightColour) # Restore TV Lights colour
LivingRoom_TVLightLeft_Level.sendCommand(tvLightLevel) # Restore TV Lights level
SkyBox.sendCommand("play") # Play the satellite box
LivingRoom_TVRemoteKey.sendCommand("KEY_PLAY") # Play the TV
timer = null // reset the timer
] )
}
}
} else {
playSound(soundFile) # Play audio on the default audio sink
}
}
end
Audio Files
Your will require the two audio files:
adhan.mp3 - https://drive.google.com/open?id=1AgQQfvYerchEhArLVEhkCLPbF7D1r8uZ
adhanfajr.mp3 - https://drive.google.com/open?id=1lSVZM8KiRpWxS_2XsqEwyiUdWWKCqb_y
Place the two files in the conf/sounds
folder of openHAB