sdgsdg
(Chris)
August 18, 2021, 11:55am
1
Hi.
I have 12 different sounds stored locally on my home assistant that I want to play depending on the hour of the day.
I.e. 01:00 and 13:00 play file one.wav
I.e. 02:00 and 14:00 play file two.wav
all the way to twelve. What is the best way to script this as I do not want to create individual automations for each hour. Thank you.
I’m not sure if this is the most efficient, but you can create one automation with a trigger every hour having a specified trigger id and perform a play action based on each of the triggers.
sdgsdg
(Chris)
August 18, 2021, 12:17pm
4
I found a solution!
I created a script with this text:
service: media_player.play_media
target:
device_id: [YOUR OWN DEVICE]
data:
media_content_id: >
https://[Your own external URL]:8123/local/sounds/{{now().strftime("%I")}}-hour.wav
media_content_type: music
In my sound folder I have the twelve files named 01-hour.wav, 02-hour.wav etc. It seems to work for now at least.
123
(Taras)
August 18, 2021, 12:18pm
5
Create an automation with a Time Pattern Trigger set to trigger every hour.
Name your sound files 1.wav,
2.wav,
3.wav, etc up to 11.wav
and another named 0.wav
to represent noon and midnight (i.e. 12 chimes).
Use this template to compute which one of the 12 wav files should be played when the automation is triggered:
{{ now().hour % 12 }}.wav
Easy-peasy.
1 Like
sdgsdg
(Chris)
August 18, 2021, 12:18pm
6
Thanks @KingRichard this is what I based my solution on. For some reason, it didn’t come up when I tried to search initially.
123
(Taras)
August 18, 2021, 12:26pm
7
Either one of these will work and will exclude the leading zero to avoid having to zero-pad the file name:
https://[Your own external URL]:8123/local/sounds/{{now().hour % 12}}-hour.wav
https://[Your own external URL]:8123/local/sounds/{{now().strftime("%-I")}}-hour.wav
It’s the hyphen in %-I
that eliminates zero-padding a single-digit hour.
The main difference between the two is that the first one represents noon and midnight as 0
hour.
1 Like