This works well since its based on changing times of the sensors. The automation activates the call for prayers which is played on a Google Chromecast.
The problem is that automation takes about 2 to 3 seconds “after” the clock turns to the minute, which is understandable. The call for prayers is heard about 3 seconds past the actual sensor time. May be because it takes time for the idle Chromecast to start playing the mp3 file.
Is there a script I could use to maybe create alternate sensors which refer to the above time sensors and are 3 seconds early than the time sensors and then I could use the trigger to reference those offset sensors instead?
So if sensor.fajr_prayer has time value of 5:00AM (5:00:00) the script run offset sensor would be sensor.fajr_prayer_offset - 4:59:57AM (4:59:57). Also, the current prayer sensors show time in HH:MM and not HH:MM:SS… so that’s another hurdle.
I am not great at scripting however any direction in this would be highly appreciated.
I had researched and tried that earlier. However in that script the trigger fails as it runs every min and does not take into account any time which is not a whole 60 secs/1 min. So 120 secs/2 mins will trigger however if I tried 90secs/1.5 mins or 30 secs it fails to trigger.
From the link I gave earlier and modified as follows, maybe:
trigger:
- platform: template
value_template: >
{% set f = as_timestamp(states('sensor.fajr_prayer')) - 60 %}
{% set d = as_timestamp(states('sensor.dhuhr_prayer')) - 60 %}
{% set a = as_timestamp(states('sensor.asr_prayer')) - 60 %}
{% set i = as_timestamp(states('sensor.isha_prayer')) - 60 %}
{% set m = as_timestamp(states('sensor.maghrib_prayer')) - 60 %}
{{ now().timestamp() | int in [f, d, a, i, m] }}
Thank you! This changes the trigger.entity_id … correct? What will be the new ids for the 5 times? f, d, a, m and i ?
I had tried that code earlier while experimenting different ways to trigger… the problem with this was the action failed everytime… since it was looking for triger.entity_id. There are two different mp3 files. One plays only for the first morning call for prayer … thats sensor.fajr_prayer. Rest 4 play for the second mp3 file.
Should I change the code to trigger.entity_id == ‘f’ else ‘Adhan’}}.mp3
This worked perfectly. I was even able to consolidate another related automation into this where I turn on the receiver and chrome cast a min before prayer time and power off immediately after the prayer ends.
If you want to play a different sound file based on the prayer time, simply create separate Time Triggers and specify the sound file’s name in a Trigger Variable.
@123 I have added the offset sensors to the config.yaml and wating for the prayer time to test the trigger.
For the new offset sensors, I would like to move them to the sensors.yaml file as I refer that in my config.yaml. Right now my sensors.yaml file looks like below, however I am not able to move the new offset sensors in there and config check is giving me an error.
You can’t do that with the Template Sensor configuration I posted above.
Template Sensors defined in modern format (as opposed to the older “legacy” format) are part of the template domain, not the sensor domain, so you cannot put them in sensors.yaml.
If you have this line in your configuration.yaml file, then you can put them in the templates.yaml file.
I created a template_sensors.yaml file and added the below sensors plus added a unique_id for each so that I can hide the offset sensor from the dashboard.
Yes, I am working on it. For some reason the mp3 did not play for the Asr prayer however other activities in the automation worked properly till the end. Re-doing the media player action… must be some thing with the breaks in the lines. Will definitely report back.
Thanks. That worked when I gave the straight path to the mp3 file. To make the automation less complex I am using the following template which has worked perfectly in the earlier automation. It changes the file based on trigger.entity_id. Of the 5 prayer times only the morning Fajr prayer is a bit different and around 30 secs lengthy. Rest 4 use the same mp3 as they are all the same. So trying to use one trigger for all 5 times.