How to create the alarm clock in HASS?

I think i need to learn writing in Scripts.Yaml, i want to set loop action because the xiaomi gateway only ring 1 times (about 3 seconds not enough for me to wakeup), i need to more and more loop to more and more sound to get me out of my bed. :smiley:

You can always repete the action in the automation.

Hello @Hellis81, how can i do it? :smiley: thanks

Are you using GUI or code to make your automations?

Im using code to do action in automation :smiley:
My code based on @123,
My code is

  alias: Wake 1
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == states('sensor.wake_time_1') }}"
  condition:
    condition: template
    value_template: >
      {% set today = 'input_boolean.wakeweekday_' ~ now().strftime("%a") | lower ~ '_1' %}
      {{ is_state('input_boolean.wakestatus_1', 'on') and is_state(today, 'on') }}
  action:
  - data:
      gw_mac: 00:00:00:00:00:00
      ringtone_id: 10
      ringtone_vol: 100
    service: xiaomi_aqara.play_ringtone

Then I will assume :

  alias: Wake 1
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == states('sensor.wake_time_1') }}"
  condition:
    condition: template
    value_template: >
      {% set today = 'input_boolean.wakeweekday_' ~ now().strftime("%a") | lower ~ '_1' %}
      {{ is_state('input_boolean.wakestatus_1', 'on') and is_state(today, 'on') }}
  action:
  - data:
      gw_mac: 00:00:00:00:00:00
      ringtone_id: 10
      ringtone_vol: 100
    service: xiaomi_aqara.play_ringtone
  - delay: 5 #????
  - data:
      gw_mac: 00:00:00:00:00:00
      ringtone_id: 10
      ringtone_vol: 100
    service: xiaomi_aqara.play_ringtone

Should do what you did and add a five second delay and do it again.
Keep in mind that HA will not wait for one action to finish before it does the next so add a delay between the sounds.
(At least that is how it works in Node RED, not sure about HA automation)

1 Like

Here is a more sophisticated version that includes a repeating alarm action that can be easily silenced using a button (in the UI).

Compared to the previous version, this one also has:

  • a timer, for repeating the alarm:
    • timer.wake_1
  • another input_boolean, for indicating when the alarm is activated:
    • input_boolean.wakeactivated_1
  • three additional automations, for managing the timer:
    • automation.wake_1_activated
    • automation.wake_1_timer_started
    • automation.wake_1_timer_finished
  • a Conditional card with a Button card to silence the alarm.

1. Timer, Input_Booleans and Input_Numbers

This version uses a 10-second timer for repeating the alarm action. Change the timer’s duration to whatever you prefer. There is also an additional input_boolean to indicate when the alarm is activated.

Click to reveal details
#configuration.yaml
timer:
  wake_1:
    duration: '00:00:10'

input_boolean:
  wakeactivated_1:
    name: Alarm 1 Activated
    icon: mdi:alarm
  wakestatus_1:
    name: Alarm 1
    icon: mdi:alarm
  wakeweekday_sun_1:
    name: Sunday
    icon: mdi:calendar
  wakeweekday_mon_1:
    name: Monday        
    icon: mdi:calendar
  wakeweekday_tue_1:
    name: Tuesday        
    icon: mdi:calendar
  wakeweekday_wed_1:
    name: Wednesday       
    icon: mdi:calendar
  wakeweekday_thu_1:
    name: Thursday       
    icon: mdi:calendar
  wakeweekday_fri_1:
    name: Friday        
    icon: mdi:calendar
  wakeweekday_sat_1:
    name: Saturday        
    icon: mdi:calendar

input_number:
  wakehour_1:
    name: Hour
    min: 00
    max: 23
    step: 1
    icon: mdi:alarm
  wakeminutes_1:
    name: Minutes
    min: 00
    max: 59
    step: 1

2. Sensors

The sensors remain the same as in the previous version.

Click to reveal details
#sensor:
- platform: template
  sensors:
    wake_time_1:
      friendly_name: 'Wake Time 1'
      value_template: "{{'{:02d}:{:02d}'.format(states('input_number.wakehour_1') | int, states('input_number.wakeminutes_1') | int) }}"

- platform: time_date
  display_options:
    - 'time'

3. Automation

In this version, there are three additional automations to handle the timer’s events. You must customize the action of automation.wake_1_alarm_activated to suit your needs. In this example, the action simply posts a persistent notification (visible in Home Assistant’s UI) every ten seconds (i.e. the timer’s duration).

Click to reveal details
- id: 'wake_1_detect_time'
  alias: Wake 1
  description: 'Detect specified wake time'
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == states('sensor.wake_time_1') }}"
  condition:
    condition: template
    value_template: >
      {% set today = 'input_boolean.wakeweekday_' ~ now().strftime("%a") | lower ~ '_1' %}
      {{ is_state('input_boolean.wakestatus_1', 'on') and is_state(today, 'on') }}
  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.wakeactivated_1

- id: 'wake_1_alarm_activated'
  alias: 'Wake 1 Activated'
  trigger:
    platform: state
    entity_id: input_boolean.wakeactivated_1
    from: 'off'
    to: 'on'
  action:
    service: timer.start
    entity_id: timer.wake_1

- alias: 'Wake 1 Timer Started'
  trigger:
    platform: event
    event_type: timer.started
    event_data:
      entity_id: timer.wake_1
  condition:
    condition: state
    entity_id: input_boolean.wakeactivated_1
    state: 'on'
  action:
    service: persistent_notification.create
    data_template:
      title: 'Wake 1 Activated'
      message: 'Wake 1 activated at: {{now()}}'

- alias: 'Wake 1 Timer Finished'
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.wake_1
  condition:
    condition: state
    entity_id: input_boolean.wakeactivated_1
    state: 'on'
  action:
    service: timer.start
    entity_id: timer.wake_1

4. Lovelace UI

When no alarm is activated, this is its appearance in the UI (same as the previous version):

Screenshot from 2020-07-07 14-15-58

When an alarm is activated, a button appears below the alarm clock allowing you to silence the alarm. When you tap the button it will toggle the state of input_boolean.wakeactivated_1 (thereby preventing additional notifications) and the button will disappear from the UI.

Screenshot from 2020-07-07 14-17-29

Click to reveal Lovelace card configuration
cards:
  - entities:
      - entity: input_boolean.wakestatus_1
        name: Mode
      - entity: sensor.wake_time_1
        name: Wake Time
      - entity: input_number.wakehour_1
      - entity: input_number.wakeminutes_1
    show_header_toggle: false
    title: Alarm Clock 1
    type: entities
  - cards:
      - entity: input_boolean.wakeweekday_sun_1
        hold_action:
          action: none
        icon_height: 40px
        name: Sun
        show_icon: true
        show_name: true
        tap_action:
          action: toggle
        type: button
      - entity: input_boolean.wakeweekday_mon_1
        hold_action:
          action: none
        icon_height: 40px
        name: Mon
        show_icon: true
        show_name: true
        tap_action:
          action: toggle
        type: button
      - entity: input_boolean.wakeweekday_tue_1
        hold_action:
          action: none
        icon_height: 40px
        name: Tue
        show_icon: true
        show_name: true
        tap_action:
          action: toggle
        type: button
      - entity: input_boolean.wakeweekday_wed_1
        hold_action:
          action: none
        icon_height: 40px
        name: Wed
        show_icon: true
        show_name: true
        tap_action:
          action: toggle
        type: button
      - entity: input_boolean.wakeweekday_thu_1
        hold_action:
          action: none
        icon_height: 40px
        name: Thu
        show_icon: true
        show_name: true
        tap_action:
          action: toggle
        type: button
      - entity: input_boolean.wakeweekday_fri_1
        hold_action:
          action: none
        icon_height: 40px
        name: Fri
        show_icon: true
        show_name: true
        tap_action:
          action: toggle
        type: button
      - entity: input_boolean.wakeweekday_sat_1
        hold_action:
          action: none
        icon_height: 40px
        name: Sat
        show_icon: true
        show_name: true
        tap_action:
          action: toggle
        type: button
    type: horizontal-stack
  - type: conditional
    conditions:
      - entity: input_boolean.wakeactivated_1
        state: 'on'
    card:
      type: button
      tap_action:
        action: toggle
      hold_action:
        action: none
      show_icon: true
      show_name: true
      entity: input_boolean.wakeactivated_1
      name: Silence Alarm
      icon: 'mdi:alarm-off'
      icon_height: 50px
type: vertical-stack

NOTE

My personal preference would be to replace the two input_numbers (Hour and Minutes) and sensor (Wake Time) with a single input_datetime. The result of that choice means you must use your phone/tablet’s onscreen keyboard to set the time. I don’t mind the aesthetics of that but others might so that’s why I retained the original example’s use of input_numbers.

17 Likes

oh woa, very detail and you descibe very detail in per codes, Its help for me to know how it’s work, thank you for helping me :blush:. I gonna do its now, thank you very much @123

Hello @123,

Your config very effective for me. It’s wake me up right time to start my day.

But now im update my Hassio to 0.113, and i see it’s have so many code: Single, Queued, Parallel and Restart. Now, All my script don’t work, its only have sequence Red - Blue - Alarm 1 - Alarm 2 and turn off by themself, only do 1 sequence, not repeat. How can i fix that?

And i try using queued but it don’t work

Below is my code:

Thank for your help ^^

xiaomi_alarm1:
  sequence:
  - data:
      gw_mac: 7C:49:EB:1B:AA:52
      ringtone_id: 13
      ringtone_vol: 50
    service: xiaomi_aqara.play_ringtone
  - delay: '[object Object]'
  - service: script.xiaomi_alarm2
  mode: queued
  max: 10
xiaomi_alarm2:
  sequence:
  - data:
      gw_mac: 7C:49:EB:1B:AA:52
      ringtone_id: 13
      ringtone_vol: 75
    service: xiaomi_aqara.play_ringtone
  - delay: '[object Object]'
  - service: script.xiaomi_alarm1
  mode: queued
  max: 10
flash_gateway1:
  alias: Flash Gateway On Red
  sequence:
  - service: light.turn_on
    data:
      entity_id: light.gateway_light_7c49eb1baa52
      color_name: red
      brightness: 255
  - delay:
      milliseconds: 600
  - service: light.turn_off
    data:
      entity_id: light.gateway_light_7c49eb1baa52
  - service: script.flash_gateway2
flash_gateway2:
  alias: Flash Gateway On Blue
  sequence:
  - service: light.turn_on
    data:
      entity_id: light.gateway_light_7c49eb1baa52
      color_name: blue
      brightness: 255
  - delay:
      milliseconds: 600
  - service: light.turn_off
    data:
      entity_id: light.gateway_light_7c49eb1baa52
  - service: script.flash_gateway1

What is that? It should be a time value.

For example, a 2 second delay can be created like this:

  - delay: '00:00:02'

or like this:

  - delay:
      seconds: 2
1 Like

Oh i change to

  - delay:
      seconds: 2
But it not a loop, i think it's come from new action for scipt (single, restart, parallel and Queued

I’m not suggesting you change to a 2-second delay only that you correct the delay you already have in your script because this is invalid:
delay: '[object Object]'

Regarding the mode option, if you do not include it in a script, according to the documentation, the script will default to using mode: single.

1 Like

Oh i read infomations change in 0.113. It’s have advanced for coding (Included 4 mode for Automation & Action, and Somes changes in Automation, to fix code can run on 0.113. We need to fix:

In Automation (Change method to Call Script in action)

#(For 0.113 or later)  
action:   
    - service: script.turn_on 
      entity_id:
      - script.flash_gateway1
      - script.xiaomi_alarm1

Describe: In 0.112 or previous Version, we call script directly, like below (DONT DO BELOW CODE FOR 0.113 OR LATER)

Action: 
    - service: script.xiaomi_alarm1
    - service: script.flash_gateway1

This mean, after run script.xiaomi_alarm1 done, it will do continue flash_gateway1.

But Script script.xiaomi_alarm1 is a infinity loops. It’s cant be done, and cant continue script flash_gateway1.

In new config we need to call it thourgh on service script turn on. It’s will run 2 scripts same time (not wait script 1 done (end) and will do script 2).

I don’t known 4 new mode for script and automation, but i try per mode one by one. And Parallel is work. And option max for Parralel to do maxium loops. I using 90 for sound and 1000 for light. I think it right for me. (depend on you)

In Script (Using Parallel mode)

xiaomi_alarm1:
  sequence:
  - data:
      gw_mac: ####
      ringtone_id: 13
      ringtone_vol: 50
    service: xiaomi_aqara.play_ringtone
  - delay:
      seconds: 6
  - service: script.xiaomi_alarm2
  mode: parallel
  max: 90

xiaomi_alarm2:
  sequence:
  - data:
      gw_mac: ####
      ringtone_id: 13
      ringtone_vol: 75
    service: xiaomi_aqara.play_ringtone
  - delay:
      seconds: 6
  - service: script.xiaomi_alarm1
  mode: parallel
  max: 90


flash_gateway1:
  alias: Flash Gateway On Red
  sequence:
  - data:
      brightness: 255
      color_name: red
      entity_id: light.gateway_light_####
    service: light.turn_on
  - delay:
      milliseconds: 600
  - data:
      entity_id: light.gateway_light_####
    service: light.turn_off
  - service: script.flash_gateway2
  mode: parallel
  max: 1000

flash_gateway2:
  alias: Flash Gateway On Blue
  sequence:
  - data:
      brightness: 255
      color_name: blue
      entity_id: light.gateway_light_####
    service: light.turn_on
  - delay:
      milliseconds: 600
  - data:
      entity_id: light.gateway_light_####
    service: light.turn_off
  - service: script.flash_gateway1
  mode: parallel
  max: 1000

And now, it’s work. Thank you very much, @123

Hi, I try to implement your configuration but snoozing doesn’t seem to work. Can you please share you entire code please?
Thanks Chris.

I will post it after my school day, 3pm GMT +7, pls remind me ^^

Ok Thanks a lot.

This is my script, so many scripts, because i devided it to make another automation (sercurity alarm)

xiaomi_alarm3:
  sequence:
  - repeat:
      count: 180
      sequence:
      - data:
          gw_mac: 7C:49:EB:1B:AA:52
          ringtone_id: 13
          ringtone_vol: 90
        service: xiaomi_aqara.play_ringtone
      - delay:
          seconds: 6
  mode: restart
flash_gateway3:
  alias: Flash Gateway On Blue and Red
  sequence:
  - repeat:
      count: 600
      sequence:
      - data:
          brightness: 255
          color_name: blue
          entity_id: light.gateway_light_7c49eb1baa52
        service: light.turn_on
      - delay:
          seconds: 1
      - data:
          brightness: 255
          color_name: red
          entity_id: light.gateway_light_7c49eb1baa52
        service: light.turn_on
      - delay:
          seconds: 1
  mode: restart

alarm_off:
  alias: Alarm was disarmed
  sequence:
  - service: script.turn_off
    data:
      entity_id:
      - script.flash_gateway3
      - script.xiaomi_alarm3

green_light_safe:
  alias: Green light for safe
  sequence:
  - service: light.turn_on
    data:
      entity_id: light.gateway_light_7c49eb1baa52
      color_name: green
      brightness: 100
  - delay:
      seconds: 5
  - service: light.turn_off
    data:
      entity_id: light.gateway_light_7c49eb1baa52

alarm_off_greenlight:
  alias: Alarm was disarmed & Green light on
  sequence:
  - service: script.turn_on
    data:
      entity_id:
      - script.alarm_off
      - script.green_light_safe

alarm_snooze:
  alias: Alarm was Snooze
  sequence:
  - service: script.alarm_off
  - service: light.turn_on
    data:
      entity_id: light.gateway_light_7c49eb1baa52
      color_name: purple
      brightness: 100
  - delay:
      seconds: 5
  - service: light.turn_off
    data:
      entity_id: light.gateway_light_7c49eb1baa52
  - delay:
      seconds: 300
  - service: script.turn_on
    entity_id:
    - script.flash_gateway3
    - script.xiaomi_alarm3
  - delay:
      seconds: 300
  - service: script.turn_on
    entity_id:
    - script.alarm_off

And my automation. like above but custom script turn on to run 2 scripts.

  action:
  - service: script.turn_on
    entity_id:
    - script.flash_gateway3
    - script.xiaomi_alarm3

Great thanks. Maybe your lovelace?
I tried 123 code it works to start but not the snooze

My lovelace using HA-Card (Custom Component)

  - content: |
      # Alarm System
    style:
      .: |
        ha-card {
          background-color: transparent !important;
          box-shadow: none !important;
        }
      ha-markdown:
        $: |
          h1 {
            font-size: 25px;
            font-weight: bold;
            text-align: center;
            letter-spacing: '-0.01em';
           }
    type: markdown
  - entities:
      - entity: input_boolean.wakestatus_1
        name: Mode
      - entity: sensor.time
        name: The Time Now
      - entity: sensor.wake_time_1
        name: Wake Time
      - entity: input_number.wakehour_1
      - entity: input_number.wakeminutes_1
    show_header_toggle: false
    type: entities
  - cards:
      - entity: input_boolean.wakeweekday_sun_1
        hold_action:
          action: none
        icon_height: 40px
        name: Sun
        show_icon: true
        show_name: true
        tap_action:
          action: toggle
        type: button
      - entity: input_boolean.wakeweekday_mon_1
        hold_action:
          action: none
        icon_height: 40px
        name: Mon
        show_icon: true
        show_name: true
        tap_action:
          action: toggle
        type: button
      - entity: input_boolean.wakeweekday_tue_1
        hold_action:
          action: none
        icon_height: 40px
        name: Tue
        show_icon: true
        show_name: true
        tap_action:
          action: toggle
        type: button
      - entity: input_boolean.wakeweekday_wed_1
        hold_action:
          action: none
        icon_height: 40px
        name: Wed
        show_icon: true
        show_name: true
        tap_action:
          action: toggle
        type: button
      - entity: input_boolean.wakeweekday_thu_1
        hold_action:
          action: none
        icon_height: 40px
        name: Thu
        show_icon: true
        show_name: true
        tap_action:
          action: toggle
        type: button
      - entity: input_boolean.wakeweekday_fri_1
        hold_action:
          action: none
        icon_height: 40px
        name: Fri
        show_icon: true
        show_name: true
        tap_action:
          action: toggle
        type: button
      - entity: input_boolean.wakeweekday_sat_1
        hold_action:
          action: none
        icon_height: 40px
        name: Sat
        show_icon: true
        show_name: true
        tap_action:
          action: toggle
        type: button
    type: horizontal-stack
  - cards:
      - entity: script.alarm_off_greenlight
        hold_action:
          action: toggle
        icon: 'mdi:alarm-off'
        icon_height: 50px
        name: Silence Alarm
        show_icon: true
        show_name: true
        show_state: false
        tap_action:
          action: more-info
        type: button
      - entity: script.alarm_snooze
        hold_action:
          action: more-info
        icon: 'mdi:alarm-snooze'
        icon_height: 50px
        name: Snooze !!!
        show_icon: true
        show_name: true
        show_state: false
        tap_action:
          action: toggle
        type: button
    type: horizontal-stack
type: vertical-stack
1 Like

I work with package. Here is my complete yaml

timer:
  wake_1:
    duration: '00:00:10'

input_boolean:
  wakeactivated_1:
    name: Alarm 1 Activated
    icon: mdi:alarm
  wakestatus_1:
    name: Alarm 1
    icon: mdi:alarm
  wakeweekday_sun_1:
    name: Sunday
    icon: mdi:calendar
  wakeweekday_mon_1:
    name: Monday        
    icon: mdi:calendar
  wakeweekday_tue_1:
    name: Tuesday        
    icon: mdi:calendar
  wakeweekday_wed_1:
    name: Wednesday       
    icon: mdi:calendar
  wakeweekday_thu_1:
    name: Thursday       
    icon: mdi:calendar
  wakeweekday_fri_1:
    name: Friday        
    icon: mdi:calendar
  wakeweekday_sat_1:
    name: Saturday        
    icon: mdi:calendar

input_number:
  wakehour_1:
    name: Hour
    min: 00
    max: 23
    step: 1
    icon: mdi:alarm
  wakeminutes_1:
    name: Minutes
    min: 00
    max: 59
    step: 1
    
sensor:
  - platform: template
    sensors:
      wake_time_1:
        friendly_name: 'Wake Time 1'
        value_template: "{{'{:02d}:{:02d}'.format(states('input_number.wakehour_1') | int, states('input_number.wakeminutes_1') | int) }}"

script:
  xiaomi_alarm1:
    sequence:
    - data:
        gw_mac: 'macadress'
        ringtone_id: 13
        ringtone_vol: 50
      service: xiaomi_aqara.play_ringtone
    - delay:
        seconds: 6
    - service: script.xiaomi_alarm2
    mode: parallel
    max: 10

  xiaomi_alarm2:
    sequence:
    - data:
        gw_mac: 'macadress'
        ringtone_id: 13
        ringtone_vol: 75
      service: xiaomi_aqara.play_ringtone
    - delay:
        seconds: 6
    - service: script.xiaomi_alarm1
    mode: parallel
    max: 10

xiaomi_alarm3:
  sequence:
  - repeat:
      count: 180
      sequence:
      - data:
          gw_mac: 'macadress'
          ringtone_id: 13
          ringtone_vol: 40
        service: xiaomi_aqara.play_ringtone
      - delay:
          seconds: 6
  mode: restart

alarm_snooze:
  alias: Alarm was Snooze
  sequence:
  - service: script.alarmclock_off
  - delay:
      seconds: 300
  - service: script.turn_on
    entity_id:
    - script.xiaomi_alarm3
  - delay:
      seconds: 300
  - service: script.turn_on
    entity_id:
    - script.alarmclock_off

alarmclock_off:
  alias: Alarm was disarmed
  sequence:
  - service: script.turn_off
    data:
      entity_id:
      - script.xiaomi_alarm3


automation:
  - id: 'wake_1_detect_time'
    alias: Wake 1
    description: 'Detect specified wake time'
    trigger:
      platform: template
      value_template: "{{ states('sensor.time') == states('sensor.wake_time_1') }}"
    condition:
     condition: template
     value_template: >
        {% set today = 'input_boolean.wakeweekday_' ~ now().strftime("%a") | lower ~ '_1' %}
        {{ is_state('input_boolean.wakestatus_1', 'on') and is_state(today, 'on') }}
    action:
      service: input_boolean.turn_on
      entity_id: input_boolean.wakeactivated_1

  - id: 'wake_1_alarm_activated'
    alias: 'Wake 1 Activated'
    trigger:
      platform: state
      entity_id: input_boolean.wakeactivated_1
      from: 'off'
      to: 'on'
#    action:
#      service: timer.start
#      entity_id: timer.wake_1
    action:   
      service: script.turn_on 
      entity_id:
      - script.xiaomi_alarm1

  - alias: 'Wake 1 Timer Started'
    trigger:
      platform: event
      event_type: timer.started
      event_data:
        entity_id: timer.wake_1
    condition:
      condition: state
      entity_id: input_boolean.wakeactivated_1
      state: 'on'
    action:
      service: persistent_notification.create
      data_template:
        title: 'Wake 1 Activated'
        message: 'Wake 1 activated at: {{now()}}'

  - alias: 'Wake 1 Timer Finished'
    trigger:
      platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.wake_1
    condition:
      condition: state
      entity_id: input_boolean.wakeactivated_1
      state: 'on'
    action:
      service: timer.start
      entity_id: timer.wake_1```
1 Like