Alarm Clock as a package

I was looking for an alarm clock and was slightly surprised to not find a built in version, as I would have thought this would be one of the most used things when you start going into the world of smart home.

I found this Creating an Alarm Clock Updated! very cool post but that was a lot of code in my configuration.yaml. I also wanted some reusable code, as I need more than one alarm clock. So I encapsulated the code I foundin this post into a more generic and reusable package.

This is how it looks (I added the alarm to a group along the temperate I would like to have my room to be heated up):

Note: This solution is using packages. They are not yet supported by the automation editor or group reload. So if you reload groups the groups you define in packages will disappear (same with automations)

In the home assistant section of configuration.yaml:

homeassistant:
   packages:
       alarm_1: !include alarm_1.yaml
       alarm_2: !include alarm_2.yaml

The alarm.yaml’s are designed that you never really need to touch them. For a second alarm, copy the “alarm_1.yaml”, search and replace “alarm_1” with “alarm_2” , and finally add one line to the configuration yaml. Done. The alarm fires an event “alarm_1_event” which you can pick up in an automation of your choice.

input_number:
    alarm_1_hour:
        name: Hours
        icon: mdi:timer
        initial: 6
        min: 0
        max: 23
        step: 1
    alarm_1_minutes:
        name: Minutes
        icon: mdi:timer
        initial: 30
        min: 0
        max: 59
        step: 1

input_boolean:
    alarm_1_weekday:
        name: Weekdays
        initial: off
        icon: mdi:calendar
    alarm_1_weekend:
        name: Weekends
    initial: off
    icon: mdi:calendar

sensor:
    - platform: template
      sensors:
         alarm_1_time:
              friendly_name: 'Time'
              value_template: >-
                  {{ "%0.02d:%0.02d" | format(states("input_number.alarm_1_hour") | int, states("input_number.alarm_1_minutes") | int) }}

group:
    alarm_1:
        name: Wake Me Up
        entities:
            - sensor.alarm_1_time
            - input_number.alarm_1_hour
            - input_number.alarm_1_minutes
            - input_boolean.alarm_1_weekday
            - input_boolean.alarm_1_weekend


automation:
    - id: alarm_1_weekdays
      alias: "Wake me up (week)"
      trigger:
          - platform: time
            minutes: '/1'
            seconds: 0
      condition:
          - condition: state
            entity_id: input_boolean.alarm_1_weekday
            state: 'on'
          - condition: time
            weekday:
                - mon
                - tue
                - wed
                - thu
                - fri
          - condition: template
            value_template: >-
                {{ now().strftime("%H:%M") == states.sensor.alarm_1_time.state }}
      action:
          - event: alarm_1_event
            event_data:
                name: Alarm 1 Triggered
                type: Weekdays

    - id: alarm_1_weekend
      alias: "Wake me up (weekend)"
      trigger:
          - platform: time
            minutes: '/1'
            seconds: 0
      condition:
          - condition: state
            entity_id: input_boolean.alarm_1_weekend
            state: 'on'
          - condition: time
            weekday:
                - sat
                - sun
          - condition: template
            value_template: >-
                {{ now().strftime("%H:%M") == states.sensor.alarm_1_time.state }}

      action:
          event: alarm_1_event
          event_data:
              name: Alarm 1 Triggered
              type: Weekends
15 Likes

I use a similar package setup but use input_datetime to reduce the code right down and make it a bit less hacky for WAF. I also fire the actual automation in the package as mine are packaged for the rooms they’re in. This is the bedroom one


link removed

The other rooms are in the parent directory, there’s 4 of them in the house, and then I have a “sync alarms” package that sets the time and activates the alarms for all 4


link removed

If that’s any use to anyone.

3 Likes

Yeah, packages are a nice feature, but development go’s to Gui. :disappointed_relieved:

2 Likes

I agree with your post in that issue thread. Packages are the best way to organise your configuration.

I totally understand that the devs want to make things more accessible to non-techie types, but it’s starting to feel a little bit like those of us that are happy to do our own config are just being pushed to the back bench since hass.io was released really.

So long as the option to manually configure never goes and the docs continue to show what you can do with each component I’m not too fussed, I always restart my whole instance each time anyway so the issue you raised doesn’t affect me, but you won’t be the only ‘regular’ user who wants this and to say, basically “shove off, we want to be noob friendly now so in spite of your contribution to the community we’re not interested in helping you” is a bit off, and that’s one of several GitHub issues I’ve seen go that way recently which is a bit upsetting really.

Plus, packages are what the community is trying to put together to actually make it noob friendly. “Copy and paste this and you have an alarm clock” is always going to be easier than making your own no matter how clever the gui editor gets.

Anyway, I’m well off topic, so


2 Likes

Did not know that they have datetime as input type by now. I played with it, but ended up not using it, as one cannot cascade groups. I have my generic alarm group in a group with all the things that should happen (that is in my case set temperature, time how long it should stay warm, etc). So the changing of the alarm settings will be displayed in an overlay. Once this is open you will not be able to open another one on top of this. Changing datetime however opens another overlay, or at least tries to, and will fail to do so.

It is always easy to say from the outside, but there are a lot of design decisions which I would not have made this way and which make it really hard to debug automations and get things to run. Given the time I spend on debugging and the state of HA, I find it really ambitious to make it “accessible for non-techie types”. But again it’s just an outside view and I’ll take what I can get.

1 Like

I decided after all that it is easier and much shorter to move the automation to appdaemon. The actual automation can then be written in 2-3 lines.

Note: It is currently for appdaemon v2.x. For v3 use hass instead of appapi (comments below)

#import appdaemon.plugins.hass.hassapi as hass
import appdaemon.appapi as appapi

from datetime import datetime

#class WatchAlarm(hass.Hass):
class WatchAlarm(appapi.AppDaemon):

    def initialize(self):
        self.run_every(self.watchAlarm, datetime.now(), 60)

    def watchAlarm(self, kwargs):
        ifWeek = self.get_state(self.args["ifWeek"])
        ifWeekend = self.get_state(self.args["ifWeekend"])
        time = self.get_state(self.args["time"])
        time = datetime.strptime(time, '%H:%M:%S')
        eventName = self.args["eventName"]

        now = datetime.now()
        if (now.isoweekday() <= 5 and ifWeek) or (now.isoweekday() >= 6 and ifWeekend):
                if time.hour == now.hour and time.minute == now.minute:
                    self.log("It is now {0:%H:%M} and we fire {1}".format(time, eventName))
                    self.fire_event(eventName)
2 Likes

Ehh I wish it could be less techie


I am using your I add incremental music alarm (I use spotify-mopidy)

script:
  bedroom_wake_up_music:
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.master_bedroom_speaker
          volume_level: '0.20'
      - service: media_player.play_media
        data_template:
          entity_id: media_player.mopidy
          media_content_type: 'audio/mp4'
          media_content_id: spotify:user:xxx:playlist:5xxxirFBWSj # buongiorno
      - delay: "00:00:01"
      - service: media_player.play_media
        data_template:
          media_content_id: 'http://192.168.1.239:8000/mopidy.mp3'
          media_content_type: 'audio/mp4'
          entity_id: media_player.master_bedroom_speaker
      - delay: "00:05:00"
      - service: media_player.volume_up
        entity_id: media_player.master_bedroom_speaker
      - delay: "00:05:00"
      - service: media_player.volume_up
        entity_id: media_player.master_bedroom_speaker
      - delay: "00:05:00"
      - service: media_player.volume_up
        entity_id: media_player.master_bedroom_speaker
      - delay: "00:30:00"
      - service: media_player.stop
        entity_id: media_player.master_bedroom_speaker
1 Like

Here is my version of Alarm Clock - https://github.com/skalavala/smarthome/blob/master/packages/alarm_clock.yaml

image

The icons are missing as I am accessing my HA setup from VPN :frowning:

1 Like

@anon43302295 could you please post the link to your package again? thanks!

link removed

The bedroom one is a bit more advanced and wakes us up 15 minutes early in bad weather, the girls and boys ones are just ‘normal’, and the sync-all, ummm, syncs them all to the same time.

1 Like

Ever since my updating to 0.82 somehow my Alarmclock stopped working. Thanks to this post here i could update my alarm clock easily.

I added of course some things. lights coming on slowly before my phones and stuff go off and so on

image

input_number:
    alarm_1_hour:
        name: Hours
        icon: mdi:timer
        initial: 7
        min: 0
        max: 23
        step: 1
    alarm_1_minutes:
        name: Minutes
        icon: mdi:timer
        initial: 0
        min: 0
        max: 59
        step: 1
    alarm_1_offset:
        name: Transition
        icon: mdi:blur-linear
        initial: 15
        min: 0
        max: 60
        step: 15

input_boolean:
    alarm_1_weekday:
        name: Weekdays
        initial: on
        icon: mdi:calendar
    alarm_1_weekend:
        name: Weekends
        initial: off
        icon: mdi:calendar

sensor:
    - platform: time_date
      friendly_name: current_time_for_alarm_1
      display_options:
        - 'time'
    - platform: template
      sensors:
         alarm_1_time:
              friendly_name: 'Alarm Time'
              value_template: >-
                  {{ "%0.02d:%0.02d" | format(states("input_number.alarm_1_hour") | int, states("input_number.alarm_1_minutes") | int) }}
    - platform: template
      sensors:
         alarm_1_time_minus_offset:
              friendly_name: 'Offset Time'
              value_template: >-
                  {{ '%0.02d:%0.02d' | format( ((((states('input_number.alarm_1_hour') | int)*60 + (states('input_number.alarm_1_minutes') | int) - (states('input_number.alarm_1_offset'))| int)/60)| int),(((((((((states('input_number.alarm_1_hour') | int)*60 + (states('input_number.alarm_1_minutes') | int) - (states('input_number.alarm_1_offset')) | int)/60)) - ((((states('input_number.alarm_1_hour') | int)*60 + (states('input_number.alarm_1_minutes') | int) - (states('input_number.alarm_1_offset')) | int)/60)| int))*100) | int)*60/100) | round) ) }}

group:
    alarm_1:
        name: Wake Me Up
        entities:
            - sensor.alarm_1_time
            - sensor.alarm_1_time_minus_offset
            - input_number.alarm_1_hour
            - input_number.alarm_1_minutes
            - input_number.alarm_1_offset
            - input_boolean.alarm_1_weekday
            - input_boolean.alarm_1_weekend


automation:            
- id: alarm_1_weekday
  alias: 'Wake me up (weekday)'
  trigger:
    - platform: time
      minutes: '/1'
      seconds: 00
  condition:
    - condition: state
      entity_id: input_boolean.alarm_1_weekday
      state: 'on'
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri  
    - condition: template
      value_template: "{{ states.sensor.time.state == states.sensor.alarm_1_time_minus_offset.state  }}"
  action:
  #- data:
  #    message: Alarm ! Current time is {{ states.sensor.time.state }} and alarm is set to {{ states.sensor.alarm_1_time.state }} with an offset of {{ states.input_number.alarm_1_offset.state }} set to {{ states.sensor.alarm_1_time_minus_offset.state }} Weekdays ({{ states.input_boolean.alarm_1_weekday.state }} ) and Weekends ({{ states.input_boolean.alarm_1_weekend.state }} ) test {{ "%0.02d:%0.02d" | format(states("input_number.alarm_1_hour") | int, states("input_number.alarm_1_minutes") | int) }}
  #  service: notify.rocketchat
  - data: {}
    service: script.wakeup_sequence
- id: alarm_1_weekend
  alias: 'Wake me up (weekend)'
  trigger:
    - platform: time
      minutes: '/1'
      seconds: 00
  condition:
    - condition: state
      entity_id: input_boolean.alarm_1_weekend
      state: 'on'
    - condition: time
      weekday:
        - sat
        - sun
    - condition: template
      value_template: "{{ states.sensor.time.state == states.sensor.alarm_1_time_minus_offset.state  }}"
  action:
  #- data:
  #    message: Alarm ! Current time is {{ states.sensor.time.state }} and alarm is set to {{ states.sensor.alarm_1_time.state }} with an offset of {{ states.input_number.alarm_1_offset.state }} set to {{ states.sensor.alarm_1_time_minus_offset.state }} Weekdays ({{ states.input_boolean.alarm_1_weekday.state }} ) and Weekends ({{ states.input_boolean.alarm_1_weekend.state }} ) test {{ "%0.02d:%0.02d" | format(states("input_number.alarm_1_hour") | int, states("input_number.alarm_1_minutes") | int) }}
  #  service: notify.rocketchat
  - data: {}
    service: script.wakeup_sequence
1 Like

templates now need entity_id’s included
 check for errors in your log relating to those templates. I’m guessing you will see some. Add the entity_id’s and they should work again

Not sure if i can follow where are entity_ids included?

within each template you now need to include the entity_id that it relates to. You have a few templates in that package. I’m not great with templates but you need to add the entity_id’s under ‘friendly_name’ in each template.

I see, i might add some entity_ids. However the package i posted works perfectly fine. thanks tho

So it does work? I have a few templates which were giving entity_id errors but I think they may have still worked
 I didn’t test them, I just added the id’s and the errors stopped

Sorry for the misunderstanding, I thought my sentence afterwards showed that I fixed my alarm clock thanks to this forum topic.

Thanks to this post here i could update my alarm clock easily.

My bad, it’s been a very long day