Skip to content

Instantly share code, notes, and snippets.

@OJ7
Last active April 18, 2024 18:55
Show Gist options
  • Save OJ7/ac5d7bd97d09c427aa1946c3d6927975 to your computer and use it in GitHub Desktop.
Save OJ7/ac5d7bd97d09c427aa1946c3d6927975 to your computer and use it in GitHub Desktop.
Home Assistant Adhan Automation for Google Home/Nest (Updated 2024)
- id: '1234556778901'
alias: Play Adhan on Google Home
description: ''
trigger:
- platform: template
value_template: "{{ \n as_timestamp(strptime(states(\"sensor.time_date\"), \"%H:%M,
%Y-%m-%d\")) == states(\"sensor.islamic_prayer_times_dhuhr_prayer\") | as_datetime
| as_timestamp or\n as_timestamp(strptime(states(\"sensor.time_date\"), \"%H:%M,
%Y-%m-%d\")) == states(\"sensor.islamic_prayer_times_asr_prayer\") | as_datetime
| as_timestamp or \n as_timestamp(strptime(states(\"sensor.time_date\"), \"%H:%M,
%Y-%m-%d\")) == states(\"sensor.islamic_prayer_times_maghrib_prayer\") | as_datetime
| as_timestamp or \n as_timestamp(strptime(states(\"sensor.time_date\"), \"%H:%M,
%Y-%m-%d\")) == states(\"sensor.islamic_prayer_times_isha_prayer\") | as_datetime
| as_timestamp \n}}"
condition: []
action:
- service: media_player.volume_set
data:
volume_level: 0.6
target:
entity_id:
- media_player.family_room_speaker
- service: media_player.play_media
data:
media_content_id: https://mydomain.duckdns.org:8123/local/adhan/adhan.mp3 # use local IP address if not setting up DDNS
media_content_type: audio/mp3
target:
entity_id: media_player.family_room_speaker
mode: single
- id: '1234556778902'
alias: Play Fajr Adhan on Google Home
description: ''
trigger:
- platform: template
value_template: "{{ \n as_timestamp(strptime(states(\"sensor.time_date\"), \"%H:%M,
%Y-%m-%d\")) == states(\"sensor.islamic_prayer_times_dhuhr_prayer\") | as_datetime
| as_timestamp \n}}"
condition: []
action:
- service: media_player.volume_set
data:
volume_level: 0.45
target:
entity_id: media_player.family_room_speaker
- service: media_player.play_media
data:
media_content_id: https://mydomain.duckdns.org:8123/local/adhan/fajr.mp3 # use local IP address if not setting up DDNS
media_content_type: audio/mp3
target:
entity_id: media_player.family_room_speaker
mode: single
- id: '1234556778903'
alias: Lower Volume at Sunrise
description: ''
trigger:
- platform: template
value_template: "{{ \n as_timestamp(strptime(states(\"sensor.time_date\"), \"%H:%M,
%Y-%m-%d\")) == states(\"sensor.islamic_prayer_times_sunrise_time\") | as_datetime
| as_timestamp\n}}"
condition: []
action:
- service: media_player.volume_set
data:
volume_level: 0.3
target:
device_id:
- a40a8d7b8e40c20b45bf6cda40b9cee0ee6
mode: single
# Loads default set of integrations. Do not remove.
default_config:
# Load frontend themes from the themes folder
frontend:
themes: !include_dir_merge_named themes
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
homeassistant:
external_url: "http://mydomain.duckdns.org" # optional for remote access
http: # optional for remote access
ssl_certificate: /ssl/fullchain.pem
ssl_key: /ssl/privkey.pem
use_x_forwarded_for: true
trusted_proxies:
- 172.30.33.0/24
mobile_app:
sensor:
- platform: time_date
display_options:
- 'time'
- 'date'
- 'date_time'
- 'time_date'
- 'time_utc'
###############################
# Prayer Times Widget Sensors #
###############################
- platform: template
sensors:
next_prayer:
friendly_name: Next Prayer
value_template: >-
{% set now = strptime(states("sensor.date_time"), "%Y-%m-%d, %H:%M") | as_timestamp | timestamp_custom('%H:%M %p') %}
{% set imsak = states('sensor.islamic_prayer_times_midnight_time') | as_timestamp | timestamp_custom('%H:%M %p') %}
{% set fajr = states('sensor.islamic_prayer_times_fajr_prayer')| as_timestamp | timestamp_custom('%H:%M %p') %}
{% set dhuhr = states('sensor.islamic_prayer_times_dhuhr_prayer')| as_timestamp | timestamp_custom('%H:%M %p') %}
{% set asr = states('sensor.islamic_prayer_times_asr_prayer')| as_timestamp | timestamp_custom('%H:%M %p') %}
{% set maghrib = states('sensor.islamic_prayer_times_maghrib_prayer')| as_timestamp | timestamp_custom('%H:%M %p') %}
{% set isha = states('sensor.islamic_prayer_times_isha_prayer')| as_timestamp | timestamp_custom('%H:%M %p') %}
{% if now < imsak %}
{{ states('sensor.islamic_prayer_times_midnight_time') }}
{% elif imsak <= now < fajr %}
{{ states('sensor.islamic_prayer_times_fajr_prayer') }}
{% elif fajr <= now < dhuhr %}
{{ states('sensor.islamic_prayer_times_dhuhr_prayer') }}
{% elif dhuhr <= now < asr %}
{{ states('sensor.islamic_prayer_times_asr_prayer') }}
{% elif asr <= now < maghrib %}
{{ states('sensor.islamic_prayer_times_maghrib_prayer') }}
{% elif maghrib <= now < isha %}
{{ states('sensor.islamic_prayer_times_isha_prayer') }}
{% else %}
{% set tomorrow = now + timedelta(days=1) %}
{% set tomorrow_str = tomorrow.strftime('%Y-%m-%d') %}
{{ states('sensor.islamic_prayer_times_fajr_prayer', {'date': tomorrow_str}) }}
{% endif %}
- platform: template
sensors:
time_until_next_prayer:
friendly_name: Time Until Next Prayer
value_template: >-
{% set current_time = now().strftime('%H:%M') %}
{% set next_prayer = states('sensor.next_prayer') %}
{% set next_prayer_datetime = next_prayer | as_datetime %}
{% set current_datetime = strptime(current_time, '%H:%M').replace(year=next_prayer_datetime.year, month=next_prayer_datetime.month, day=next_prayer_datetime.day, tzinfo=now().tzinfo) %}
{% if current_datetime < next_prayer_datetime %}
{{ (next_prayer_datetime - current_datetime).total_seconds() }}
{% else %}
{{ (next_prayer_datetime + timedelta(days=1) - current_datetime).total_seconds() }}
{% endif %}
unit_of_measurement: seconds
- platform: template
sensors:
time_until_next_prayer_formatted:
friendly_name: Time Until Next Prayer Formatted
value_template: >-
{% set total_seconds = states('sensor.time_until_next_prayer') | float %}
{% set hours = (total_seconds // 3600) | int %}
{% set minutes = ((total_seconds % 3600) // 60) | int %}
{{ '%02d:%02d' % (hours, minutes) }}

After installing Home Asssistant:

  • Install Islamic Prayer Times integration and configure its settings to your desired calculation method.
  • Place adhan files in www/adhan (www should be in the same directory as configuration.yaml).
    • Using File Editor addon: navigate to /homeassistant, create www directory, create adhan directory in there, upload adhan files.
  • Copy above files to your home assistant config directory.
  • Go to Settings -> Automations -> update the automations to use your desired device for playing adhan.

Optional for remote access:

  • Setup Dynamic DNS
    • Using DuckDNS addon: create an account/domain on duckdns.org, set domain and token in the addon's config.
  • Setup reverse proxy
    • Using NGINX Home Assistant SSL Proxy addon: set the same domain as above in the addon's config.
  • Start the above addons (enable start on boot).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment