Creating an alarm clock ! updated


#1

I didn’t find a component to create an alarm clock for my difficult morning, so i implement that.
It’s composed with simple modules :blush:

We need 2 input_slider alarmminutes for minutes and alarmhour for hour.
one sensor to display wake up time from the two slider.
And two input_boolean, alarmweekon to activate/desactivate the alarm, and alarmweekday to setup alarm for the weekdays (if this one is activated only the weekdays are active, if not it will activate for only weekend days).

in the configuration.yaml :

input_slider:
  alarmhour:
    name: Heure
    icon: mdi:timer
    initial: 6
    min: 0
    max: 23
    step: 1
  alarmminutes:
    name: Minutes
    icon: mdi:timer
    initial: 30
    min: 0
    max: 59
    step: 5

input_boolean:
  alarmweekday:
    name: Les jours de la semaine 
    initial: on
    icon: mdi:calendar
  alarmweekon:
    name: Activee     
    initial: on


group:
  alarmclock:
    name: Wake Me Up
    entities: 
      - sensor.alarm_time
      - input_slider.alarmhour
      - input_slider.alarmminutes
      - input_boolean.alarmweekday
      - input_boolean.alarmweekon

in the sensors.yaml :

- platform: template
  sensors:
    alarm_time:
      friendly_name: 'Time'
      value_template: '{{ "%0.02d:%0.02d" | format(states("input_slider.alarmhour") | int, states("input_slider.alarmminutes") | int) }}'

And we need automation to finish that :

  - alias: "Wake me up with bedroom light transition for weekdays"
    trigger:
      - platform: time
        minutes: '/1'
        seconds: 0
    condition:
      - condition: state
        entity_id: input_boolean.alarmweekon
        state: 'on'
      - condition: state
        entity_id: input_boolean.alarmweekday
        state: 'on'
      - condition: time
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
      - condition: template
        value_template: '{{ now().strftime("%H:%M") == states.sensor.alarm_time.state }}'
    action:
      - service: light.turn_on
        entity_id: light.bedroom
        data:
          transition: 600
          brightness: 255
  - alias: "Wake me up with bedroom light transition for weekend days"
    trigger:
      - platform: time
        minutes: '/1'
        seconds: 0
    condition:
      - condition: state
        entity_id: input_boolean.alarmweekon
        state: 'on'
      - condition: state
        entity_id: input_boolean.alarmweekday
        state: 'off'
      - condition: time
        weekday:
          - sat
          - sun
      - condition: template
        value_template: '{{ now().strftime("%H:%M") == states.sensor.alarm_time.state }}'
    action:
      service: light.turn_on
      entity_id: light.bedroom
      data:
        transition: 600
        brightness: 255

Many improvements could be found in digging the original post:


Alarm Clock with Sonos & Philips Hue
Alarm clock component
Creating an iOS Alarm Clock w/ Snooze and Awake Actionable Notifications
Alarm clock component
My personal implementation of Home Assistant
Alarm Clock as a package
Break up configuration.yaml into multiple files
#3

Thanks a lot for your project and description!
Do you know how I could modify it so that it is possible to set the next alarm time via Google Home / IFTTT / Voice?


#4

Thanks for the writeup @hokagegano! I tried adapting your code for my setup but it gave me errors.

Looks like input_slider might be obsolete, replacing it with input_number made it work.


#5

Hi @poldim

you’re right with the use of input_number.
The code updated :

input_number:
  alarmhour:
    name: Heure
    icon: mdi:timer
    initial: 6
    min: 0
    max: 23
    step: 1
    mode: slider
  alarmminutes:
    name: Minutes
    icon: mdi:timer
    initial: 35
    min: 0
    max: 59
    step: 5
    mode: slider

For the group :slight_smile:

  alarmclock:
    name: Wake Me Up
    entities: 
      - input_boolean.freebox
      - sensor.alarm_time
      - input_number.alarmhour
      - input_number.alarmminutes
      - input_boolean.alarmweekday
      - input_boolean.alarmweekon

in sensor.yaml

- platform: template
  sensors:
    alarm_time:
      friendly_name: 'Time'
      value_template: '{{ "%0.02d:%0.02d" | format(states("input_number.alarmhour") | int, states("input_number.alarmminutes") | int) }}'

and for automation

  - alias: "Wake me up with bedroom light transition for weekdays"
    trigger:
      - platform: time
        minutes: '/1'
        seconds: 0
    condition:
      - condition: state
        entity_id: input_boolean.alarmweekon
        state: 'on'
      - condition: state
        entity_id: input_boolean.alarmweekday
        state: 'on'
      - condition: time
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
      - condition: template
        value_template: '{{ now().strftime("%H:%M") == states.sensor.alarm_time.state }}'
    action:
      - service: light.turn_on
        entity_id: light.bedroom
        data:
          transition: 600
          brightness: 255
  - alias: "Wake me up with bedroom light transition for weekend days"
    trigger:
      - platform: time
        minutes: '/1'
        seconds: 0
    condition:
      - condition: state
        entity_id: input_boolean.alarmweekon
        state: 'on'
      - condition: state
        entity_id: input_boolean.alarmweekday
        state: 'off'
      - condition: time
        weekday:
          - sat
          - sun
      - condition: template
        value_template: '{{ now().strftime("%H:%M") == states.sensor.alarm_time.state }}'
    action:
      service: light.turn_on
      entity_id: light.bedroom
      data:
        transition: 600
        brightness: 255

Fire event as action of a generic automation
#6

Thanks @hokagegano for sharing your project!
I am using it successfully but have changed the actions a little bit:

action:
  - service: media_player.volume_set
    data_template:
      entity_id: media_player.schlafzimmer
      volume_level: 0.1
  - service: tts.google_say
    entity_id: media_player.schlafzimmer
    data:
      message: "Guten Morgen. Es ist {{now().hour}} Uhr {{now().minute}}"
      cache: false
  - service: media_player.play_media
    data_template:
      entity_id: media_player.wohnung
      media_content_id: 'http://fluxfm.hoerradar.de/fluxfmberlin-live-aac-mq?sABC=59s45n23%230%23no8617362n29q2o435p17n54928n16s5%23gharva&amsparams=playerid:tunein;skey:1509186083'
      media_content_type: 'audio/mp4' 

The only problem is the action with tts because it doesn’t get executed (at least I cannot hear a word and also nothing in the error log). I have tried the same action in the service tool of Home Assistant and it worked. How can I fix it in this project as well?


#7

Hey @Peleke not sure if you’re still having the issue of the tts not working but here’s a couple of things you can try. First thing is to comment out the part for playing the music and test to see if the tts works without it. If it does then set up like a 5 second delay after the tts and before the play media. I had the same issue and what happens is the music plays before the tts gets a chance to play and takes over. If that doesn’t work then maybe try switching

data:
  message: "Guten Morgen. Es ist {{now().hour}} Uhr {{now().minute}}"
  cache: false

to

data_template:
  message: "Guten Morgen. Es ist {{now().hour}} Uhr {{now().minute}}"
  cache: false

#8

Thanks, I got it working now with the added delay :slight_smile:


#9

Hey, can you share a complete copy for me? thank you! I need it very much.


#10

Can you share your input_boolean.alarmweekon and input_boolean.alarmweekday ?
Thanks


#11

What is inside input_boolean.freebox ?


#12

Hi,

Sorry for the late.
alarmweekday allow to trigger alarm only the working days
alarmweekon is the activated button for alarm.

The freebox is a french isp box, this boolean allow to power on the box with the alarm.


#13

Ok, thanks…


#14

Can you tell me how to change the default alarm clock hour?
I tried to change the sensor.alarm_time, but really don’t understand how…
The line is the following:

{{ "%0.02d:%0.02d" | format(states("input_number.alarmhour") | int, states("input_number.alarmminutes") | int) }}'


#15

Hi @maurizio53

I repost my last config for alarm clock :wink:
Test and say me if that resolve your issue

in your configuration.yaml :

input_number:
  alarmhour:
    name: Heure
    icon: mdi:timer
    initial: 6
    min: 0
    max: 23
    step: 1
    mode: slider
  alarmminutes:
    name: Minutes
    icon: mdi:timer
    initial: 35
    min: 0
    max: 59
    step: 5
    mode: slider

input_boolean:
  alarmweekday:
    name: Les jours de la semaine 
    initial: on
    icon: mdi:calendar
  alarmweekon:
    name: Activee     
    initial: on

in the automations.yaml :

  - alias: "reveil matin Aurore Semaine"
    trigger:
      - platform: time
        minutes: '/1'
        seconds: 0
    condition:
      - condition: state
        entity_id: input_boolean.alarmweekon
        state: 'on'
      - condition: state
        entity_id: input_boolean.alarmweekday
        state: 'on'
      - condition: state
        entity_id: calendar.jours_feries_en_france
        state: 'off'
      - condition: time
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
      - condition: template
        value_template: '{{ ((now().strftime("%s") | int + 300) | timestamp_custom("%H:%M") == states.sensor.alarm_time.state) }}'
    action:
      - service: light.turn_on
        entity_id: light.chambre
        data:
          transition: 600
          brightness: 255

  - alias: "reveil matin Aurore les weekend"
    trigger:
      - platform: time
        minutes: '/1'
        seconds: 0
    condition:
      - condition: state
        entity_id: input_boolean.alarmweekon
        state: 'on'
      - condition: state
        entity_id: input_boolean.alarmweekday
        state: 'off'
      - condition: time
        weekday:
          - sat
          - sun
      - condition: template
        value_template: '{{ now().strftime("%H:%M") == states.sensor.alarm_time.state }}'
    action:
      service: light.turn_on
      entity_id: light.chambre
      data:
        transition: 600
        brightness: 255

in the sensor.yaml :

- platform: template
  sensors:
    alarm_time:
      friendly_name: 'Time'
      value_template: '{{ "%0.02d:%0.02d" | format(states("input_number.alarmhour") | int, states("input_number.alarmminutes") | int) }}'

in the group.yaml :

  alarmclock:
    name: Wake Me Up
    entities:
      - sensor.alarm_time
      - input_number.alarmhour
      - input_number.alarmminutes
      - input_boolean.alarmweekday
      - input_boolean.alarmweekon

the calendar.jours_feries_en_france means days off in France :blush:


#16

It works as before. What i don’t understand is how to set a default alarm hour than 06:35? Where to change it?


#17

The default value is on the declaration of input_number value initial …


#18

Ah, ok i see… thanks