8 m

Vintage wooden radio clockThere is nothing worse than waking up to an annoying, boring sound that repeats itself over and over again. That’s why I like to wake up with my favorite music. I also don’t like telling my speaker what I want to hear when I wake up, every time I set an alarm. Setting an alarm for every day has its own down sides, like an on going alarm early in the morning, because I’m not home and I forgot to turn it off. It was time to build my own smart alarm clock. Here is how I did it.

My smart alarm clock does multiple things. First it is reliable and effective. Sleeping through a self-made alarm clock is no excuse to arrive late at work. Second, it plays my favorite music to wake me up. Third, the alar only sounds when I am at home and asleep. Fourth, there is a different alarm time for workdays and weekends. Lastly, there are 2 easy switches to turn off the weekend and workday alarms separately from each other, including a reminder to turn them back on after I switched one or both off.

One small thing before you start building this for yourself: I choose to create a audible reminder on my Amazon Echo speaker. Therefore, I needed some extra elements to accomplish that. You may have other preferences. I tried to explain why I created every single thing, so you can change this alarm clock idea easily to fit your own needs.

I used the following elements for this smart alarm clock:

  • 2 workday sensors; one for the actual alarm and one for the reminder
  • 2 input booleans; one to trigger the alarm and one for the reminders
  • 2 input datetime helpers to set the time for the alarm
  • 5 automations; two to trigger the actual alarm at the right time, one to turn off the actual alarm and two for the reminders
  • 2 template switches; one for each alarm
  • 3 binary sensors; one to trigger the music when the alarm goes off and the others for the reminders
  • 1 nice playlist with music

First, I added two workday binary sensors to my configuration file. I needed two in order to create the reminders at a later time. You can copy my configuration below, but you may have other workdays of live in another country. If so, please check the configuration variables here. (Please note that the sensor for tomorrow has an offset of one day.)

# Workday binary sensors for alarm clock example
binary_sensor:
  - platform: workday
    country: NL
    name: workday_today
  - platform: workday
    country: NL
    name: workday_tomorrow
    days_offset: 1

Next, I created two input booleans. One to trigger the actual alarm (I called it “Alarm”) and one to get a more accurate offset for the workday_tomorrow sensor (I called it “Workday 18hrs”). The second boolean is needed for the reminders. You can create your own on the configuration > Helpers page in Home Assistant.

I also created two input datetime helpers, which can be created at the same place were you created the input booleans. I created one for workdays and one for weekends. Important note: be aware of the fact that these input datetime’s default value is at midnight (00:00:00). Forget to reset these helpers to your preferred wakeup time will result in a sleepless night. 😉

After that I created two automations, which will actually trigger the alarm. One for the workday alarm and one for the weekend alarm. One of the two differences between these automations is the workday_today sensor in the condition section. For workday alarms, it needs to be on and for weekends it needs to be off. The other difference is the time on which the automation will be triggered by the input datetime. My workday alarm automation looks as follows:

- alias: Workday alarm
  description: ''
  trigger:
  - at: input_datetime.workday_alarm
    platform: time
  condition:
  - condition: state
    entity_id: binary_sensor.workday_today
    state: 'on'
  - condition: state
    entity_id: input_boolean.night_mode
    state: 'on'
  - condition: state
    entity_id: input_boolean.away_mode
    state: 'off'
  action:
  - data: {}
    entity_id: input_boolean.alarm
    service: input_boolean.turn_on
  mode: single

I also created an automation to turn off the actual alarm after I woke up. I used the night mode as trigger. This way, when the night mode turns off after I say ‘good morning’ to one of my smart speakers, the alarm also turns off.

- alias: Turn off alarm
  description: ''
  trigger:
  - entity_id: input_boolean.night_mode
    from: 'on'
    platform: state
    to: 'off'
  condition:
  - condition: state
    entity_id: input_boolean.alarm
    state: 'on'
  action:
  - data: {}
    entity_id: input_boolean.alarm
    service: input_boolean.turn_off
  mode: single

I created the switches to toggle the alarm next. I used template switches to toggle the automations on or off. By doing so, I didn’t need unnecessary booleans. I created these switches in my configuration file. The switch for my workday alarm is shown below:

# Alarm switch example
switch:
  - platform: template
    switches:
      alarm_workday:
        friendly_name: "Workday alarms"
        value_template: "{{ is_state('automation.workday_alarm', 'on') }}"
        turn_on:
          service: automation.turn_on
          data:
            entity_id: automation.workday_alarm
        turn_off:
          service: automation.turn_off
          data:
            entity_id: automation.workday_alarm

At this point, the input boolean ‘Alarm’ turns on at the right time and only when I am home and asleep. Now I needed the boolean to start playing music on my Amazon Echo speaker. To accomplish that, I created one binary motion sensor which mirrors the ‘Alarm’ boolean. When the input boolean turns on, the binary sensor turns off and also the other way around. I did this, because the trigger options in an Alexa routine when a motion sensor stops detecting motion are far more detailed than when a sensor detects motion. For example: I can create multiple routines. The first for when the sensor stops detecting motion; which will start playing music. The second routines gets triggered when the sensor detects no motion for 5 minutes and increases the volume. This can only be done when a sensor stops detecting motion. I also added a turn off delay to prevent the alarm from ringing when I, for example, restart Home Assistant. The configuration is as follows:

# Binary alarm sensor example
binary_sensor:
  - platform: template
    sensors:
      wake_up_alarm:
        friendly_name: "Wake up alarm sensor"
        delay_off:
          seconds: 20
        device_class: motion
        value_template: "{{ is_state('input_boolean.alarm', 'off') }}"

I restarted Home Assistant after doing all this and created my Alexa routines. As I may do a suggestion; I created a playlist on Spotify with nice songs for waking up. You can find it below.

Reminders

Now, it’s time for the reminder; in case I forget to toggle the alarm switches back on. For this to work, I needed to know if the next day is a workday or not, so I can trigger the right warning at the right time. There is only one major issue with the sensor ‘workday_tomorrow’ which I created earlier. To be honest, it took me some time and creativity to solve it, but I did eventually!

The issue is as follows: I wanted to trigger the warning a few seconds after I turn on night mode and when the alarm for the next day is turned off. The sensor ‘workday_tomorrow’ tells me if the next day is a workday, so it will do fine before midnight. When I go to bed after midnight, this sensor gives me a reading for the wrong day. What I needed was an offset of less than one day. Sadly, it isn’t possible to do this in the configuration of the workday sensor, so I had to find another way around.

The solution is the input boolean ‘workday_18hrs’. I created two automations which triggers at 06:00 in the morning. They copy the state of the ‘workday_tomorrow’ sensor, but some hours later. This way, the input boolean ‘workday_18hrs’ still gives me correct information about the next day, even if I go to bed after midnight. I improved this basic idea by adding the night mode as trigger and condition. This was needed, otherwise I woke up from a warning for the next day at 06:00, even I turned off my alarm! Long story short: the automation for when the next day is a workday is below.

- alias: Workday tomorrow 18hrs on
  description: ''
  trigger:
  - at: 06:00:00
    platform: time
  - entity_id: input_boolean.night_mode
    for: 00:01:00
    from: 'on'
    platform: state
    to: 'off'
  condition:
  - condition: state
    entity_id: binary_sensor.workday_tomorrow
    state: 'on'
  - condition: state
    entity_id: input_boolean.night_mode
    state: 'off'
  action:
  - data: {}
    entity_id: input_boolean.workday_18hrs
    service: input_boolean.turn_on
  mode: single

I did the same for the weekend, but than the ‘workday_tomorrow’ will be off and the action will turn off the input boolean ‘workday_18hrs’.

What’s left is the sensor that triggers the audible warning on my Echo speaker after I went to bed. I created a binary motion sensor, which turns on when night mode is on, the automation which triggers the actual alarm is off and the ‘workday_18hrs’ is on. I also added a few seconds turn on delay to prevent interference with my good night routine. This looks as follows:

# Binary alarm off reminder sensor example
binary_sensor:
  - platform: template
    sensors:
      alarm_workday_off_reminder:
        friendly_name: "Workday alarm off reminder"
        delay_on:
          seconds: 15
        device_class: motion
        value_template: >-
          {{ is_state('input_boolean.workday_18hrs', 'on')
             and is_state('input_boolean.night_mode', 'on')
             and is_state('automation.workday_alarm', 'off') }}

And that’s it! I created a routine for the workday alarm reminder as well as for the weekend alarm reminder to trigger an audible notification after I went to bed. Of course, you can use a notification on your phone to do the same thing, but since I am trying to charge my phone outside my bedroom, that won’t work. Obviously, your automation will be different when you are using a Google Cast device.

Lovelase card with alarm settings

Screenshot of the Lovelace dashboard. On the bottom is a card shown with settings for the alarm clockWhat’s left is an easy method to toggle the alarms and to set the time on which the alarm will be triggered. I tried many different solutions, but the one below really stands out. I created a card on my dashboard which let me turn on and of the alarms, but also let me change the time. I used a horizontal stack card for this, so the two alarms can be placed next to each other. On this horizontal stack card, I placed two entities cards; one for each alarm. On each of those entities cards, I placed the input datetime and the switch for that specific alarm. The result is shown in the screenshot on the left. The example code is below and I translated the terms to English for you.

- type: horizontal-stack
  cards:
    - type: entities
      entities:
        - entity: input_datetime.workday_alarm
          name: Time
          icon: 'mdi:clock'
        - entity: switch.alarm_workday
          name: Active
          icon: 'mdi:alarm'
      title: Workday alarm
    - type: entities
      entities:
        - entity: input_datetime.weekend_alarm
          name: Time
          icon: 'mdi:clock'
        - entity: switch.alarm_weekend
          name: Active
          icon: 'mdi:alarm'
      title: Weekend alarm

And that’s it! Feel free to improve and change my idea; I am looking forward to your ideas.

I updated this article on November 11, 2020, to include the use of input datetime helpers. I am working on a simple snooze action. It’s on the drawing board and as soon as it’s ready, I will post it here.