A Cuckoo Clock, but Digital - Cuckoo in Home Assistant
Posted on February 20, 2024 • 4 min read • 744 words
Since I absolutely detest the ticking of clocks, a cuckoo clock is not welcome in my house. That sound of gears turning, or even worse, the ticking away of every second, brrr. Fortunately, this is a cuckoo clock that can be switched off, as I’ve built it into Home Assistant. The clock has been cuckooing to my satisfaction for about five years now, without the need to wind it up.
The clock itself
The idea of having a cuckoo clock in your smart home is something you can find in various places on the internet. I cannot claim that I came up with the idea myself. I also adopted it from another Home Assistant user, namely from vCloudInfo .
What the clock does is sound a cuckoo every half hour and chime the number of hours it is on the hour. So, at 4 PM, the clock goes off 4 times. I considered having it chime 16 times in a 24-hour rhythm, but that was a bit too much for me.
The clock is triggered every half hour.
trigger:
- platform: time_pattern
minutes: "0"
- platform: time_pattern
minutes: "30"But only if this happens at times when we are awake (hopefully) and no music is playing in the kitchen yet.
condition:
- condition: time
after: "06:59:00"
before: "21:30:00"
- condition: not
conditions:
- condition: state
entity_id: media_player.kitchen
state: playingSubsequently, it is sent to the HomePod minis in the kitchen and the bedroom.
action:
- service: media_player.play_media
data:
entity_id:
- media_player.kitchen
- media_player.bedroomWhat is sent depends on the time. When it is half past the hour, the same file that is sent at one o’clock is used. At all other times, there is a specific audio file with the number of cuckoos. You could also create a loop and then use only one audio file. What you then miss, which is characteristic of the cuckoo clock, is the little door that opens and closes at the end. This is precisely what makes it feel so ‘real’.
media_content_id: |
{% if now().strftime("%M")|int == 30 %}
/config/www/audio/cuckoo-1.wav
{% else %}
{% if (now().strftime("%H")|int == 17) and (is_state("binary_sensor.workday_sensor", "on"))%}
/config/www/audio/five-o-clock.wav
{% else %}
/config/www/audio/cuckoo-{{now().strftime("%I")}}.wav
{% endif %}
{% endif %}
media_content_type: audio/wavThis cuckoo clock has a little twist. Since my lovely spouse had the tendency to continue working past five o’clock while working from home, a different sound plays at five o’clock, specifically that of an old-fashioned steam whistle. This makes it clear: End of the workday, at least for her.
Improvements
Now that I’m looking at the script again after so much time, I think: “Hmm, there are still some points for improvement,” but don’t forget that the clock has been active for five years already. My knowledge of Home Assistant has also increased in those five years. I might make different choices now.
One of those choices would be not to use fixed values in the automation itself, but to use the output of other scripts. Another option would be to use helpers, which you can change from the frontend. This allows a household user to easily adjust certain things themselves, instead of having to tamper with the automations. In practice, this means that I get asked to make adjustments. That is obviously not the intention of automating.
In the script below, I would at least make the before and after conditions configurable at the frontend. For example, I could use the same helper that is also used in our sunrise simulation. How great would that be: Being woken by the sunlight while listening to the cuckoo clock, instead of some super annoying alarm clock. I could also use other variables or sensors, such as It's morning and no phones are charging anymore, or The house is in day mode, or Someone is at home. I also want to differentiate where someone is in the house, so that the cuckoo clock can only be heard there.
As you can see, there’s still plenty to do.
The automation
alias: Cuckoo
trigger:
- platform: time_pattern
minutes: "0"
- platform: time_pattern
minutes: "30"
condition:
- condition: time
after: "06:59:00"
before: "21:30:00"
- condition: not
conditions:
- condition: state
entity_id: media_player.kitchen
state: playing
action:
- service: media_player.play_media
data:
entity_id:
- media_player.kitchen
- media_player.bedroom
media_content_id: |
{% if now().strftime("%M")|int == 30 %}
/config/www/audio/cuckoo-1.wav
{% else %}
{% if (now().strftime("%H")|int == 17) and (is_state("binary_sensor.workday_sensor", "on"))%}
/config/www/audio/five-o-clock.wav
{% else %}
/config/www/audio/cuckoo-{{now().strftime("%I")}}.wav
{% endif %}
{% endif %}
media_content_type: audio/wav
mode: restart

