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 App Configuration
  2. Select Manage Sensors
  3. Find the Next Alarm sensor
  4. Enable the Next Alarm sensor
Home Assistant mobile app menu
1. Open the Home Assistant App menu and select App Configuration
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. This allows me to compare the alarm time with the current time and add any offsets to make the automation trigger either before or after the alarm.

Here is an example template that triggers the automation 10 minutes after 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) + 10*60 ) | 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 red part adds 10 minutes to the alarm (10 times 60 seconds). If you want the automation to trigger 20 minutes after the alarm, change it to 20*60. If you want it to trigger 10 minutes before the alarm, change the + to a -. This is the offset from the alarm time itself.
  • The green part formats the offset 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 as expected.

Screenshot of home assistant template trigger that triggers an automation to run when your phone alarm goes off
Or in YAML format:
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) + 10*60 ) | 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.

Home assistant screenshot showing a state condition to check if you are home.