Open aasimenator opened 2 years ago
Wsalam,
Which version of Home Assistant are you on? Have you enabled debug logging to see why it's failing?
A few things to check:
- volume_level: "1"
data_template
key. If you are hard-coding something you need to change the data_template
key to data
Try with this automation:
- alias: Play Fajr Azan
trigger:
- platform: time_pattern
seconds: "30"
condition:
- condition: template
value_template: '{{ now().time().strftime("%H:%M") == states.sensor.fajr.state }}'
action:
- service: media_player.volume_set
data:
entity_id: media_player.living_room_speaker
volume_level: "1"
- service: media_player.play_media
data:
entity_id: media_player.living_room_speaker
media_content_id: https://www.islamcan.com/audio/adhan/azan1.mp3
media_content_type: audio/mp3
Also I have further optimized this and I am sharing the updates below. I will update my documentation too later on:
The main change is that I am creating a time_date
sensor. This makes it easier to trigger at the exact same time of the Azan.
Since the Azan API provides additional important information. I am extracting the added information into multiple sensors like Islamic date.
- platform: time_date
display_options:
- 'time'
- 'date'
- 'date_time'
- 'date_time_utc'
- 'date_time_iso'
- 'time_date'
- 'time_utc'
- 'beat'
- platform: rest
name: Prayer Times
resource_template: 'http://api.aladhan.com/v1/timings/:date_or_timestamp?latitude=45.668399&longitude=-73.944645&method=0&midnightMode=1&adjustment={{ states.input_select.days_offset_islamic_date.state }}'
json_attributes:
- data
value_template: '{{ value_json.status }}'
scan_interval: 10800 # every three hours
- platform: template
sensors:
islamic_day:
friendly_name: Islamic Day
value_template: >
{% if states.sensor.prayer_times.state not in ['OK'] %}
{{ states.sensor.islamic_day.state }}
{% else %}
{{ states.sensor.prayer_times.attributes["data"]["date"]["hijri"]["day"] }}
{% endif %}
islamic_month:
friendly_name: Islamic Month
value_template: >
{% if states.sensor.prayer_times.state not in ['OK'] %}
{{ states.sensor.islamic_month.state }}
{% else %}
{{ states.sensor.prayer_times.attributes["data"]["date"]["hijri"]["month"]["en"] }}
{% endif %}
islamic_year:
friendly_name: Islamic Year
value_template: >
{% if states.sensor.prayer_times.state not in ['OK'] %}
{{ states.sensor.islamic_year.state }}
{% else %}
{{ states.sensor.prayer_times.attributes["data"]["date"]["hijri"]["year"] }}
{% endif %}
islamic_date:
friendly_name: Islamic Date
value_template: >
{{ states.sensor.islamic_day.state }} {{ states.sensor.islamic_month.state }} {{ states.sensor.islamic_year.state }}
sunrise:
friendly_name: Sunrise Time
value_template: >
{% if states.sensor.prayer_times.state not in ['OK'] %}
{{ states.sensor.sunrise.state }}
{% else %}
{{ states.sensor.prayer_times.attributes["data"]["timings"]["Sunrise"] }}
{% endif %}
imsak:
friendly_name: Imsak Time
value_template: >
{% if states.sensor.prayer_times.state not in ['OK'] %}
{{ states.sensor.imsak.state }}
{% else %}
{{ states.sensor.prayer_times.attributes["data"]["timings"]["Imsak"] }}
{% endif %}
fajr:
friendly_name: Fajr Prayer Time
value_template: >
{% if states.sensor.prayer_times.state not in ['OK'] %}
{{ states.sensor.fajr.state }}
{% else %}
{{ states.sensor.prayer_times.attributes["data"]["timings"]["Fajr"] }}
{% endif %}
zhuhr:
friendly_name: Zhuhr Prayer Time
value_template: >
{% if states.sensor.prayer_times.state not in ['OK'] %}
{{ states.sensor.zhuhr.state }}
{% else %}
{{ states.sensor.prayer_times.attributes["data"]["timings"]["Dhuhr"] }}
{% endif %}
asr:
friendly_name: Asr Prayer Time
value_template: >
{% if states.sensor.prayer_times.state not in ['OK'] %}
{{ states.sensor.asr.state }}
{% else %}
{{ states.sensor.prayer_times.attributes["data"]["timings"]["Asr"] }}
{% endif %}
sunset:
friendly_name: Sunset Time
value_template: >
{% if states.sensor.prayer_times.state not in ['OK'] %}
{{ states.sensor.sunset.state }}
{% else %}
{{ states.sensor.prayer_times.attributes["data"]["timings"]["Sunset"] }}
{% endif %}
maghrib:
friendly_name: Maghrib Prayer Time
value_template: >
{% if states.sensor.prayer_times.state not in ['OK'] %}
{{ states.sensor.maghrib.state }}
{% else %}
{{ states.sensor.prayer_times.attributes["data"]["timings"]["Maghrib"] }}
{% endif %}
isha:
friendly_name: Isha Prayer Time
value_template: >
{% if states.sensor.prayer_times.state not in ['OK'] %}
{{ states.sensor.isha.state }}
{% else %}
{{ states.sensor.prayer_times.attributes["data"]["timings"]["Isha"] }}
{% endif %}
midnight:
friendly_name: Midnight Time
value_template: >
{% if states.sensor.prayer_times.state not in ['OK'] %}
{{ states.sensor.midnight.state }}
{% else %}
{{ states.sensor.prayer_times.attributes["data"]["timings"]["Midnight"] }}
{% endif %}
alias: 'Play Maghrib Azan '
description: ''
trigger:
- platform: template
value_template: '{{ states(''sensor.time'') == states.sensor.maghrib.state }}'
condition: []
action:
- service: media_player.volume_set
data_template:
entity_id: media_player.azan_speakers
volume_level: '{{states.input_number.azan_volume_maghrib.state}}'
- delay:
seconds: 2
- service: media_player.play_media
data_template:
entity_id: media_player.azan_speakers
media_content_id: >-
http://192.168.4.145:8123/local/{{states.input_select.azan_select_maghrib.state.split('
')[0]}}
media_content_type: audio/mp3
mode: single
@syedair
Thank you for replying.
I am running the latest version of HomeAssistant 2021.12.7 I was able to trigger the automation manually. I did not have the volume-level hard-coded to 1 (although triggered manually played fine)
I was able to get another version of this from the HA forums working on my home assistant server. I will yours a give it a try later if something breaks.
Thank you for your help!
I have reopened the issue because I had to switch back to this version, as the Islamic_Prayer_Times does not have the possibility to change the school method from the standard one to Hanafi which means my Asr azans were a lot earlier than it needed to be. I have reconfigured the settings as mentioned above and tested them.
Next Azan is due in 1 hour approx, I will let you know if it was triggered automatically or not.
@syedair didn't trigger automatically again. Plays fine if triggered manually
If you are on the latest home assistant, it gives you the ability to debug the last automation run. It shows how it was triggered and which conditions it met, etc. sharing the snapshot where you can find this.
It would also be helpful to enable debug logs and share those here, so I can better guide you.
Cannot see the same options as yours. It gives me an error "Only automation with unique IDs are debuggable"
As a next step, I recommend you to create an automation using the UI. You can paste the new automation YAML code that I shared before by changing the mode to YAML (don’t forget to make modifications as per your system).
Hi, So some Azan's are playing while others are not. from the above screenshot I sent, you can see that Fajr Azan was triggered, and Asr was also triggered. but Dhuhr, Magrib & Isha were not triggered. Dhuhr I saw there was a mistake in my code and I've fixed it. but there is no mistake in the other, for some reason Asr Azan was played at the correct time but Magrib didn't play.
I fixed the error regarding the debugging by creating id:
for each of the automation and that has given me the debug options.
But since it's not triggered, it is not showing debugging information anyways
That’s great news.
Would you also share the code for the automation that’s working? So I can spot the difference.
I also recommend you use my latest improvements.
@syedair below is my configuration currently, The following automations are playing, while others are not.
Configuration.yaml
homeassistant:
latitude: 19.2677893
longitude: 73.0823755
elevation: 21
unit_system: metric
time_zone: Asia/Kolkata
# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:
discovery:
media_extractor:
sun:
updater:
# Text to speech
tts:
- platform: google_translate
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include sensors.yaml
mqtt:
discovery: true
discovery_prefix: homeassistant
broker: 192.168.192.31
port: 1883
client_id: home-assistant-1
keepalive: 60
username: xx
password: xxxx
http:
use_x_forwarded_for: true
trusted_proxies:
- xxxxxx\24
Automations.yaml
- alias: Fajr Azan
id: 8eb93d80-6ad3-11ec-90d6-0242ac120003
trigger:
- platform: time_pattern
seconds: "30"
condition:
- condition: template
value_template: '{{ now().time().strftime("%H:%M") == states.sensor.fajr.state }}'
action:
- service: media_player.volume_set
data:
entity_id: media_player.living_room_speaker
volume_level: "1"
- service: media_player.play_media
data:
entity_id: media_player.living_room_speaker
media_content_id: https://www.islamcan.com/audio/adhan/azan2.mp3
media_content_type: audio/mp3
- alias: Dhuhr Azan
id: 8eb93f60-6ad3-11ec-90d6-0242ac120003
trigger:
- platform: time_pattern
seconds: "30"
condition:
- condition: template
value_template: '{{ now().time().strftime("%H:%M") == states.sensor.dhuhr.state }}'
action:
- service: media_player.volume_set
data:
entity_id: media_player.living_room_speaker
volume_level: "1"
- service: media_player.play_media
data:
entity_id: media_player.living_room_speaker
media_content_id: https://www.islamcan.com/audio/adhan/azan2.mp3
media_content_type: audio/mp3
- alias: Asr Azan
id: 8eb94096-6ad3-11ec-90d6-0242ac120003
trigger:
- platform: time_pattern
seconds: "30"
condition:
- condition: template
value_template: '{{ now().time().strftime("%H:%M") == states.sensor.asr.state }}'
action:
- service: media_player.volume_set
data:
entity_id: media_player.living_room_speaker
volume_level: "1"
- service: media_player.play_media
data:
entity_id: media_player.living_room_speaker
media_content_id: https://www.islamcan.com/audio/adhan/azan2.mp3
media_content_type: audio/mp3
- alias: Magrib Azan
id: 8eb941b8-6ad3-11ec-90d6-0242ac120003
trigger:
- platform: time_pattern
seconds: "30"
condition:
- condition: template
value_template: '{{ now().time().strftime("%H:%M") == states.sensor.magrib.state }}'
action:
- service: media_player.volume_set
data:
entity_id: media_player.living_room_speaker
volume_level: "1"
- service: media_player.play_media
data:
entity_id: media_player.living_room_speaker
media_content_id: https://www.islamcan.com/audio/adhan/azan2.mp3
media_content_type: audio/mp3
- alias: Isha Azan
id: 8eb942da-6ad3-11ec-90d6-0242ac120003
trigger:
- platform: time_pattern
seconds: "30"
condition:
- condition: template
value_template: '{{ now().time().strftime("%H:%M") == states.sensor.isha.state }}'
action:
- service: media_player.volume_set
data:
entity_id: media_player.living_room_speaker
volume_level: "1"
- service: media_player.play_media
data:
entity_id: media_player.living_room_speaker
media_content_id: https://www.islamcan.com/audio/adhan/azan2.mp3
media_content_type: audio/mp3
Sensors.yaml
- platform: time_date
display_options:
- "time"
- "date"
- "date_time"
- "date_time_utc"
- "date_time_iso"
- "time_date"
- "time_utc"
- "beat"
- platform: rest
name: Azan Times
resource_template: "http://api.aladhan.com/v1/timings/:date_or_timestamp?latitude=19.2677893&longitude=73.0823755&method=1&midnightMode=0&school=1"
json_attributes:
- data
value_template: "{{ value_json.status }}"
scan_interval: 3000
- platform: template
sensors:
sunrise:
friendly_name: Sunrise
value_template: '{{ states.sensor.azan_times.attributes["data"]["timings"]["Sunrise"] }}'
fajr:
friendly_name: Fajr
value_template: '{{ states.sensor.azan_times.attributes["data"]["timings"]["Fajr"] }}'
dhuhur:
friendly_name: Dhuhr
value_template: '{{ states.sensor.azan_times.attributes["data"]["timings"]["Dhuhr"] }}'
asr:
friendly_name: Asr
value_template: '{{ states.sensor.azan_times.attributes["data"]["timings"]["Asr"] }}'
sunset:
friendly_name: Sunset
value_template: '{{ states.sensor.azan_times.attributes["data"]["timings"]["Sunset"] }}'
maghrib:
friendly_name: Maghrib
value_template: '{{ states.sensor.azan_times.attributes["data"]["timings"]["Maghrib"] }}'
isha:
friendly_name: Isha
value_template: '{{ states.sensor.azan_times.attributes["data"]["timings"]["Isha"] }}'
midnight:
friendly_name: Midnight
value_template: '{{ states.sensor.azan_times.attributes["data"]["timings"]["Midnight"] }}'
I also recommend you use my latest improvements.
I would like to get the basics working first then I can play around with other items... as my only goal is to get the correct prayer times playing on my speaker as there is no Azan in my area.
I think I found an issue with Magrib automation.
You need to change the following line:
value_template: '{{ now().time().strftime("%H:%M") == states.sensor.magrib.state }}'
with this:
value_template: '{{ now().time().strftime("%H:%M") == states.sensor.maghrib.state }}'
Note the sensor name should match with what you have configured.
To test other templates, you can use Home Assistant Template section under Developer Tools/Template in the UI:
@syedair Ok I see the issue there with maghrib but what about Dhuhr or Asr or Isha New error I see
Let’s solve it step-wise. So now Maghrib azan is working for you?
For others, I don’t find any issues in your config. I can’t compare it with mine as I have made a lot of improvements in the latest code, I shared with you. Which I still recommend you use, instead of this old version.
Did you try using the template page on the UI to read the values from the sensors?
I am not at home anymore, will be back next week. will let you know if it works or not.
Ack. If I get time, I will convert based on your requirements the latest code, so it will be a simple copy and paste.
thanks, I just checked these remotely, although I didn't hear the azan as I am not at home but the trigger times are correct and only 2 azan's are not playing
Perfect. This explains it. Magrib and Dhuhr automation has typos in condition block:
For Maghrib, you need to change the following line:
value_template: '{{ now().time().strftime("%H:%M") == states.sensor.magrib.state }}'
with this:
value_template: '{{ now().time().strftime("%H:%M") == states.sensor.maghrib.state }}'
——
For Dhuhr, you need to change the following line:
value_template: '{{ now().time().strftime("%H:%M") == states.sensor.dhuhr.state }}'
with this:
value_template: '{{ now().time().strftime("%H:%M") == states.sensor.dhuhur.state }}'
I hope this resolves your issue.
@syedair Thank You very much, after the changes everything is working properly, All Azan's are triggering at the correct time.
@aasimenator perfect. Please keep me in duas for me and my parents.
Assalamuwalaikum,
I used your code in my HomeAssistant to get things working. as far as I know, everything works when I manually trigger the automation from the automation dashboard below.
But if I don't trigger it manually it is not starting automatically, is there any reason for this to happen. The only changes I've made to your code is not to include the input_select.yaml file as I have set it to play directly from the internet. rest everything is the same sensors.yaml
automations.yaml