Azan / Athan / Adhan (first ever post on any community page)

Home Assistant Automation: Azan Reminder & Soundbar Control

Overview

This automation ensures that a reminder plays 5 minutes before the Dhuhr, Asr, Maghrib, and Isha Athan (Azan) using a media player (e.g., a soundbar). Additionally, it turns off a connected switch (e.g., a TV) to avoid interruptions.

For Fajr, the configuration is not yet added, but feel free to contribute!

Requirements

  1. Mawaqit Integration: Follow this guide (Thanks, Moha Tah!) to integrate Mawaqit into Home Assistant.
  2. A compatible media player in Home Assistant (e.g., a DLNA soundbar).
  3. An entity switch (optional) to turn off devices like a TV before the Azan.

YAML Code

yaml

CopyEdit

alias: 4 Azan Working - Soundbar
description: "Reminder 5 minutes before Azan & turn off TV"
trigger:
  - platform: template
    value_template: >-
      {{ (as_timestamp(states('sensor.dhuhr_adhan')) - 300) | timestamp_custom('%H:%M:%S') == now().strftime('%H:%M:%S') }}
  - platform: template
    value_template: >-
      {{ (as_timestamp(states('sensor.asr_adhan')) - 300) | timestamp_custom('%H:%M:%S') == now().strftime('%H:%M:%S') }}
  - platform: template
    value_template: >-
      {{ (as_timestamp(states('sensor.maghrib_adhan')) - 300) | timestamp_custom('%H:%M:%S') == now().strftime('%H:%M:%S') }}
  - platform: template
    value_template: >-
      {{ (as_timestamp(states('sensor.isha_adhan')) - 300) | timestamp_custom('%H:%M:%S') == now().strftime('%H:%M:%S') }}

  # Main Azan triggers
  - platform: time
    at: sensor.dhuhr_adhan
  - platform: time
    at: sensor.asr_adhan
  - platform: time
    at: sensor.maghrib_adhan
  - platform: time
    at: sensor.isha_adhan

condition: []

action:
  - choose:
      # 5-minute pre-Azan alert
      - conditions:
          - condition: template
            value_template: >-
              {{ (as_timestamp(states('sensor.dhuhr_adhan')) - 300) | int == now().timestamp() | int or
                 (as_timestamp(states('sensor.asr_adhan')) - 300) | int == now().timestamp() | int or
                 (as_timestamp(states('sensor.maghrib_adhan')) - 300) | int == now().timestamp() | int or
                 (as_timestamp(states('sensor.isha_adhan')) - 300) | int == now().timestamp() | int }}
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.nabeel_steckdose_l1
          - service: media_player.play_media
            target:
              entity_id: media_player.soundbar_dnla
            data:
              media_content_id: media-source://media_source/local/azan/beep_with_silence.mp3
              media_content_type: audio/mpeg
          
      # Main Azan (5 min later)
      - conditions: []
        sequence:
          - service: media_player.play_media
            target:
              entity_id: media_player.soundbar_dnla
            data:
              media_content_id: media-source://media_source/local/azan/Azan.mp3
              media_content_type: audio/mpeg

mode: single

How It Works

:white_check_mark: 5 minutes before Azan:

  • Plays a short notification sound.
  • Turns off the TV (or any switch you define).

:white_check_mark: At Azan time:

  • Plays the full Athan (Azan) audio on the soundbar.

To-Do (Fajr Azan Support)

  • You need to add the Fajr Azan sensor:

yaml

CopyEdit

- platform: template
  value_template: >-
    {{ (as_timestamp(states('sensor.fajr_adhan')) - 300) | timestamp_custom('%H:%M:%S') == now().strftime('%H:%M:%S') }}
- platform: time
  at: sensor.fajr_adhan
  • Include Fajr in the condition checks inside action.

Why This Is Useful

:small_blue_diamond: Great for big sound systems that take time to start.
:small_blue_diamond: Works even if your TV or other devices are on.
:small_blue_diamond: Ensures you never miss the Azan with a reminder.

Hope this helps! Let me know if you need any improvements. :blush:

The example you posted doesn’t take advantage of various scripting improvements. For example, the Time Trigger now supports an offset. The example also uses outdated syntax such as platform instead of trigger and service instead of action.

For your consideration, the following example employs:

  • Updated syntax
  • Time Trigger with offset
  • Trigger ID to differentiate between offset/no_offset triggering
  • if-then to determine if the switch should be turned off
  • Immediate If to determine which content to play.
alias: 4 Azan Working - Soundbar
description: "Reminder 5 minutes before Azan & turn off TV"
triggers:
  - id: offset
    trigger: time
    at:
      entity_id: sensor.dhuhr_adhan
      offset: '-00:05:00'
  - id: offset
    trigger: time
    at:
      entity_id: sensor.asr_adhan
      offset: '-00:05:00'
  - id: offset
    trigger: time
    at:
      entity_id: sensor.maghrib_adhan
      offset: '-00:05:00'
  - id: offset
    trigger: time
    at:
      entity_id: sensor.isha_adhan
      offset: '-00:05:00'
  - id: offset
    trigger: time
    at:
      entity_id: sensor.fajr_adhan
      offset: '-00:05:00'
  - id: no_offset
    trigger: time
    at:
      - sensor.dhuhr_adhan
      - sensor.asr_adhan
      - sensor.maghrib_adhan
      - sensor.isha_adhan
      - sensor.fajr_adhan
conditions: []
actions:
  - if: "{{ trigger.id == 'offset' }}"
    then:
      - action: switch.turn_off
        target:
          entity_id: switch.nabeel_steckdose_l1
  - action: media_player.play_media
    target:
      entity_id: media_player.soundbar_dnla
    data:
      media_content_id: media-source://media_source/local/azan/{{ iif(trigger.id == 'offset', 'beep_with_silence', 'Azan') }}.mp3
      media_content_type: audio/mpeg
mode: single

EDIT

Correction. A single Time Trigger cannot be used to offset multiple timestamp sensors. A separate Time Trigger is needed for each sensor.

1 Like

yes as you can see it was totally chatgpt but after plenty of tries
thanks for your code but it shows error while saving somthing like 0 error
actcully i wantted to create a blueprint for ease :slight_smile:

You are absolutely correct. I made a mistake in my example. I made an incorrect assumption about how a Time Trigger can be used to offset multiple timestamp sensors. A separate Time Trigger is needed for each sensor that will be offset.

I corrected my example and confirmed it is validated without errors.

1 Like

Thanks for your reply