Skip to content

Instantly share code, notes, and snippets.

@AalianKhan
Last active October 19, 2024 22:44
Show Gist options
  • Save AalianKhan/322c6ed840cecc5fc5043281d7224674 to your computer and use it in GitHub Desktop.
Save AalianKhan/322c6ed840cecc5fc5043281d7224674 to your computer and use it in GitHub Desktop.
Play the adhan on selected speakers while also pausing media players of your choice. You can also add islamic month sensor to play suhoor and iftar dua. You can also optionally disable Fajr adhan.
# MIT License
#
# Copyright (c) 2024 Aalian Khan
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
blueprint:
name: "🔊 Adhan Announcement"
description: Play the adhan on selected speakers while also pausing media players
of your choice. You can also add islamic month sensor to play suhoor and iftar
dua. You can also optionally disable Fajr adhan.
Make sure you the sensors that stores the timestamp for the adhan are named as following
sensor.fajr_prayer
sensor.dhuhr_prayer
sensor.asr_prayer
sensor.maghrib_prayer
sensor.isha_prayer
domain: automation
input:
play_media_player:
name: Media players to play
description: The Media players to play the Adhan on
selector:
target:
entity:
domain: media_player
wait_media_player:
name: Media player to wait for
description: The media player to wait for and check if adhan is finished and
continue the automation. Pick any 1 media player you selected above.
selector:
entity:
domain: media_player
multiple: false
fajr:
name: Play on fajr?
description: Should it play the adhan for fajr?
default: {}
selector:
boolean: {}
pause_media_player:
name: Media players to pause (Optional)
description: The Media players to pause when the Adhan plays. Such as TVs
default: {}
selector:
target:
entity:
domain: media_player
resume_media:
name: Resume paused media players after adhan?
description: Should it resume all paused media players after the adhan has played?
default: false
selector:
boolean: {}
change_volume:
name: Change the volume?
description: Should it adjust the volume when playing the adhan?
default: false
selector:
boolean: {}
volume:
name: The volume to set to (Optional)
description: The volume to set to while the adhan is playing
default: 0.25
selector:
number:
min: 0
max: 1
step: 0.05
islamic_month_sensor:
name: Islamic month sensor (Optional)
description: The sensor to check what month it is in the islamic calendar. An entity_id is required, put sun.sun you don't want to use it
default: "sun.sun"
selector:
entity: {}
islamic_month_sensor_value:
name: Islamic month sensor value (Optional)
description: The value to check from the sensor to see if it is Ramadan
default: ""
selector:
text: {}
fajr_mp3:
name: Path to Fajr adhan MP3 (Optional)
description: Enter the path where your custom media player is stored. Examples,
media-source://media_source/local/fajr_adhan.mp3 (local path is /media/fajr_adhan.mp3),
or an address such as https://www.islamcan.com/audio/adhan/azan1.mp3. Defaults
to my Github
default: https://github.com/AalianKhan/adhans/blob/master/adhan_fajr.mp3?raw=true
selector:
text: {}
adhan_mp3:
name: Path to adhan MP3 (Optional)
description: Enter the path where your custom media player is stored. Examples,
media-source://media_source/local/adhan.mp3 (local path is /media/adhan.mp3),
or an address such as https://www.islamcan.com/audio/adhan/azan1.mp3. Defaults
to my Github
default: https://github.com/AalianKhan/adhans/blob/master/adhan.mp3?raw=true
selector:
text: {}
suhoor_mp3:
name: Path to Suhoor dua MP3 (Optional)
description: Enter the path where your custom media player is stored. Examples,
media-source://media_source/local/suhoor_dua.mp3 (local path is /media/suhoor_dua.mp3),
or an address such as https://www.islamcan.com/audio/adhan/azan1.mp3. Defaults
to my Github
default: https://github.com/AalianKhan/adhans/blob/master/suhoor_dua.mp3?raw=true
selector:
text: {}
iftaar_mp3:
name: Path to Iftaar dua MP3 (Optional)
description: Enter the path where your custom media is stored. Examples, media-source://media_source/local/iftaar_dua.mp3
(local path is /media/ftaar_dua.mp3), or an address such as https://www.islamcan.com/audio/adhan/azan1.mp3.
Defaults to my Github
default: https://github.com/AalianKhan/adhans/blob/master/iftar_dua.mp3?raw=true
selector:
text: {}
source_url: https://gist.github.com/AalianKhan/322c6ed840cecc5fc5043281d7224674
variables:
wait_media_player: !input wait_media_player
play_fajr: !input fajr
media_resume: !input resume_media
trigger:
- platform: time
at: sensor.fajr_prayer
id: fajr_trigger
- platform: time
at: sensor.dhuhr_prayer
id: dhuhr_trigger
- platform: time
at: sensor.asr_prayer
id: asr_trigger
- platform: time
at: sensor.maghrib_prayer
id: maghrib_trigger
- platform: time
at: sensor.isha_prayer
id: isha_trigger
action:
- choose:
- conditions:
- condition: state
entity_id: !input islamic_month_sensor
state: !input islamic_month_sensor_value
sequence:
- choose:
- conditions:
- condition: and
conditions:
- condition: trigger
id: fajr_trigger
- condition: template
value_template: '{{ play_fajr == true }}'
sequence:
- service: media_player.media_pause
target: !input pause_media_player
data: {}
- service: media_player.play_media
target: !input play_media_player
data:
media_content_id: !input suhoor_mp3
media_content_type: mp3
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 0
- wait_template: '{{ is_state(wait_media_player, ''idle'') }}'
timeout: 00:05:00
- service: media_player.play_media
target: !input play_media_player
data:
media_content_id: !input fajr_mp3
media_content_type: mp3
- condition: template
value_template: '{{ media_resume }}'
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 0
- wait_template: '{{ is_state(wait_media_player, ''idle'') }}'
timeout: 00:05:00
- service: media_player.media_play
target: !input pause_media_player
data: {}
- conditions:
- condition: trigger
id: maghrib_trigger
sequence:
- service: media_player.media_pause
target: !input pause_media_player
data: {}
- service: media_player.play_media
target: !input play_media_player
data:
media_content_id: !input iftaar_mp3
media_content_type: mp3
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 0
- wait_template: '{{ is_state(wait_media_player, ''idle'') }}'
timeout: 00:05:00
- service: media_player.play_media
target: !input play_media_player
data:
media_content_id: !input adhan_mp3
media_content_type: mp3
- condition: template
value_template: '{{ media_resume }}'
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 0
- wait_template: '{{ is_state(wait_media_player, ''idle'') }}'
timeout: 00:05:00
- service: media_player.media_play
target: !input pause_media_player
data: {}
- conditions:
- condition: or
conditions:
- condition: trigger
id: dhuhr_trigger
- condition: trigger
id: asr_trigger
- condition: trigger
id: isha_trigger
sequence:
- service: media_player.media_pause
target: !input pause_media_player
data: {}
- service: media_player.play_media
target: !input play_media_player
data:
media_content_id: !input adhan_mp3
media_content_type: mp3
- condition: template
value_template: '{{ media_resume }}'
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 0
- wait_template: '{{ is_state(wait_media_player, ''idle'') }}'
timeout: 00:05:00
- if:
- condition: template
value_template: '{{ media_resume }}'
then:
- service: media_player.media_play
target: !input pause_media_player
data: {}
default:
- choose:
- conditions:
- condition: and
conditions:
- condition: trigger
id: fajr_trigger
- condition: template
value_template: '{{ play_fajr == true }}'
sequence:
- service: media_player.media_pause
target: !input pause_media_player
data: {}
- service: media_player.play_media
target: !input play_media_player
data:
media_content_id: !input fajr_mp3
media_content_type: mp3
- condition: template
value_template: '{{ media_resume }}'
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 0
- wait_template: '{{ is_state(wait_media_player, ''idle'') }}'
timeout: 00:05:00
- service: media_player.media_play
target: !input pause_media_player
data: {}
- conditions:
- condition: or
conditions:
- condition: trigger
id: dhuhr_trigger
- condition: trigger
id: asr_trigger
- condition: trigger
id: maghrib_trigger
- condition: trigger
id: isha_trigger
sequence:
- service: media_player.media_pause
target: !input pause_media_player
data: {}
- service: media_player.play_media
target: !input play_media_player
data:
media_content_id: !input adhan_mp3
media_content_type: mp3
# - service: scene.create
# data:
# snapshot_entities: !input play_media_player
# scene_id: before_adhan
# - if:
# - condition: template
# value_template: '{{ change_volume }}'
# then:
# - service: media_player.volume_set
# data:
# volume_level: !input volume
# target: !input play_media_player
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 0
- wait_template: '{{ is_state(wait_media_player, ''idle'') }}'
timeout: 00:05:00
# - service: scene.turn_on
# data: {}
# target:
# entity_id: scene.before_adhan
# enabled: true
- if:
- condition: template
value_template: '{{ media_resume }}'
then:
- service: media_player.media_play
target: !input pause_media_player
data: {}
@AalianKhan
Copy link
Author

Walaikum Salam
Thank you very much for informing I have updated the blueprint

@ncodee
Copy link

ncodee commented Dec 2, 2023

[object Object] is autofilling for both Islamic Month sensor (optional) and Islamic Month sensor value (optional) fields, which is not letting you save the automation unless these fields are filled.

Please have a look into this, jzk.

@firozozman
Copy link

Are we able to define the Prayer time calculation method using this? Or is it defaulted to something?

@AalianKhan
Copy link
Author

AalianKhan commented Mar 12, 2024

@firozozman if you are referring to islamic prayer the integration, please see this link. If you are asking the adhan api, please see this link. If you have other questions, just ask

@firozozman
Copy link

@firozozman if you are referring to islamic prayer the integration, please see this link. If you are asking the adhan api, please see this link. If you have other questions, just ask

Thanks @AalianKhan I installed Islamic Prayer Times from the link you provided. Configure your BluePrint as well. It did trigger Adhan yesterday. But stopped working this morning, since Fajr. Not sure what information you need from me, so that you may help me troubleshoot. JK

@ncodee
Copy link

ncodee commented Mar 19, 2024

[object Object] is autofilling for both Islamic Month sensor (optional) and Islamic Month sensor value (optional) fields, which is not letting you save the automation unless these fields are filled.

Please have a look into this, jzk.

@AalianKhan Can you please look at this?

@meedagithub12
Copy link

Getting error message while trying to save script

Message malformed: required key not provided @ data['trigger']

is there any instruction how to set it up or what to change in script

@haizulseron
Copy link

haizulseron commented Jul 12, 2024

I keep getting this, help please D:

Message malformed: expected str for dictionary value @ data['action'][0]['choose'][0]['conditions'][0]['state']

@umarhussain15
Copy link

Hi @AalianKhan,
Can you add the license to your Gist under which you want to distribute it?
I want to submit this blueprint to Mawaqit Home Assistant Integration, so people can also use it with Mawaqit's sensors.
Thank you.

https://github.com/mawaqit/home-assistant

@AalianKhan
Copy link
Author

AalianKhan commented Oct 16, 2024

Salam @umarhussain15, I have added a license.

@oerix
Copy link

oerix commented Oct 17, 2024

[object Object] is autofilling for both Islamic Month sensor (optional) and Islamic Month sensor value (optional) fields, which is not letting you save the automation unless these fields are filled.

Please have a look into this, jzk.

Can you address this issue please? It's been nearly a year now @AalianKhan

@umarhussain15
Copy link

Hi @oerix you can configure this new sensor in your home assistant configuration.yaml
https://www.home-assistant.io/integrations/rest
For example, I have this config

sensor:
- platform: rest
  name: Islamic Month
  resource_template:  http://api.aladhan.com/v1/gToH/?date={{ now().strftime("%d-%m-%Y") }}&adjustment=1
  value_template: "{{ value_json.data.hijri.month.en }}"

Then you can reference this sensor in automation and in the value you can write "Ramadan"

@oerix
Copy link

oerix commented Oct 18, 2024

https://www.home-assistant.io/integrations/rest

Jazak'Allah Khaiyran for this. However, that option field is labelled as 'optional', and it looks like the developer is ignoring all requests to fix this prolonged bug.

@AalianKhan
Copy link
Author

Salam @oerix,

I'm sorry for not getting back to you sooner. The truth is I wasn't totally sure how to fix it and left it at that (totally my mistake). It turns out I was just overthinking it. Basically, it needs some entity_ID to look at for it to save. So I gave it just that, I believe every installation has a sun.sun so I have set that as the default entity_ID. Before I was trying to find a way to not look for an entity_ID but I couldn't find one so this is the next best solution.

Again sorry for the very very late response.

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