Creating a alarm clock

: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

This should work if you want your automation to run 5 minutes before alarmtime:

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

Remember you can allways test your syntaxes in the DEV tool

@Bob_NL, yeah I tried that and it didn’t work. I think I’m having the same problem as @Vantskruv. When I type {{ ((now().strftime("%s") | int) | timestamp_custom("%H:%M")) }} into dev tools I get back 00:12. The time here is currently 23:12. Had a look at my timezone and it is configured correctly as Europe/Stockhom.

EDIT: My pi’s time zone was incorrect. Went into raspi-config and corrected it. Home Assistant is now displaying the right time data.

1 Like

Amazing with work here… could this get rolled into a module??

Everything works excellent but i can’t understand why it shows the .0 after the hour and minutes? how i can display only the integers?

Everything works excellent but i can’t understand why it shows the .0 after the hour and minutes? how i can display only the integers?

Could you post your sensor syntax? The int() filter can get you to integers. Here’s what I use for my template sensor:

  - platform: template
    sensors:
      alarm_clock_hour:
        value_template: '{{ states.input_slider.alarm_clock_hour.state | int }}'
      alarm_clock_minute:
        value_template: '{{ states.input_slider.alarm_clock_minute.state | int }}'

I use the following sensor to display the current set alarm time:

  - platform: template
      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) }}'

And the following sensor to calculate the time until my next alarm:

  - platform: template
      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 -%}
                   {{24*60+relative_time}}
          {%- else -%}
                   {{ relative_time-60}}
          {%- endif %}
2 Likes

Exactly what I was looking for, adding " | int " solved the issue…

@ronmar on vídeo you shared… your mobile phone rings alarm 17:25, but how you connect it to HA?
other words, how your mobile phone sends information to HA? i saw all your code, and didn´t see nothing about that.

For some reason, my automation doesn’t work automatically on time, i does work when i set the time with slider on current time, but not if i set to the future time. Here’s my automation code, what’s wrong?

  - alias: Wake Me Up
    trigger:
      platform: template
      value_template: '{{ ((now().strftime("%-H") | int) == (states.input_slider.alarmhour.state | int)) and ((now().strftime("%-M") | int) == (states.input_slider.alarmminutes.state | int)) }}'
    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:
                - sun
                - mon
                - tue
                - wed
                - thu
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarmstatus
              state: 'on'
            - condition: state
              entity_id: input_boolean.alarmweekday
              state: 'off'
    action:
      - service: script.turn_on
        data:
          entity_id: script.alarmclock