Sync IOS 17 Sleep alarm to HA (take 2)

While using the Sync iOS 17 Sleep alarm to HA by the amazing M20Lucas. I noticed it wasn’t working properly with few scenario. Since I worked on a total rewrite of that feature and it should now handle on top of his work:

  • Different Timezone
  • Update with alarm in the same day (past midnight sync)
  • Detect the next alarm (with limitation)
  • Allow better multi schedule management (with limitation)

Also, his method and this method are technically totally different. Here the main lift and conversion is done through a script in HA instead of a Shortcut in iOS.

I create a new thread to make sure that bug report between the two method are not mixed in one thread.


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 two ‘datetime’ helper within HA )
  • One will be for the next alarm
  • One will be for the next alarm skipped
    – 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. Install this blueprint in HA: IOS sleep data receiver · GitHub
  2. Make a script from this blueprint using the two datetime mentioned earlier
  3. On your iPhone, download a copy of the Shortcut here .
  4. Set the HA server and update the entity id of the script to match the one created

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 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.

Notes
M20Lucas if you read that message, thanks a lot! If you want me to change this description or post, let me know :slight_smile:

2 Likes

Thanks a lot for this automation @holblin. It works great.

One thing that doesn’t work however is changing the time of the wake-up alarm for one-time only.
I.e. when I have set a scheduled wake up alarm in the Clock/Health app for i.e. 6:00, then use the slider to change it to something else like 7:00 and when prompted with “Would you like to apply this change to all weekdays in this schedule?” i select “Change Next Alarm Only”, the HA script sets the next alarm datetime to 1 Jan 2000.

It is very useful for me to have a set schedule but also be allowed to adjust it for the next day only, would this be possible to support in your script?