Skip to content

Instantly share code, notes, and snippets.

@zubir2k
Last active April 24, 2024 14:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zubir2k/4d39dba287b2252771904d2f08d1d1e8 to your computer and use it in GitHub Desktop.
Save zubir2k/4d39dba287b2252771904d2f08d1d1e8 to your computer and use it in GitHub Desktop.
Home Assistant Islamic Prayer Time [Extended]
## Official Islamic Prayer Time (extended) ##
## https://www.home-assistant.io/integrations/islamic_prayer_times
sensor:
- platform: template
sensors:
islamic_prayer_times_imsak_time:
friendly_name: "Islamic Prayer Times Imsak time"
device_class: timestamp
icon_template: mdi:calendar-clock
value_template: >-
{% set prayer_times = integration_entities("Islamic Prayer Times") %}
{{ (as_timestamp(states(prayer_times[0])) | int - (10*60)) | timestamp_utc }}
now_prayer:
friendly_name: "Prayer Time"
icon_template: mdi:calendar-clock
value_template: >-
{% set prayer_times = integration_entities("Islamic Prayer Times") %}
{% if now().timestamp() >= as_timestamp(states('sensor.islamic_prayer_times_imsak_time')) and now().timestamp() < as_timestamp(states(prayer_times[0])) %}Imsak
{% elif now().timestamp() >= as_timestamp(states(prayer_times[0])) and now().timestamp() < as_timestamp(states(prayer_times[1])) %}Fajr
{% elif now().timestamp() >= as_timestamp(states(prayer_times[1])) and now().timestamp() < as_timestamp(states(prayer_times[2])) %}Sunrise
{% elif now().timestamp() >= as_timestamp(states(prayer_times[2])) and now().timestamp() < as_timestamp(states(prayer_times[3])) %}Dhuhr
{% elif now().timestamp() >= as_timestamp(states(prayer_times[3])) and now().timestamp() < as_timestamp(states(prayer_times[4])) %}Asr
{% elif now().timestamp() >= as_timestamp(states(prayer_times[4])) and now().timestamp() < as_timestamp(states(prayer_times[5])) %}Maghrib
{% elif now().timestamp() >= as_timestamp(states(prayer_times[5])) and now().timestamp() < as_timestamp(states(prayer_times[6])) %}Isha
{% elif now().timestamp() >= as_timestamp(states(prayer_times[6])) %}Midnight
{% else %}Midnight
{% endif %}
attribute_templates:
timestamp: >-
{% set prayer_times = integration_entities("Islamic Prayer Times") %}
{%if is_state('sensor.now_prayer', 'Imsak') %}{{ states('sensor.islamic_prayer_times_imsak_time') }}
{%elif is_state('sensor.now_prayer', 'Fajr') %}{{ states(prayer_times[0]) }}
{%elif is_state('sensor.now_prayer', 'Sunrise') %}{{ states(prayer_times[1]) }}
{%elif is_state('sensor.now_prayer', 'Dhuhr') %}{{ states(prayer_times[2]) }}
{%elif is_state('sensor.now_prayer', 'Asr') %}{{ states(prayer_times[3]) }}
{%elif is_state('sensor.now_prayer', 'Maghrib') %}{{ states(prayer_times[4]) }}
{%elif is_state('sensor.now_prayer', 'Isha') %}{{ states(prayer_times[5]) }}
{%elif is_state('sensor.now_prayer', 'Midnight') %}{{ states(prayer_times[6]) }}
{%endif%}
12hours: >-
{% set prayer_times = integration_entities("Islamic Prayer Times") %}
{%if is_state('sensor.now_prayer', 'Imsak') %}{{ as_timestamp(states('sensor.islamic_prayer_times_imsak_time')) | timestamp_custom('%I:%M %p') }}
{%elif is_state('sensor.now_prayer', 'Fajr') %}{{ as_timestamp(states(prayer_times[0])) | timestamp_custom('%I:%M %p') }}
{%elif is_state('sensor.now_prayer', 'Sunrise') %}{{ as_timestamp(states(prayer_times[1])) | timestamp_custom('%I:%M %p') }}
{%elif is_state('sensor.now_prayer', 'Dhuhr') %}{{ as_timestamp(states(prayer_times[2])) | timestamp_custom('%I:%M %p') }}
{%elif is_state('sensor.now_prayer', 'Asr') %}{{ as_timestamp(states(prayer_times[3])) | timestamp_custom('%I:%M %p') }}
{%elif is_state('sensor.now_prayer', 'Maghrib') %}{{ as_timestamp(states(prayer_times[4])) | timestamp_custom('%I:%M %p') }}
{%elif is_state('sensor.now_prayer', 'Isha') %}{{ as_timestamp(states(prayer_times[5])) | timestamp_custom('%I:%M %p') }}
{%elif is_state('sensor.now_prayer', 'Midnight') %}{{ as_timestamp(states(prayer_times[6])) | timestamp_custom('%I:%M %p') }}
{%endif%}
24hours: >-
{% set prayer_times = integration_entities("Islamic Prayer Times") %}
{%if is_state('sensor.now_prayer', 'Imsak') %}{{ as_timestamp(states('sensor.islamic_prayer_times_imsak_time')) | timestamp_custom('%H:%M') }}
{%elif is_state('sensor.now_prayer', 'Fajr') %}{{ as_timestamp(states(prayer_times[0])) | timestamp_custom('%H:%M') }}
{%elif is_state('sensor.now_prayer', 'Sunrise') %}{{ as_timestamp(states(prayer_times[1])) | timestamp_custom('%H:%M') }}
{%elif is_state('sensor.now_prayer', 'Dhuhr') %}{{ as_timestamp(states(prayer_times[2])) | timestamp_custom('%H:%M') }}
{%elif is_state('sensor.now_prayer', 'Asr') %}{{ as_timestamp(states(prayer_times[3])) | timestamp_custom('%H:%M') }}
{%elif is_state('sensor.now_prayer', 'Maghrib') %}{{ as_timestamp(states(prayer_times[4])) | timestamp_custom('%H:%M') }}
{%elif is_state('sensor.now_prayer', 'Isha') %}{{ as_timestamp(states(prayer_times[5])) | timestamp_custom('%H:%M') }}
{%elif is_state('sensor.now_prayer', 'Midnight') %}{{ as_timestamp(states(prayer_times[6])) | timestamp_custom('%H:%M') }}
{%endif%}
automation:
- alias: 🕋 Adzan Automation
description: Play Audio and Notification (Disable this if you wish to use your own)
trigger:
- platform: template
value_template: >-
{{ is_state('sensor.now_prayer', ['Fajr','Dhuhr','Asr','Maghrib','Isha'])
and is_state_attr('sensor.now_prayer', '24hours', now().strftime('%H:%M'))
}}
condition: []
action:
- service: notify.notify
data:
title: 🕋 Prayer Alert - {{states('sensor.now_prayer')}}
message: >-
{{ now().strftime('%-I:%M %p') }} - It is now
{{states('sensor.now_prayer')}}. Perform your salah.
- service: persistent_notification.create
data:
message: >-
{{ now().strftime('%-I:%M %p') }} - It is now
{{states('sensor.now_prayer')}}. Perform your salah.
title: 🕋 Prayer Alert - {{states('sensor.now_prayer')}}
notification_id: prayertime
- service: media_player.play_media
data:
media_content_type: audio/mp3
media_content_id: >-
{% if is_state('sensor.now_prayer','Fajr')
%}https://dl.sndup.net/rkrk/azansubuh_alexa.mp3{% else
%}https://dl.sndup.net/c62p/azan_alexa.mp3{% endif %}
extra:
title: Adzan {{states('sensor.now_prayer')}}
thumb: https://i.imgur.com/1U9Ehvr.png
target:
entity_id: media_player.all_devices #Change this to your media player
mode: single

Home Assistant Islamic Prayer Time [Extended]

Reference: https://www.home-assistant.io/integrations/islamic_prayer_times

Assalamu'alaikum

The current integration only creates sensors consist of fajr, dhuhr, asr, maghrib, isha, sunrise and midnight. This will add 2 additional sensors which will ease in creating adzan automation:

  1. Current Prayer Time
  2. Imsak Time

Installation:

  • Add the following yaml into configuration.yaml
  • It will automatically create an automation for the adzan. Be sure to adjust the media_player and audio.
  • You may also create your own automation based on the automation conditions that I have prepared.

The automation example given calls the following action:

  • Alert Persistent Notification
  • Alert via Notify.notify (for HA companion app)
  • Play adzan audio

Notes:

  • You are free to choose what type of action upon triggering the prayer time
  • Please change the media_player to your own. Do note that this automation works on Google Nest speakers.
  • Please ensure to have 2 adzan mp3 (fajr and the rest of the prayer time).
  • For alexa users, the action to play the audio would be as follows:
service: notify.alexa_media_xxx #Change this to your alexa device
data:
  data:
    type: tts
  message: >-
    <audio src='{%if is_state("sensor.now_prayer","Fajr")
    %}https://dl.sndup.net/rkrk/azansubuh_alexa.mp3{% else
    %}https://dl.sndup.net/c62p/azan_alexa.mp3{% endif %}'/>

May this be beneficial to all. Insha'Allah

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