Creating a alarm clock

What’s the point of setting the finish time? Do you have any events triggered on that value?

it’s in appdaemon so it’s all python code. But I will post it in a while, still have a couple of changes and some personal information to move elsewhere.

1 Like

please share your code :wink:

will do once it’s documented and cleaned up.

Only to turn off the lights and radio in the bedroom, I just find it easy to use the sliders to set the off time, I suppose you could use a delay and templates to do the same thing. Next on the list is a snooze button :slight_smile:

That’s what I use :slight_smile:.

OK, here it is…

In Configuration.yaml
    input_slider:
      alarmhour:
        name: Alarm Hour
        icon: mdi:timer
        initial: 06
        min: 0
        max: 23
        step: 1
      alarmmins:
        name: Alarm Minutes
        icon: mdi:timer
        initial: 10
        min: 0
        max: 59
        step: 1
      alarmfinishhr:
        name: Finish Hour
        icon: mdi:watch-export
        initial: 06
        min: 0
        max: 23
        step: 1
      alarmfinishmins:
        name: Finish Minutes
        icon: mdi:watch-export
        initial: 30
        min: 0
        max: 55
        step: 5

In input_boolean.yaml
      alarmstatus:
        name: Wake Me Up
        initial: on
        icon: mdi:alarm-check
      alarmweekday:
        name: Weekdays Only
        initial: on
        icon: mdi:calendar

In groups.yaml

alarmclock:
  name: Wake Me Up
  #view: yes
  entities: 
    - sensor.alarm_time
    - input_slider.alarmhour
    - input_slider.alarmmins
    - input_boolean.alarmstatus
    - input_boolean.alarmweekday
    - sensor.alarm_time_fin
    - input_slider.alarmfinishhr
    - input_slider.alarmfinishmins

alrmclock:
  name: Wake Me Up!
  view: yes
  entities: 
    - group.alarmclock


In automation.yaml

     - alias: 'Wake Me Up'
       trigger:
          - platform: time
            minutes: '/1'
            seconds: 0      
       condition:
          condition: or
          conditions:
            - condition: and
              conditions:
                - condition: state
                  entity_id: input_boolean.alarmstatus
                  state: 'on'
                - condition: state
                  entity_id: input_boolean.alarmweekday
                  state: 'on'
                - condition: time
                  weekday:
                    - mon
                    - tue
                    - wed
                    - thu
                    - fri
                    - sat
                - condition: template
                  value_template: '{{ now().time().strftime("%R") == states.sensor.alarm_time.state }}'
            - condition: and
              conditions:
                - condition: state
                  entity_id: input_boolean.alarmstatus
                  state: 'on'
                - condition: state
                  entity_id: input_boolean.alarmweekday
                  state: 'off'
                - condition: template
                  value_template: '{{ now().time().strftime("%R") == states.sensor.alarm_time.state }}'
       action:
          - service: notify.mypushbullet
            data:
              title: "Alarm"
              message: "Good morning Sir. Time to Wake Up!"
          - service: switch.turn_on
            data:
              entity_id:
                - switch.bedroom_pc_lamp
                - switch.bedroom_red_bottom_lamp
                - switch.bedroom_uplight_bluebottom
                - light.bedroom_ceiling_light
                - switch.bedroom_pc__sound
          - service: switch.turn_on
            data:
              entity_id:
                - switch.foobaralarm
              
              
     - alias: Turn off Alarm
       trigger:
            - platform: time
              minutes: '/1'
              seconds: 0   
       condition:
         - condition: template
           value_template: '{{ now().time().strftime("%R") == states.sensor.alarm_time_fin.state }}'     
       action:
          - service: switch.turn_off
            data:
              entity_id:
                - switch.bedroom_pc_lamp
                - switch.bedroom_uplight_bluebottom
                - switch.bedroom_red_bottom_lamp
                - light.bedroom_ceiling_light
                - switch.bedroom_pc__sound
          - service: notify.mypushbullet
            data:
              title: "Alarm Finished"
              message: "You should be Up by Now!"
          - service: switch.turn_off
            data:
              entity_id:
                - switch.foobaralarm

Enjoy :slight_smile:

1 Like

thanks you

I think you forgot your sensors (alarm_time and alarm_time_fin), unless they were mentioned earlier already of course. Didn’t read the whole threat. But cool work!

:slight_smile: yep :stuck_out_tongue:

- platform: template
  sensors:
    alarm_hour:
#      friendly_name: 'Hour'
      value_template: '{{ states("input_slider.alarmhour") | round(0) }}'
    alarm_minutes:
#      friendly_name: 'Minutes'
      value_template: '{{ states("input_slider.alarmmins") | round(0) }}'
    alarm_time:
      friendly_name: 'Time'
      value_template: '{% if states("sensor.alarm_hour")|length == 1 %}0{% endif %}{{ states("sensor.alarm_hour") }}:{% if states("sensor.alarm_minutes")|length == 1 %}0{% endif %}{{ states("sensor.alarm_minutes") }}'
    alarm_hour_fin:
#      friendly_name: 'FinHour'
      value_template: '{{ states("input_slider.alarmfinishhr") | round(0) }}'
    alarm_minutes_fin:
#      friendly_name: 'FinMinutes'
      value_template: '{{ states("input_slider.alarmfinishmins") | round(0) }}'
    alarm_time_fin:
      friendly_name: 'FinTime'
      value_template: '{% if states("sensor.alarm_hour_fin")|length == 1 %}0{% endif %}{{ states("sensor.alarm_hour_fin") }}:{% if states("sensor.alarm_minutes_fin")|length == 1 %}0{% endif %}{{ states("sensor.alarm_minutes_fin") }}'

Consider this. To be fair, I copy/pasted & adapted it from an earlier entry in this topic :slight_smile:

  alarm_time:
    friendly_name: "Alarm start time"
    value_template: '{{ "%0.02d:%0.02d" | format(states("input_slider.alarmhour") | int, states("input_slider.alarmmins") | int) }}'

  alarm_time_fin:
    friendly_name: "Alarm end time"
    value_template: '{{ "%0.02d:%0.02d" | format(states("input_slider.alarmfinishhr") | int, states("input_slider.alarmfinishmins") | int) }}'
1 Like

Look good and is certainly more compact :slight_smile: does it work so that you get a leading zero?

I extended my alarm clock with a Lullaby option :slight_smile: :slight_smile:

Lullaby initiates with a HTTP Post command which is send from Tasker when I lay my phone on the wireless charger next to my bed. It first changes the sliders (via MQTT) to a different value and then to the value of which my alarmtime is set (readout with AutoAlarm). Then it recalculates the time left untill my next alarm, sends this as TTS to my bedroom speaker and finally fires the Lullaby trigger (if it’s set to “On” of course).

If anyone is intrerested in the configurations, let me know.

4 Likes

I have interest.share with us please.

Ok here we go. Perhaps it has gotten way to complicated but this is my result after HOURS and HOURS of trial and error ( with a lot of help from the experts on this forum) and it seems to do the job pretty well :slight_smile:

Sensors:

  - platform: template
    sensors:
      alarmtime:
        friendly_name: Wakeup Time
        entity_id:
         - input_slider.alarm_hour
         - input_slider.alarm_minutes
        value_template: '{{ "%0.02d:%0.02d" | format(states("input_slider.alarm_hour") | int, states("input_slider.alarm_minutes") | int) }}'
      nextalarm:
        friendly_name: Seconds untill next alarm
        entity_id:
         - input_slider.alarm_hour
         - input_slider.alarm_minutes
        value_template: >
          {% set relative_time =  (states.input_slider.alarm_hour.state|float|multiply(60) + states.input_slider.alarm_minutes.state|float) - (now().hour|float|multiply(60) + now().minute) %}
          {%- if relative_time < 0 -%}
                   {{23*60+relative_time}}
          {%- else -%}
                   {{ relative_time-60}}
          {%- endif %}
      time_until_alarm:
        friendly_name: Time untill next alarm
        entity_id:
         - sensor.nextalarm
        value_template: '{{  (states.sensor.nextalarm.state.split(" ")[0] | int *60 ) | timestamp_custom("%H:%M") }}'

Automation for wakeup light with radio:

- alias: 'Wakeup Light with Radio'
  trigger:
    platform: time
    minutes: '/5'
    seconds: '0'
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ ((now().strftime("%s") | int + 1200) | timestamp_custom("%H:%M")) == states.sensor.alarmtime.state  }}'
      - condition: state
        entity_id: input_boolean.alarm_clock
        state: 'on'
  action:
    - service: light.turn_on
      data:
        entity_id: light.bed_room
        brightness: 255
        rgb_color: [255,255,255]
        transition: 1200   
    - service: media_player.play_media
      data_template:
        entity_id: media_player.bed_room
        media_content_id: >
            {% if is_state("input_select.radio_wakeup", "Radio 538") %} http://vip-icecast.538.lw.triple-it.nl:80/RADIO538_MP3
            {% elif is_state("input_select.radio_wakeup", "Q-Music") %} http://icecast-qmusic.cdp.triple-it.nl/Qmusic_nl_live_96.mp3
            {% elif is_state("input_select.radio_wakeup", "3FM") %} http://icecast.omroep.nl/3fm-bb-mp3
            {% elif is_state("input_select.radio_wakeup", "100% NL") %} http://stream.100p.nl/100pctnl.mp3
            {% elif is_state("input_select.radio_wakeup", "Veronica") %} http://8543.live.streamtheworld.com/VERONICACMP3
            {% elif is_state("input_select.radio_wakeup", "Sky Radio") %} http://8623.live.streamtheworld.com:80/SKYRADIOAAC_SC
            {% elif is_state("input_select.radio_wakeup", "Arrow Classic Rock") %} http://91.221.151.155/listen.mp3
            {% elif is_state("input_select.radio_wakeup", "Classic FM") %} http://19143.live.streamtheworld.com/CLASSICFM_SC
            {% elif is_state("input_select.radio_wakeup", "BNR Nieuwsradio") %} http://icecast-bnr.cdp.triple-it.nl/bnr_mp3_96_03
            {% endif %}
        media_content_type: 'audio/mp4' 
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.01'
    - delay: '00:02:00'
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.05'
    - delay: '00:02:00'
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.10'
    - delay: '00:01:30'
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.15'
    - delay: '00:01:30'
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.20'
    - delay: '00:01:30' 
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.25'
    - delay: '00:01:00'
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.30'
    - delay: '00:01:00'
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.35'
    - delay: '00:01:00'     
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.40'
    - delay: '00:01:00' 
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.45'
    - delay: '00:01:00'
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.50'
    - delay: '00:01:00'
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.55'
    - delay: '00:00:45'
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.60'
    - delay: '00:00:45'
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.65'
    - delay: '00:00:45'
    - service: media_player.volume_set
      data:
        entity_id: media_player.bed_room
        volume_level: '0.70'
    - delay: '00:02:00'   
    - service: light.turn_on
      data:
        entity_id: light.hall_way
        brightness: 125
    - service: light.turn_on
      data:
        entity_id: light.diner_table
        brightness: 125
    - service: light.turn_on
      data:
        entity_id: light.living_room
        brightness: 140
    - service: media_player.volume_set
      data:
        entity_id: media_player.ca_bathroom
        volume_level: '0.40'
    - delay: '00:00:10'
    - service: media_player.volume_set
      data:
        entity_id: media_player.ca_bathroom
        volume_level: '0.40'
    - service: media_player.play_media
      data_template:
        entity_id: media_player.ca_bathroom
        media_content_id: >
            {% if is_state("input_select.radio_wakeup", "Radio 538") %} http://vip-icecast.538.lw.triple-it.nl:80/RADIO538_MP3
            {% elif is_state("input_select.radio_wakeup", "Q-Music") %} http://icecast-qmusic.cdp.triple-it.nl/Qmusic_nl_live_96.mp3
            {% elif is_state("input_select.radio_wakeup", "3FM") %} http://icecast.omroep.nl/3fm-bb-mp3
            {% elif is_state("input_select.radio_wakeup", "100% NL") %} http://stream.100p.nl/100pctnl.mp3
            {% elif is_state("input_select.radio_wakeup", "Veronica") %} http://8543.live.streamtheworld.com/VERONICACMP3
            {% elif is_state("input_select.radio_wakeup", "Sky Radio") %} http://8623.live.streamtheworld.com:80/SKYRADIOAAC_SC
            {% elif is_state("input_select.radio_wakeup", "Arrow Classic Rock") %} http://91.221.151.155/listen.mp3
            {% elif is_state("input_select.radio_wakeup", "Classic FM") %} http://19143.live.streamtheworld.com/CLASSICFM_SC
            {% elif is_state("input_select.radio_wakeup", "BNR Nieuwsradio") %} http://icecast-bnr.cdp.triple-it.nl/bnr_mp3_96_03
            {% endif %}
        media_content_type: 'audio/mp4' 
    - service: homeassistant.turn_off
      data:
        entity_id: input_boolean.alarm_clock

Automations for changing alarm time triggered through MQTT, sent through Tasker:

- alias: Set alarmtime_hour
  trigger:
    platform: mqtt
    topic: "setHour"
  action:
     service: input_slider.select_value
     data_template:
      entity_id: input_slider.alarm_hour
      value: '{{ trigger.payload}}'
      
- alias: Set alarmtime_minutes
  trigger:
    platform: mqtt
    topic: "setMinutes"
  action:
     service: input_slider.select_value
     data_template:
      entity_id: input_slider.alarm_minutes
      value: '{{ trigger.payload}}'

Script for executing lullaby and gently decreasing volume over 30 mins:

  sleep:
    alias: Good Night
    sequence:
      - alias: Set Volume
        service: media_player.volume_set
        data:
          entity_id: media_player.bed_room
          volume_level: '1.0'
      - alias: Say Sleep Left   
        service: tts.google_say
        entity_id: media_player.bed_room
        data_template:
          message: 'Next alarm in  {{  (states.sensor.nextalarm.state.split(" ")[0] | int *60 ) | timestamp_custom("%H") | int }} hours and {{  (states.sensor.nextalarm.state.split(" ")[0] | int *60 ) | timestamp_custom("%M")  }} minutes . Good night.'
          cache: false 
      - delay: '00:00:15' 
      - alias: Set Volume2
        service: media_player.volume_set
        data:
          entity_id: media_player.bed_room
          volume_level: '0.75'    
      - condition: state
        entity_id: input_boolean.lullaby
        state: 'on'          
      - service: media_player.play_media
        entity_id: media_player.bed_room
        data_template:
          entity_id: media_player.bed_room
          media_content_id: >
           {% if is_state("input_select.lullaby", "Sleep Radio") %} http://37.59.28.208:8722/stream
           {% elif is_state("input_select.lullaby", "Ambient Sleeping Pill") %} http://perseus.shoutca.st:8447/h
           {% elif is_state("input_select.lullaby", "Radio Art - Sleep") %} http://live.radioart.com/fSleep.mp3
           {% elif is_state("input_select.lullaby", "Ambi Nature Radio") %} http://94.23.252.14:8067/stream
           {% elif is_state("input_select.lullaby", "Calm Radio - Sleep") %} http://streams.calmradio.com/api/39/128/stream
           {% endif %}
          media_content_type: 'audio/mp4'  
      - delay: '00:05:00'
      - service: media_player.volume_set
        data:
          entity_id: media_player.bed_room
          volume_level: '0.70'
      - delay: '00:02:30'
      - service: media_player.volume_set
        data:
          entity_id: media_player.bed_room
          volume_level: '0.65'
      - delay: '00:02:30'
      - service: media_player.volume_set
        data:
          entity_id: media_player.bed_room
          volume_level: '0.60'
      - delay: '00:05:00'
      - service: media_player.volume_set
        data:
          entity_id: media_player.bed_room
          volume_level: '0.55'
      - delay: '00:02:30'
      - service: media_player.volume_set
        data:
          entity_id: media_player.bed_room
          volume_level: '0.50'
      - delay: '00:02:30'
      - service: media_player.volume_set
        data:
          entity_id: media_player.bed_room
          volume_level: '0.45'
      - delay: '00:05:00'
      - service: media_player.volume_set
        data:
          entity_id: media_player.bed_room
          volume_level: '0.40'   
      - delay: '00:02:30'
      - service: media_player.volume_set
        data:
          entity_id: media_player.bed_room
          volume_level: '0.35'   
      - delay: '00:02:30'
      - service: media_player.volume_set
        data:
          entity_id: media_player.bed_room
          volume_level: '0.30'   
      - delay: '00:05:00'
      - service: media_player.volume_set
        data:
          entity_id: media_player.bed_room
          volume_level: '0.25'   

Input sliders, selectors and booleans:

input_boolean:
  alarm_clock:
    name: On/Off
    initial: off
    icon: mdi:alarm
  lullaby:
    name: Lullaby On/Off
    initial: On
    icon: mdi:sleep
 
input_slider: 
  alarm_hour:
    name: Hour
    icon: mdi:timer
    initial: 6
    min: 0
    max: 23
    step: 1
  alarm_minutes:
    name: Minutes
    icon: mdi:timer
    initial: 35
    min: 0
    max: 55
    step: 5
    
input_select:
  radio_wakeup:
    name: Wakeup Radio Station
    options:
      - Radio 538
      - Q-Music
      - 3FM
      - 100% NL
      - Veronica
      - Sky Radio
      - Arrow Classic Rock
      - Classic FM
      - BNR Nieuwsradio
    initial: Radio 538
    icon: mdi:radio         
  lullaby:
    name: Lullaby Radio Station
    options:
      - Sleep Radio
      - Ambient Sleeping Pill
      - Radio Art - Sleep
      - Ambi Nature Radio
      - Calm Radio - Sleep
    initial: Ambient Sleeping Pill
    icon: mdi:music-circle

Frontend view:

group:
 Wake-up Alarm:
  name: Wakeup Light with Radio
  entities:
  - input_boolean.alarm_clock
  - sensor.alarmtime
  - sensor.time_until_alarm
  - input_slider.alarm_hour
  - input_slider.alarm_minutes
  - input_boolean.lullaby
  - input_select.lullaby
  - input_select.radio_wakeup
13 Likes

I made a video guide for how to make an alarm clock from start to finish. It might be useful if you’re stuck.

9 Likes

Hi,

Similarly to you Bob_NL, I’ve used the following succesfully:

'{{ (now().strftime("%s") | int | timestamp_custom("%H:%M")) == states.input_select.alarmtime.state }}

However, unlike you, the following does not work for me:

'{{ ((now().strftime("%s") | int + 1200) | timestamp_custom("%H:%M")) }} == {{ states.input_select.alarmtime.state }}'

I’ve spent hours tweaking with no success. I’d ideally like the automation to run x amount of time before the alarm time. Anybody any ideas?

Hey @davemccrea ! So if I understand you correct I should be doing precicely that with my alarm clock on hass. What I wanted to do was trigger my automation 5 minutes EARLIER than the alarm time you choose.

All I did was a have a second template sensor for the time like so:
alarm_time:
friendly_name: ‘Time with offset’
value_template: ‘{{ “%0.02d:%0.02d” | format(states(“input_slider.alarmhour”) | int, states(“input_slider.alarmminutes”) | int - 5) }}’

So my trigger uses the above like so:
trigger:
platform: template
value_template: ‘{{ states.sensor.time.state == states.sensor.alarm_time.state }}’

As for the hass front end/ui “time” I basscially have another time sensor display the correct time when you choose with the sliders, and viola, it works for me :slight_smile:

EDIT: ugh the code formating on the forum is not workingproperly for me :confused:

Thanks for the code @lv-88! Could do the trick …

What happens, as an example, if you set your alarm for 0703? Am I right in saying that the automation would begin at 0700 rather than 0658?

Well that won’t happen in my case since I have incremental steps of 5 for the minute slider, but if it didn’t have it I guess you are correct