Starting an Alarm or Timer on an Android phone

I’d like to create an automation in Home Assistant with which I can start an Alarm/Timer on my Android smartphone. There weren’t a lot of posts on how to do this other than using the Home Assistant Companion app’s notifications, which don’t fit my requirements as I always have my notifications silent with vibrations. Mossaddeque Mahmood however created this post detailing his method for using Android Intents to create an Android timer set to 1 second.

I followed his guide and ended up with the following automation:

alias: Start Phone Alarm
description: Start Phone Alarm
trigger: []
condition: []
action:
  - service: notify.mobile_app_mi_mix_3
    data:
      message: command_volume_level
      title: 20
      data:
        channel: alarm_stream
  - service: notify.mobile_app_mi_mix_3
    data:
      message: command_activity
      data:
        channel: "com.google.android.deskclock"
        tag: "android.intent.action.SET_TIMER"
        group: "android.intent.extra.alarm.SKIP_UI:true,android.intent.extra.alarm.LENGTH:1"
mode: single

When I run this automation the alarm volume on my phone is updated, but no 1 seconds timer is set/started. I however get a brief Unable to send activity intent, please check command format error popup.

What would I need to change in the automation to get to the correct command format / are there alternative methods to achieve this?

2 Likes

This calls for the Google Clock app. It must be installed on the phone in order for this to work.
Android Clock App

@Schmetis @achapa45 is there a way to do this with the installed Samsung clock app.? I’ve just been trying to VIEW it before I try to set the timer but I can’t even get it to VIEW.

I have followed what’s posted in this thread, on two different phones, with great success in the last months. My two cell phones stopped reacting to this script correctly recently, not both at the same time, but eventually both stopped working correctly.

Now, ringer is very faint, not the same type and the phone shows this notification:

I am not sure what broke this script; android update? Companion app update? Any ideas ?

Got my answers here, some breaking changes in the companion app: Notification Commands | Home Assistant Companion Docs

It becomes:

  findcellmath:
    mode: restart
    sequence:
    - service: notify.mobile_app_pixel_4a
      data:
        message: "command_volume_level"
        data:
          media_stream: "alarm_stream"
          command: 20
    - service: notify.mobile_app_pixel_4a
      data:
        message: command_activity
        data:
          intent_package_name: "com.google.android.deskclock"
          intent_action: "android.intent.action.SET_TIMER"
          intent_extras: "android.intent.extra.alarm.SKIP_UI:true,android.intent.extra.alarm.LENGTH:1"

I’m running into the same issue as @Schmetis
I can’t get the timer to start with the length.

It does open the clock app to a new timer.
So both extras are not working.

I have this set up for alarms and it works just fine

alias: set android timer (Duplicate)
sequence:
  - service: notify.mobile_app_pixel_6
    data:
      message: command_activity
      data:
        intent_package_name: "com.google.android.deskclock"
          intent_action: "android.intent.action.SET_TIMER"
          intent_extras: "android.intent.extra.EXTRA_SKIP_UI:true,android.intent.extra.EXTRA_LENGTH:500"
mode: single

Since Google sent me here, I’m posting in case it’s useful to someone else:

Alarm will trigger one minute after the command:

service: notify.mobile_app
data:
  message: command_activity
  data:
    intent_action: android.intent.action.SET_ALARM
    intent_extras: >-
      android.intent.extra.alarm.HOUR:{{ now().strftime('%H')
      }},android.intent.extra.alarm.MINUTES:{{ (now() +
      timedelta(minutes=1)).strftime('%M')
      }},android.intent.extra.alarm.SKIP_UI:true

Timer will trigger one second after the command:

service: notify.mobile_app
data:
  message: command_activity
  data:
    intent_action: android.intent.action.SET_TIMER
    intent_extras: android.intent.extra.alarm.LENGTH:1,android.intent.extra.alarm.SKIP_UI:true

1 Like

This does nothing on my phone. Pixel 7 Pro.

I just edited the previous post and removed the package name, that way it calls the default app for the phone. Also make sure that HA has permissions for alarms and appearing on top of other apps.

Awesome. That worked. Is it possible to add a name to a timer or to an alarm through this method?

Edit: Nevermind. I found it. I just add this to the intent_extras:

android.intent.extra.alarm.MESSAGE:<message>
1 Like

This seems no longer to work for me.
All I get is a Homeassistant notification with the text “command_activity”. (like shown in Starting an Alarm or Timer on an Android phone - #4 by Mathieu_Lafrance)

But I am sure it worked a year ago.

There is one little bug in the alarm code from @carlos.vroque above. If you set the alarm in the last minute of hour (say at 7:59 for an alarm to go off at 8:00) then it sets the alarm for the previous hour (7:00 in my example) and would go off the next day instead of the next minute. It’s a simple fix where you need to add the 1 minute to the hour as well as the minute calculation:

service: notify.mobile_app
data:
  message: command_activity
  data:
    intent_action: android.intent.action.SET_ALARM
    intent_extras: >-
      android.intent.extra.alarm.HOUR:{{ (now() +
      timedelta(minutes=2)).strftime('%H')
      }},android.intent.extra.alarm.MINUTES:{{ (now() +
      timedelta(minutes=2)).strftime('%M')
      }},android.intent.extra.alarm.SKIP_UI:true

Also I will note that anybody using this code will want to update the service target (notify.mobile_app in this example) to your actual device target (ex: notify.mobile_app_coderanger_s_phone). You can find this value in Developer Tools / Services by searching for “notify.mobile_app”. Your device will be listed with the correct notify.mobile_app_xxx service name underneath it.

Edit: It’s safer to set the alarm 2 minutes instead of 1 minute in the future as well. While testing I ran into a issue where by the time the phone got the alarm setting it has passed and was set for the next day. This happens because the next minute could be only 1 second away (between 1 and 60 seconds away). Setting it to 2 minutes in the future guarantees at least a minute (between 61-120 seconds away).

1 Like

I also had problem to make it working. It worked only when I have unlocked the phone and home assistant opened and visible at front. To make it working in general I had to add two additional permissions to Home Assistant application, mainly: ‘Show on Lock screen’ and ‘Open new windows while running in the background’. These permissions are under ‘Other Permissions’ in my phone.
At this moment I am able to set alarm automatically even though my phone is locked. I use android 13 with MIUI 14.0.5 (Xiaomi).