Sync iOS 17 Sleep Alarm to HA

Intro
Since iOS 17, my previous solution to send my sleep alarm time to Home Assistant, doesn’t work anymore. Thanks Tim Apple! 🥲

This time around, I tried to make the setup a lot simpler. I made the Shortcut a little easier to setup, I won’t try to explain every detail/step of the Shortcut here (but see my many notes within the Shortcut if you’re interested) and I’ll try to answer questions if possible (or, again, see my notes in the shortcut itself). But I found out the hard way, my time and energy are limited… Here is my personal solution for sending my sleep alarm time to HA:


Pre-setup
For this to work you need the following:

  • iPhone running iOS 17, with the following apps installed:
    – Clock
    – Health
    – Shortcuts
    – Home Assistant

  • Setup a sleep alarm cycle
    – either through the Clock or Health app – Apple instructions
    – ‘regular’ alarms won’t work with this Shortcut (though Apple did make it easier to sort and select these alarms within Shortcuts, but you’ll have to write your own Shortcut)

  • Create a ‘datetime’ helper within HA
    – You need to know its entity id (similar to input_datetime.example)
    – Make sure it is a date and time sensor, not just time or date.
    – Not sure how? Read this.


Setup
On to the real stuff…

  1. On your iPhone, download a copy of the Shortcut here.
  2. Answer the questions in the setup of the Shortcut and run it for the first time.
    You might be asked to give this shortcuts permission to access your sleep alarm and/or to send data to HA: just select always.
  3. Done! Well… sort of…

Post-Setup
Apple’s way of automating things with Shortcuts differs from how it works in HA. A shortcut like the one you just downloaded, is basically just the ‘action’ part of an automation (or a script) within HA. If you tap on the Shortcut on your iPhone, it runs, but there aren’t any triggers that run the Shortcut automatically.
I personally use two ’triggers’ to run this Shortcut. Either when I close the Health or Clock app (I probably changed the alarm), or ‘When Wind Down starts’. The ‘Wind down option’ is part of the sleep alarm schedule and I’ve set it to 1 hour before my actual bedtime. But there are more options available as triggers.

  1. Within the Shortcuts-app, go to the Automations tab (at the bottom).
  2. Create a new Automation, by pressing the + in the top right.
  3. Two options:
    a. Choose SleepWind Down Begins + Run ImmediatlyNext
    b. Choose App → Select Health and Clock in the list + uncheck Is Opened + select Is Closed + Run ImmediatlyNext
  4. Look for the Shortcut you just downloaded in My Shortcuts and select it.

That’s it. I then use HA to calculate different times and trigger certain automations based on my alarm time. Shortcuts is cool and all (and it could be used to do way more with HA as well), but it’s not very easy to use IMHO.


Bonus
I’ve created a stripped down version of this Shortcut+Automation to just send the current date and time to HA when I wake up (read: when I turn off the alarm). The Shortcut can be downloaded here and the Automation that runs it is called When Waking Up (what a surprise Apple!).

15 Likes

Thanks for the new version!

I’m having an issue here though…

Looks like Shortcuts can’t send the data to your HA server. Check if the shortcut has permission to send data to HA:

  • press the i at the bottom
  • go to the Privacy tab
  • turn permission on for HA

Yep it’s all good on privacy. And even the old shortcut gets through that point :thinking:

Could you check the formatting of the datetime within the dictionary?

Tap the Adjusted date right above the HA service call.

Check if the date and time is set to ISO8601. If this part isn’t formatted right, this could give your error.

Do you know if it is possible to sync the time regardless if the alarm is enabled for the next sleep plan?

I have a setup where my Sonos speaker wakes me slowly with increasing volume while my Philips hue lamp slowly increases in brightness. It would be great to be able to set the wake up time by editing my sleep plan, however I don’t want an alarm from my phone / Apple Watch:)

Why would you even want to use the sleep alarm on your iPhone if you don’t want the alarm to go off? I suggest you simply use HA to set a time (with an input datetime helper) to run your wake-up automation. That way you keep it as simple as possible, less chance of failing.

But if you must, you could add ‘Edit sleep alarm’ after the first service call to HA within the shortcut, and set it to ‘turn off’. After sending the datetime to HA, your sleep alarm will be skipped for the next day (only).

I’ve got this setup but it doesn’t seem to be updating the helper in HA this is what I get when I print the dictionary result

Is this the normal result?

No it’s not. It ought to be just a single datetime, not four, like yours…

I’m unsure why you got 4 different date times, instead of one. You could add ‘show result’ at the different stages to see where it goes wrong.

Yeah it looks like it starts at adding alarm hours to adjusted time

I figured it out. I had to reset the hours and minutes to edit sleep time. It must have gotten unsynced or something.

I’m guessing you’re on the other side of the Atlantic (given the date formatting)? Does the ‘get start of day’ from ‘tomorrow’ give you 25 Oct 2023 0:00? Or something else?

It might be the formatting (again :persevere:). Could you check if both 'Adjusted date’s are formatted as ISO?

I’ll try to look into it this week and come up with a solution, but I’m to tired to think of something else now, so I’m of to bed.

It feels truly great that, after finding out that my automations doesn’t work after updating to iOS 17, someone adressed the topic, and created and updated Shortcut. Even with Import questions that makes the process extremely easy! Much much thanks @M20Lucas .

Also, I’d like to ask you: how could I subtract X minutes from the datetime sent to HA? In order to start doing some things before I wake up.

Thanks again.

I’m seeing this same issue, and I’ve tried all the suggestions mentioned so far. I’m curious if anyone else who’s gotten this error has figured anything out.

UPDATE: Sorry, I should have specified I’m seeing the same error that @Zipties was seeing.

UPDATE 2: In case it helps troubleshoot, the stripped down version of the shortcut works as intended.

UPDATE 3: I tried running the shortcut while looking at the logs in Home Assistant, and I get the following error in the logs when I run the shortcut. Not sure how/if this helps.

Error when calling service during mobile_app webhook (device name: Sean’s iPhone): Invalid datetime specified: ['2023-10-27T12:30:00-04:00', '2023-10-26T00:30:00-04:00', '2023-10-26T12:30:00-04:00', '2023-10-26T00:30:00-04:00'] for dictionary value @ data['datetime']

UPDATE 4: I tried adding “Show Result” each step of the way in the shortcut, and below are screenshots of what I got each step. Also, for what it’s worth, my sleep alarm is set for 7:00 AM tomorrow.




I love that :slight_smile:

Can you provide more details on exactly what you mean by this? I think I may be running into the same issue you were.

Seeing the same.

So what worked for me was removing the variable “:clock1: Hours” (that comes from the alarm) from the “Add :clock1: Hours hours to Adjusted time” block. Then I added back in the “:clock1: Hours” variable and then repeated the same thing with the “:clock1: Minutes” variable and that seemed to fix my problem of multiple times appearing. Let me know if that makes sense or not and I can try to explain it in more detail or take some more screenshots.

Removing hours and minutes like @Stevebutler18 mentioned didn’t work for me, but removing the “Adjusted Date” variable and re-adding it worked. You may try that if removing and re-adding the “:clock1: Hours” and “:clock1: Minutes” doesn’t work for you.

I’m wondering if it’s a locale issue with the shared shortcut (I’m in the US). I was able to share the edited shortcut to my wife’s phone with no issues once I dropped and re-added the variable.

1 Like

For some reason I’m still seeing the issue. I even tried recreating the shortcut from scratch. As soon as I put the add hours and add minutes actions in there, it starts duplicating the number of entries in the end result. I always end up with a list of like four date/times by the end.