Upcoming Prayer (Islam)

Hi everyone,

I have been struggling for a week to make a sensor to show which prayer is next.

example of the result I would like to have:

I even tried with two time of timestamps:

The input is comping form a rest API. I try do fix it with a code but I am doing something wrong. Can anyone help me out?

sensor:
  - platform: template
    sensors:
      next_prayer:
        friendly_name: "Next Prayer"
        value_template: >
          {% set fajr_time = states('sensor.fajr_readable') %}
          {% set dhuhr_time = states('sensor.dhuhr_readable') %}
          {% set asr_time = states('sensor.asr_readable') %}
          {% set maghrib_time = states('sensor.maghrib_readable') %}
          {% set isha_time = states('sensor.isha_readable') %}
          {% set current_time = now().strftime('%H:%M') %}
          {% set current_datetime = strptime(current_time, '%H:%M') %}
          {% set events = [fajr_time, dhuhr_time, asr_time, maghrib_time, isha_time] %}
          {% set valid_events = events | select('regex_match', '^\d{2}:\d{2}$') | list %}
          {% if valid_events %}
            {% set valid_events_datetimes = valid_events | map('strptime', '%H:%M') | list %}
            {{ valid_events_datetimes | select('>', current_datetime) | min }}
          {% else %}
            "No upcoming events"
          {% endif %}
        icon_template: mdi:clock-time-four-outline

I think your sensor times are in a good format

Mine look like this as I want to play the adhaan too

Let me try to recreate what you’re trying to achieve :crossed_fingers: or :vulcan_salute:

1 Like

regex_match is not a valid test for select, you would use match.

But I would set the whole template up a bit differently to take advantage of Jinja’s filters:

{% set s_list = ['sensor.fajr_readable', 'sensor.dhuhr_readable', 'sensor.asr_readable', 'sensor.maghrib_readable', 'sensor.isha_readable'] %}

{% set upcoming_events = expand(s_list | select('has_value')) 
| selectattr('state', 'ge', now().strftime('%H:%M'))
| list %}

{% if upcoming_events %}
  {% set first = upcoming_events | sort(attribute='state') | first %}
  {{ first.attributes.friendly_name }} at {{first.state}}
{% else %}
  "No upcoming events"
{% endif %}
1 Like

It’s the ugliest thing I’ve ever written as I’m no jinga expert

but it works

  {% if fajr_time > now().strftime("%H:%M") %}
  Fajr : {{ fajr_time }}
  {% elif dhuhr_time > now().strftime("%H:%M") %}
  Dhuhr : {{ dhuhr_time }}
  {% elif asr_time > now().strftime("%H:%M") %}
  Asr : {{ asr_time }}
  {% elif maghrib_time > now().strftime("%H:%M") %}
  Maghrib : {{ maghrib_time }}
  {% elif isha_time > now().strftime("%H:%M") %}
  Isha : {{ isha_time }}
  {% else %}
  No upcoming events
  {% endif %}
1 Like

Thank you so much!

Sorry some double code because struggling with next prayer :wink:

rest:
    - resource: http://api.aladhan.com/timingsByCity?city=Amsterdam&country=Netherlands&method=3
      scan_interval: 43200
      sensor:
        - name: "Imsak"
          unique_id: imsak
          value_template: "{{ today_at(value_json['data']['timings']['Imsak']) }}"
          device_class: timestamp
          icon: mdi:mosque
        - name: "imsak_readable"
          unique_id: imsak_readable
          value_template: "{{ as_timestamp(today_at(value_json['data']['timings']['Imsak'])) | timestamp_custom('%H:%M') }}"
          icon: mdi:mosque    

        - name: "Fajr"
          unique_id: fajr
          value_template: "{{ today_at(value_json['data']['timings']['Fajr']) }}"
          device_class: timestamp
          icon: mdi:mosque
        - name: "fajr_readable"
          unique_id: fajr_readable
          value_template: "{{ as_timestamp(today_at(value_json['data']['timings']['Fajr'])) | timestamp_custom('%H:%M') }}"
          icon: mdi:mosque 

        - name: "Dhuhr"
          unique_id: dhuhr
          value_template: "{{ today_at(value_json['data']['timings']['Dhuhr']) }}"
          device_class: timestamp
          icon: mdi:mosque
        - name: "dhuhr_readable"
          unique_id: dhuhr_readable
          value_template: "{{ as_timestamp(today_at(value_json['data']['timings']['Dhuhr'])) | timestamp_custom('%H:%M') }}"
          icon: mdi:mosque   
          
        - name: "Asr"
          unique_id: asr
          value_template: "{{ today_at(value_json['data']['timings']['Asr']) }}"
          device_class: timestamp
          icon: mdi:mosque
        - name: "asr_readable"
          unique_id: asr_readable
          value_template: "{{ as_timestamp(today_at(value_json['data']['timings']['Asr'])) | timestamp_custom('%H:%M') }}"
          icon: mdi:mosque   


        - name: "Maghrib"
          unique_id: maghrib
          value_template: "{{ today_at(value_json['data']['timings']['Maghrib']) }}"
          device_class: timestamp
          icon: mdi:mosque
        - name: "maghrib_readable"
          unique_id: maghrib_readable
          value_template: "{{ as_timestamp(today_at(value_json['data']['timings']['Maghrib'])) | timestamp_custom('%H:%M') }}"
          icon: mdi:mosque   


        - name: "Isha"
          unique_id: isha
          value_template: "{{ today_at(value_json['data']['timings']['Isha']) }}"
          device_class: timestamp
          icon: mdi:mosque
        - name: "Isha_readable"
          unique_id: isha_readable
          value_template: "{{ as_timestamp(today_at(value_json['data']['timings']['Isha'])) | timestamp_custom('%H:%M') }}"
          icon: mdi:mosque   

        - name: "Hijri"
          unique_id: hijri
          value_template: "{{ value_json['data']['date']['hijri']['date'] }}"
          icon: mdi:mosque

        - name: "Weekday"
          unique_id: weekday
          value_template: "{{ value_json['data']['date']['hijri']['day'] }}"
          icon: mdi:mosque

        - name: "Month"
          unique_id: month
          value_template: "{{ value_json['data']['date']['hijri']['month']['en'] }}"
          icon: mdi:mosque

        - name: "Islam_Year"
          unique_id: islam_year
          value_template: "{{ value_json['data']['date']['hijri']['year'] }}"
          icon: mdi:mosque
1 Like

Out of curiosity, isn’t this integration any good to you?

I think you should play to see if it calculates the prayer times correctly

Personally I scrape the local mosques website for the times

I prefer separate sensors for times (and not all times😉) and the problem is we don’t have much local masjid in the Netherlands. only 900.000 Muslims of the 17.900.000.

2 Likes

Well the integration does create separate sensors it’s not the same as the one in created

Worth testing, I don’t use it at the moment I just have it installed to play with

1 Like

Me also wanna make an integration so you can play adhan for example on speakers. I personally play it because you will never hear it outside.

1 Like

Yeah for my use case - I wrote everything myself but when searching the community I found that there’s an integration as posted before

and also this blueprint to play the azaan which requires the integration… I’ve not tested them but worth experimenting rather than coding

1 Like

Will look in that too, thanks for sharing! Now time to rest for suhur :wink:

1 Like

Assalamu Alaikum

I am not very good at templates but I tried this and get a long result.

I put this in to a template card and it shows the same result. How can I get it show just the upcoming prayer name and time as the op showed in his card? Any help appreciated.

sensor:
   - platform: template
      sensors:
        next_prayer:
          friendly_name: "Next Prayer"
          value_template: >
              {% set s_list = {'sensor.fajr': 'Fajr', 'sensor.dhuhr': 'Dhuhr', 'sensor.asr': 'Asr', 'sensor.maghrib': 'Maghrib', 'sensor.isha': 'Isha'} %}
  
              {% set local_time = now().timestamp() %}
              {% set upcoming_events = expand(s_list.keys() | select('has_value')) 
              | selectattr('state', 'ge', (local_time)|timestamp_custom('%Y-%m-%dT%H:%M:%S', False))
              | list %}
  
              {% if upcoming_events %}
                {% set first = upcoming_events | sort(attribute='state') | first %}
                {% set time_difference = (as_timestamp(first.state) - local_time) / 60 %}
                {% if time_difference < 60 %}
                  {{ s_list[first.entity_id] }} in {{ time_difference | round(0) }} minutes
                {% elif time_difference_minutes == 1 %}
                     1 minute
                {% else %}
                  {{ as_timestamp(first.state) | timestamp_custom('%H:%M') }} ({{ s_list[first.entity_id] }})
                {% endif %}
              {% else %}
                No upcoming prayer
              {% endif %}
  
          icon_template: mdi:skip-next-circle

Made it more compact

1 Like

Thanks i will try this

I got it working

I would like to change the format to same as your card, "Asr in x minutes’ or ‘Asr at hh:mm’. Could you help?

Guys i have issue with next prayer time. I am using restfull api to get the prayer times and is working very fine.

When i put next on my sensor, i get unavailable. What i am doing wrong here? I add here 2 configs but same result both.

  - platform: template
    sensors:
      next_prayer:
        friendly_name: Next Prayer
        icon_template: mdi:star-crescent
        value_template: >
          {% if as_timestamp(now()) > as_timestamp(states('sensor.imsak')) and as_timestamp(now()) < as_timestamp(states('sensor.ogle')) %}
            {{ states('sensor.ogle') | as_timestamp | timestamp_custom('%I:%M') }}
          {%  elif as_timestamp(now()) > as_timestamp(states('sensor.ogle')) and as_timestamp(now()) < as_timestamp(states('sensor.ikindi')) %}
            {{ states('sensor.ikindi') | as_timestamp | timestamp_custom('%I:%M') }}
          {%  elif as_timestamp(now()) > as_timestamp(states('sensor.ikindi')) and as_timestamp(now()) < as_timestamp(states('sensor.aksam')) %}
            {{ states('sensor.aksam') | as_timestamp | timestamp_custom('%I:%M') }}
          {%  elif as_timestamp(now()) > as_timestamp(states('sensor.aksam')) and as_timestamp(now()) < as_timestamp(states('sensor.yatsi')) %}
            {{ states('sensor.yatsi') | as_timestamp | timestamp_custom('%I:%M') }}
          {% else %}
            {{ states('sensor.imsak') | as_timestamp | timestamp_custom('%I:%M') }}
          {% endif %}
  - platform: template
    sensors:
      namaz_vakti:
        friendly_name: "Namaz Vakti"
        value_template: >
          {% set s_list = {'sensor.imsak': 'Imsak', 'sensor.ogle': 'Ogle', 'sensor.ikindi': 'Ikindi', 'sensor.aksam': 'Aksam', 'sensor.yatsi': 'Yatsi'} %}
  
          {% set local_time = now().timestamp() %}
          {% set upcoming_events = expand(s_list.keys() | select('has_value')) 
          | selectattr('state', 'ge', (local_time)|timestamp_custom('%Y-%m-%dT%H:%M:%S', False))
          | list %}
  
          {% if upcoming_events %}
            {% set first = upcoming_events | sort(attribute='state') | first %}
            {% set time_difference = (as_timestamp(first.state) - local_time) / 60 %}
            {% if time_difference < 60 %}
              {{ s_list[first.entity_id] }} in {{ time_difference | round(0) }} minutes
            {% elif time_difference_minutes == 1 %}
                 1 minute
            {% else %}
              {{ as_timestamp(first.state) | timestamp_custom('%H:%M') }} ({{ s_list[first.entity_id] }})
            {% endif %}
          {% else %}
            No upcoming prayer
          {% endif %}

Hi Guys, I know this a very old case, but I am new into HA and I’m very inerested to get this work. Can anyone please be so kind and help?