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…
On your iPhone, download a copy of the Shortcut here.
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.
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.
Within the Shortcuts-app, go to the Automations tab (at the bottom).
Create a new Automation, by pressing the + in the top right.
Two options:
a. Choose Sleep → Wind Down Begins + Run Immediatly → Next
b. Choose App → Select Health and Clock in the list + uncheck Is Opened + select Is Closed + Run Immediatly → Next
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!).
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’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 ). Could you check if both 'Adjusted date’s are formatted as ISO?
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.
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.
So what worked for me was removing the variable “ Hours” (that comes from the alarm) from the “Add Hours hours to Adjusted time” block. Then I added back in the “ Hours” variable and then repeated the same thing with the “ 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 “ Hours” and “ 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.
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.