"Next Alarm" automation event

Hey there!

I would find very useful a new automation event, containing the next upcoming alarm, that could fire whenever an alarm rings or is edited.

My usecase is as follows:
I’ve got SleepAsAndroid pushing events to an MQTT queue, and Home Assistant listening for them. I want to know when the next alarm is, so I can dim my lights 8 hours before the alarm, or have an announcement like “Your next alarm is in X hours Y minutes” on my speakers.

Would it be possible to have a new automation event (containing the time and name of the next alarm) that fires whenever:

  1. Any alarm is edited or disabled (There’s already an ALARM_RESCHEDULED, but that’s different. If I have an alarm A for 9am and an alarm B for 10am, and I change B from 10:00 to 10:30, I’d still want to receive “NEXT_ALARM A at 9:00”, while ALARM_RESCHEDULED notifies about alarm B)
  2. An alarm rings (if I have an alarm that rings daily at 9am, when it rings on Monday the event should get sent out, because now the next alarm is at 9am on Tuesday)

I think this could fix issues the home assistant community is having when trying to integrate with SleepAsAndroid (as currently the only real way to get the next alarm is through a home assistant sensor that’s extremely buggy and unreliable). There’s a few posts of people having issues with the next alarm sensor, that having this event would 100% fix:

  1. https://old.reddit.com/r/homeassistant/comments/mi3hj1/a_rambling_post_about_using_sleep_as_android_in/
  2. Next Alarm sensor not updating / stuck in the past (Samsung Galaxy S22) - Home Assistant Companion for Android - Home Assistant Community
    I’ve had my own issues with this sensor (I set an alarm for tomorrow at 8 am, then immediately changed it to 9am. The next alarm sensor is still announcing the next alarm is at 8 am, not terribly useful)

+1 for requesting a NEXT_ALARM intent from Sleep as Android.

I’ve also seen the Android OS level next alarm notification be weird (eg. the next alarm is constantly 10 minutes in the future), but I haven’t noticed it being weird recently so maybe it’s fixed now? I also didn’t realize the Home Assistant app can read it, I just enabled it so I’ll track the next alarm time for a week or so to see if it’s reliable enough to use for automatons.

I currently use Tasker to watch Sleep as Android intents to tell Home Assistant to turn the lights off when I start sleep tracking and turn the lights on when either the alarm rings or I stop sleep tracking. My use case for wanting the actual next alarm time is to have the heat turn on about 30 minutes before the next alarm so the house is warmed up a bit before I get out of bed.

Well, after like 5 minutes of testing I’ve discovered it’s Tasker that’s doing the “next alarm is constantly 10 minutes in the future” and it’s apparently a known issue with how Tasker is able to wake up and do stuff while the phone’s in doze mode. When watching the entity in Home Assistant I can see as soon as I lock my phone Tasker “sets an alarm” for 10 minutes in the future and as soon as I unlock my phone Tasker “unsets the alarm” and the Sleep as Android next alarm time shows up again.

The good news is that Home Assistant is aware of this and the entity reports the package that sets the alarm so it’s possible to distinguish Tasker fake alarms from real alarms. I’m going to try making a helper entity that gets updated whenever Sleep as Android shows up as the next alarm as a workaround to always know the next real alarm time.

1 Like

I got a Home Assistant automation working to set a helper input_datetime entity only when the phone app reports the next alarm time from Sleep as Android. Since Tasker and Sleep as Android basically trade off who is reporting the Next Alarm Time to the Home Assistant app, the helper entity will be constantly updated every time I unlock my phone. This won’t really solve IcerOut’s issue of their Home Assistant app not always reporting immediate alarm time changes since they don’t have Tasker which forces the next alarm time to change a lot and always be up to date. A NEXT_ALARM intent from Sleep as Android would still be a good thing to implement to make 100% sure everyone can always know the next alarm without needing something like Tasker that accidentally fixes it in my case.

I use Node-RED instead of the builtin Home Assistant automations, but hopefully this pseudocode version of what I made helps anyone else who wants to do something similar:

State Node for when the Next Alarm time reported from the phone app changes. This node passes along the following messages:
msg.payload is the entity state which is the next alarm time in UTC
msg.data is the whole event data which includes the app that set the next alarm time

Switch Node to only keep going when “msg.data.new_state.attributes.Package == com.urbanandroid.sleep”

Date/Time Formatter Node to convert the msg.payload entity state from the UTC timestamp to unix timestamp in my local timezone

Call Service Node to set_datetime on the helper input_datetime entity with the Data {“timestamp”: payload }

Time Node on my helper entity with an offset of -30 minutes to call my existing “set daytime temperature” flow so my thermostat can warm up the house before I wake up.

1 Like