Trigger a Home Assistant automation when your phone alarm goes off

A lot of people don’t know this, but you can bring the time of your next alarm into Home Assistant and use this to trigger an automation. This is useful to trigger a morning routine based on when your phone alarm is set. You could turn on lights, open curtains and blinds, play some music or even start the coffee machine a few minutes after your alarm has sounded!

To use this automation you will need the Home Assistant mobile application installed on your Android or Apple smart phone.

Adding the Next Alarm sensor to Home Assistant

You first need to open the Home Assistant mobile app and enable the Next Alarm sensor. To do this you need to:

  1. Open the Home Assistant App menu and select Settings
  2. Go to Companion App
  3. Select Manage Sensors
  4. Find the Next Alarm sensor
  5. Enable the Next Alarm sensor
1. Go to Companion App in Settings
Home Assistant app settings

2. Select Manage Sensors

Home assistant mobile app sensors

3. Find the Next Alarm sensor

Datetime sensor for your phone's next alarm

4. Enable the Next Alarm sensor

You will now have a datetime sensor available in Home Assistant that is named the same as your phone and next_alarm appended to it.

Screenshot of Home Assistant next alarm datetime sensor

You can now use this sensor to trigger automations!

Triggering an Automation

I find the most powerful way to trigger an automation using this next_alarm sensor is with a template trigger.

Here is an example template that triggers the automation when the alarm is due to sound.

{{now().strftime(‘%a %h %d %H:%M %Z %Y’) == (((state_attr(‘sensor.alan_s_pixel_4_next_alarm’, ‘Time in Milliseconds’) | int / 1000) ) | timestamp_custom(‘%a %h %d %H:%M %Z %Y’))}}

Let’s take a look at the various parts of this template in more detail:

  • The orange part formats the current time to a date format like this: 2021-10-19 08:00:00.
  • The purple part takes the time of the next alarm from the next_alarm sensor. It is in this part that you would need to change the sensor.alan_s_pixel_4_next_alarm part to match the sensor entity name of your own next alarm sensor.
  • The green part formats the alarm time to the same time format as the current time, so that they can be compared

If you add this template to a template trigger automation, it will trigger when the alarm goes off.

platform: template
value_template: >-
  {{now().strftime("%a %h %d %H:%M %Z %Y") ==
  (((state_attr('sensor.alan_s_pixel_4_next_alarm', 'Time in Milliseconds') |
  int / 1000)) | timestamp_custom('%a %h %d %H:%M %Z %Y'))}}

You can now create the rest of the automation as normal!

Conditions

I’d highly recommend adding a condition to only trigger this automation if you are home. Otherwise, if you use your phone whilst you’re on holiday or away on a business trip, it will still run the automation in your home even though you aren’t there.

You can do this with a state condition that checks if your device tracker is set to home.

Delaying an action until 10 minutes after an alarm has sounded

I don’t want everything to happen immediately when my alarm goes off, that would be incredibly overwhelming. For this reason I’ve added a Delay Action into my automation so that my morning routing starts 10 minutes after my alarm goes off.

Here you can see my example automation that opens the blinds by 10%, 10 minutes after the alarm sounds. And then 10 minutes after that it opens the blinds to 50%.

Here’s the full automation in YAML format if you want it! (You’ll need to add your own alarm and device entity IDs to make this work)

alias: "Blind: Open blinds 10 minutes after alarm"
description: ""
trigger:
  - platform: template
    value_template: >-
      {{now().strftime("%a %h %d %H:%M %Z %Y") ==
      (((state_attr('sensor.alan_s_pixel_4_next_alarm', 'Time in Milliseconds')
      | int / 1000)) | timestamp_custom('%a %h %d %H:%M %Z %Y'))}}
    alias: When Alan's Phone Alarm goes off
condition:
  - condition: state
    entity_id: person.alan_byrne
    state: home
action:
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - device_id: 954ffb36c27a2d24f67d65d98b5b
    domain: cover
    entity_id: cover.bedroom_blind_motor
    type: set_position
    position: 10
    alias: Set Bedroom Blind Motor position to 10%
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - device_id: 954ffb36c27a2d24f67d65d98b
    domain: cover
    entity_id: cover.bedroom_blind_motor
    type: set_position
    position: 40
    alias: Set Bedroom Blind Motor position to 40%
mode: restart