Grandfather clock

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.

Check out Home-AssistantConfig/CucKoo_Clock.yaml at 9a8e03c0abb7fee5e2d5ee097e85869d73430993 · CCOSTAN/Home-AssistantConfig · GitHub

It works and best…

1 Like

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.

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

Thanks @KingRichard this is what I based my solution on. For some reason, it didn’t come up when I tried to search initially.

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