so all of my configs are in sensor.yaml file, put all of the following code in Sensor file not in Template file. Because the codes using Template integration under sensor components.
- platform: rest
name: "Prayer Times"
json_attributes:
- data
resource: "http://api.aladhan.com/v1/timings?latitude=50.343535&longitude=-88.454656&method=2&school=1"
value_template: '{{ value_json["data"]["meta"]["method"]["name"].title() }}'
scan_interval: 3600
- platform: template
sensors:
fajr_prayer_time:
friendly_name: "Fajr Prayer Time"
value_template: >
{{ today_at(state_attr('sensor.prayer_times','data').timings['Fajr']).strftime("%I:%M %p") }}
dhuhr_prayer_time:
friendly_name: "Dhuhr Prayer Time"
value_template: >
{{ today_at(state_attr('sensor.prayer_times','data').timings['Dhuhr']).strftime("%I:%M %p") }}
asr_prayer_time:
friendly_name: "Asr Prayer Time"
value_template: >
{{ today_at(state_attr('sensor.prayer_times','data').timings['Asr']).strftime("%I:%M %p") }}
maghrib_prayer_time:
friendly_name: "Maghrib Prayer Time"
value_template: >
{{ today_at(state_attr('sensor.prayer_times','data').timings['Maghrib']).strftime("%I:%M %p") }}
isha_prayer_time:
friendly_name: "Isha Prayer Time"
value_template: >
{{ today_at(state_attr('sensor.prayer_times','data').timings['Isha']).strftime("%I:%M %p") }}
imsak_time:
friendly_name: "Imsak"
value_template: >
{{ today_at(state_attr('sensor.prayer_times','data').timings['Imsak']).strftime("%I:%M %p") }}
- platform: template
sensors:
next_prayer:
friendly_name: Next Prayer
value_template: >-
{% set now = strptime(states('sensor.time'), '%H:%M') %}
{% set imsak = strptime(states('sensor.imsak_time'), '%I:%M %p') %}
{% set fajr = strptime(states('sensor.fajr_prayer_time'), '%I:%M %p') %}
{% set dhuhr = strptime(states('sensor.dhuhr_prayer_time'), '%I:%M %p') %}
{% set asr = strptime(states('sensor.asr_prayer_time'), '%I:%M %p') %}
{% set maghrib = strptime(states('sensor.maghrib_prayer_time'), '%I:%M %p') %}
{% set isha = strptime(states('sensor.isha_prayer_time'), '%I:%M %p') %}
{% if now < imsak %}
{{ states('sensor.imsak_time') }}
{% elif imsak <= now < fajr %}
{{ states('sensor.fajr_prayer_time') }}
{% elif fajr <= now < dhuhr %}
{{ states('sensor.dhuhr_prayer_time') }}
{% elif dhuhr <= now < asr %}
{{ states('sensor.asr_prayer_time') }}
{% elif asr <= now < maghrib %}
{{ states('sensor.maghrib_prayer_time') }}
{% elif maghrib <= now < isha %}
{{ states('sensor.isha_prayer_time') }}
{% else %}
{% set tomorrow = now + timedelta(days=1) %}
{% set tomorrow_str = tomorrow.strftime('%Y-%m-%d') %}
{{ states('sensor.fajr_prayer_time', {'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 = strptime(next_prayer, '%I:%M %p') %}
{% set current_datetime = strptime(current_time, '%H:%M').replace(year=next_prayer_datetime.year, month=next_prayer_datetime.month, day=next_prayer_datetime.day) %}
{% 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) }}
@Tismo Salam aalikom. Could you please post your yaml code for all the configuration including the card. I am trying to get it work but with no success.
@Tismo@banstro@chintito4ever
I got it working partially but still next prayer showing as unavailable. I have all the sensors in sensors.yaml but still no dice:
I am including my code in sensors.yaml:
- platform: rest
name: "Prayer Times"
json_attributes:
- data
resource: "http://api.aladhan.com/v1/timings?latitude=mylatitude&longitude=-mylongtitude&method=1&school=2"
value_template: '{{ value_json["data"]["meta"]["method"]["name"].title() }}'
scan_interval: 3600
- platform: template
sensors:
fajr_prayer_time:
friendly_name: "Fajr Prayer Time"
value_template: >
{{ today_at(state_attr('sensor.prayer_times','data').timings['Fajr']).strftime("%I:%M %p") }}
dhuhr_prayer_time:
friendly_name: "Dhuhr Prayer Time"
value_template: >
{{ today_at(state_attr('sensor.prayer_times','data').timings['Dhuhr']).strftime("%I:%M %p") }}
asr_prayer_time:
friendly_name: "Asr Prayer Time"
value_template: >
{{ today_at(state_attr('sensor.prayer_times','data').timings['Asr']).strftime("%I:%M %p") }}
maghrib_prayer_time:
friendly_name: "Maghrib Prayer Time"
value_template: >
{{ today_at(state_attr('sensor.prayer_times','data').timings['Maghrib']).strftime("%I:%M %p") }}
isha_prayer_time:
friendly_name: "Isha Prayer Time"
value_template: >
{{ today_at(state_attr('sensor.prayer_times','data').timings['Isha']).strftime("%I:%M %p") }}
imsak_time:
friendly_name: "Imsak"
value_template: >
{{ today_at(state_attr('sensor.prayer_times','data').timings['Imsak']).strftime("%I:%M %p") }}
- platform: template
sensors:
next_prayer:
friendly_name: Next Prayer
value_template: >-
{% set now = strptime(states('sensor.time'), '%H:%M') %}
{% set imsak = strptime(states('sensor.imsak_time'), '%I:%M %p') %}
{% set fajr = strptime(states('sensor.fajr_prayer_time'), '%I:%M %p') %}
{% set dhuhr = strptime(states('sensor.dhuhr_prayer_time'), '%I:%M %p') %}
{% set asr = strptime(states('sensor.asr_prayer_time'), '%I:%M %p') %}
{% set maghrib = strptime(states('sensor.maghrib_prayer_time'), '%I:%M %p') %}
{% set isha = strptime(states('sensor.isha_prayer_time'), '%I:%M %p') %}
{% if now < imsak %}
{{ states('sensor.imsak_time') }}
{% elif imsak <= now < fajr %}
{{ states('sensor.fajr_prayer_time') }}
{% elif fajr <= now < dhuhr %}
{{ states('sensor.dhuhr_prayer_time') }}
{% elif dhuhr <= now < asr %}
{{ states('sensor.asr_prayer_time') }}
{% elif asr <= now < maghrib %}
{{ states('sensor.maghrib_prayer_time') }}
{% elif maghrib <= now < isha %}
{{ states('sensor.isha_prayer_time') }}
{% else %}
{% set tomorrow = now + timedelta(days=1) %}
{% set tomorrow_str = tomorrow.strftime('%Y-%m-%d') %}
{{ states('sensor.fajr_prayer_time', {'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 = strptime(next_prayer, '%I:%M %p') %}
{% set current_datetime = strptime(current_time, '%H:%M').replace(year=next_prayer_datetime.year, month=next_prayer_datetime.month, day=next_prayer_datetime.day) %}
{% 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) }}
it should work, did you put in right place? if you’re using separate file like ‘sensors.yaml’ then do not forget to add ‘sensor: !include sensors.yaml’ in your main ‘configuration.yaml’ file.
Does using Islamic Prayer Times conflict with using the sensors created by the codes above since both use the same api? This might be why the next prayer sensor shows as unavailable?
same issue also by me. I have also installed the official prayer time integration but no way how i can show the next prayer time on the lovelace card like this.
Walaikum Assalam brother, so I don’t use official prayer time integration because I follow Hanafi school and official one doesn’t have that option, so instead I use Al-Adhan API. I have shared whole config in this thread, if you filter my comments you’ll find it. In my config you’ll find I have a sensor named ‘sensor.time_until_next_prayer’ that calculates time between now and next prayer in seconds format.
Now, for timer I have created a timer sensor using helper just you did already then set up an automation which automatically put the duration of times between two prayer times.
Thank you for all your hard work, the automation for the timer complete the display.
In addition, i make some tweak on the display (lovelace), if anyone intrested, please refer below for the code. Credit and full configuration goes to @chintito4ever .
Display with borders:
type: custom:stack-in-card
cards:
- type: custom:mushroom-template-card
primary: Prayer Times
secondary: >-
Next Prayer: Solat {{ states('sensor.next_prayer_name') }} | {{
states('sensor.next_prayer_time') }} | Starts in {{
states('sensor.time_until_next_prayer_formatted') |
regex_replace('^(\\d+):.*$', '\\1') | int }}H {{
states('sensor.time_until_next_prayer_formatted') |
regex_replace('^\\d+:(\\d+)$', '\\1') | int }}M
icon: mdi:mosque
entity: sensor.next_prayer_time
layout: horizontal
tap_action:
action: none
hold_action:
action: none
icon_color: green
fill_container: false
multiline_secondary: false
card_mod:
style:
mushroom-shape-icon$: |
.shape {
--shape-animation: ping 2s infinite;
}
@keyframes ping {
0% {
box-shadow: 0 0 5px 1px rgba(var(--rgb-green), 0.7);
}
100% {
box-shadow: 0 0 5px 15px transparent;
}
}
- type: custom:timer-bar-card
entities:
- timer.next_prayer_timer
layout: full_row
bar_radius: 5px
bar_height: 15px
bar_width: 50%
text_width: 70px
compressed: true
filter: true
bar_foreground: '#eb3434'
bar_background: '#75fa87'
- type: horizontal-stack
cards:
- type: custom:mushroom-template-card
entity: sensor.subuh_time
primary: Subuh
secondary: |
{{ states('sensor.subuh_time') }}
icon: mdi:weather-sunset-up
icon_color: '#F44336'
tap_action:
action: more-info
layout: vertical
fill_container: false
multiline_secondary: false
style: |
ha-card {
border: none; align-items: center;
}
card_mod:
style: |
.mushroom-template-card .title {
grid-row: 1 / 2; /* span across the first row */
justify-self: start; /* align to the left */
}
.mushroom-template-card .subtitle {
grid-row: 2 / 3; /* span across the second row */
justify-self: end; /* align to the right */
}
- type: custom:mushroom-template-card
entity: sensor.zuhur_time
primary: Zuhur
secondary: |
{{ states('sensor.zuhur_time') }}
icon: mdi:weather-sunny
icon_color: '#F1C40F'
tap_action:
action: more-info
layout: vertical
fill_container: false
multiline_secondary: false
card_mod:
style: |
.mushroom-template-card .title {
grid-row: 1 / 2; /* span across the first row */
justify-self: start; /* align to the left */
}
.mushroom-template-card .subtitle {
grid-row: 2 / 3; /* span across the second row */
justify-self: end; /* align to the right */
}
- type: custom:mushroom-template-card
entity: sensor.asar_time
primary: Asar
secondary: |
{{ states('sensor.asar_time') }}
icon: mdi:weather-sunset-down
icon_color: '#9C27B0'
tap_action:
action: more-info
layout: vertical
fill_container: false
multiline_secondary: false
card_mod:
style: |
.mushroom-template-card .title {
grid-row: 1 / 2; /* span across the first row */
justify-self: start; /* align to the left */
}
.mushroom-template-card .subtitle {
grid-row: 2 / 3; /* span across the second row */
justify-self: end; /* align to the right */
}
- type: custom:mushroom-template-card
entity: sensor.maghrib_time
primary: Maghrib
secondary: |
{{ states('sensor.maghrib_time') }}
icon: mdi:weather-sunset
icon_color: '#FF9800'
tap_action:
action: more-info
layout: vertical
fill_container: false
multiline_secondary: false
card_mod:
style: |
.mushroom-template-card .title {
grid-row: 1 / 2; /* span across the first row */
justify-self: start; /* align to the left */
}
.mushroom-template-card .subtitle {
grid-row: 2 / 3; /* span across the second row */
justify-self: end; /* align to the right */
}
- type: custom:mushroom-template-card
entity: sensor.isyak_time
primary: Isyak
secondary: |
{{ states('sensor.isyak_time') }}
icon: mdi:weather-night
icon_color: '#673AB7'
tap_action:
action: more-info
layout: vertical
fill_container: false
multiline_secondary: false
card_mod:
style: |
.mushroom-template-card .title {
grid-row: 1 / 2; /* span across the first row */
justify-self: start; /* align to the left */
}
.mushroom-template-card .subtitle {
grid-row: 2 / 3; /* span across the second row */
justify-self: end; /* align to the right */
}
Display without borders:
type: custom:stack-in-card
cards:
- type: custom:mushroom-template-card
primary: Prayer Times
secondary: >-
Next Prayer: Solat {{ states('sensor.next_prayer_name') }} | {{
states('sensor.next_prayer_time') }} | Starts in {{
states('sensor.time_until_next_prayer_formatted') |
regex_replace('^(\\d+):.*$', '\\1') | int }}H {{
states('sensor.time_until_next_prayer_formatted') |
regex_replace('^\\d+:(\\d+)$', '\\1') | int }}M
icon: mdi:mosque
entity: sensor.next_prayer_time
layout: horizontal
tap_action:
action: none
hold_action:
action: none
icon_color: green
fill_container: false
multiline_secondary: false
card_mod:
style:
mushroom-shape-icon$: |
.shape {
--shape-animation: ping 2s infinite;
}
@keyframes ping {
0% {
box-shadow: 0 0 5px 1px rgba(var(--rgb-green), 0.7);
}
100% {
box-shadow: 0 0 5px 15px transparent;
}
}
- type: custom:timer-bar-card
entities:
- timer.next_prayer_timer
layout: full_row
bar_radius: 5px
bar_height: 15px
bar_width: 50%
text_width: 70px
compressed: true
filter: true
bar_foreground: '#eb3434'
bar_background: '#75fa87'
- type: horizontal-stack
cards:
- type: custom:mushroom-template-card
entity: sensor.subuh_time
primary: Subuh
secondary: |
{{ states('sensor.subuh_time') }}
icon: mdi:weather-sunset-up
icon_color: '#F44336'
tap_action:
action: more-info
layout: vertical
fill_container: false
multiline_secondary: false
style: |
ha-card {
border: none; align-items: center;
}
card_mod:
style: |
.mushroom-template-card .title {
grid-row: 1 / 2; /* span across the first row */
justify-self: start; /* align to the left */
}
.mushroom-template-card .subtitle {
grid-row: 2 / 3; /* span across the second row */
justify-self: end; /* align to the right */
}
- type: custom:mushroom-template-card
entity: sensor.zuhur_time
primary: Zuhur
secondary: |
{{ states('sensor.zuhur_time') }}
icon: mdi:weather-sunny
icon_color: '#F1C40F'
tap_action:
action: more-info
layout: vertical
fill_container: false
multiline_secondary: false
card_mod:
style: |
.mushroom-template-card .title {
grid-row: 1 / 2; /* span across the first row */
justify-self: start; /* align to the left */
}
.mushroom-template-card .subtitle {
grid-row: 2 / 3; /* span across the second row */
justify-self: end; /* align to the right */
}
- type: custom:mushroom-template-card
entity: sensor.asar_time
primary: Asar
secondary: |
{{ states('sensor.asar_time') }}
icon: mdi:weather-sunset-down
icon_color: '#9C27B0'
tap_action:
action: more-info
layout: vertical
fill_container: false
multiline_secondary: false
card_mod:
style: |
.mushroom-template-card .title {
grid-row: 1 / 2; /* span across the first row */
justify-self: start; /* align to the left */
}
.mushroom-template-card .subtitle {
grid-row: 2 / 3; /* span across the second row */
justify-self: end; /* align to the right */
}
- type: custom:mushroom-template-card
entity: sensor.maghrib_time
primary: Maghrib
secondary: |
{{ states('sensor.maghrib_time') }}
icon: mdi:weather-sunset
icon_color: '#FF9800'
tap_action:
action: more-info
layout: vertical
fill_container: false
multiline_secondary: false
card_mod:
style: |
.mushroom-template-card .title {
grid-row: 1 / 2; /* span across the first row */
justify-self: start; /* align to the left */
}
.mushroom-template-card .subtitle {
grid-row: 2 / 3; /* span across the second row */
justify-self: end; /* align to the right */
}
- type: custom:mushroom-template-card
entity: sensor.isyak_time
primary: Isyak
secondary: |
{{ states('sensor.isyak_time') }}
icon: mdi:weather-night
icon_color: '#673AB7'
tap_action:
action: more-info
layout: vertical
fill_container: false
multiline_secondary: false
card_mod:
style: |
.mushroom-template-card .title {
grid-row: 1 / 2; /* span across the first row */
justify-self: start; /* align to the left */
}
.mushroom-template-card .subtitle {
grid-row: 2 / 3; /* span across the second row */
justify-self: end; /* align to the right */
}
card_mod:
style: |
:host {
--stack-card-margin: 0px;
--ha-card-border-radius: 12px;
--ha-card-border-width: 0px;
}
For ‘sensor.next_prayer_name’ please refer code as below (paste it inside ‘sensor.yaml’):
- platform: template
sensors:
next_prayer_name:
friendly_name: Next Prayer Name
value_template: >-
{% set now = strptime(states('sensor.time'), '%H:%M') %}
{% set imsak = strptime(states('sensor.imsak_time'), '%I:%M %p') %}
{% set fajr = strptime(states('sensor.subuh_time'), '%I:%M %p') %}
{% set dhuhr = strptime(states('sensor.zuhur_time'), '%I:%M %p') %}
{% set asr = strptime(states('sensor.asar_time'), '%I:%M %p') %}
{% set maghrib = strptime(states('sensor.maghrib_time'), '%I:%M %p') %}
{% set isha = strptime(states('sensor.isyak_time'), '%I:%M %p') %}
{% if now < imsak %}
{{ state_attr('sensor.imsak_time', 'friendly_name') }}
{% elif imsak <= now < fajr %}
{{ state_attr('sensor.subuh_time', 'friendly_name') }}
{% elif fajr <= now < dhuhr %}
{{ state_attr('sensor.zuhur_time', 'friendly_name') }}
{% elif dhuhr <= now < asr %}
{{ state_attr('sensor.asar_time', 'friendly_name') }}
{% elif asr <= now < maghrib %}
{{ state_attr('sensor.maghrib_time', 'friendly_name') }}
{% elif maghrib <= now < isha %}
{{ state_attr('sensor.isyak_time', 'friendly_name') }}
{% else %}
{{ state_attr('sensor.subuh_time', 'friendly_name') }}
{% endif %}