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.

1 Like

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.

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