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:

5 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?

2 Likes

Thanks a lot, was looking for a way to sync my alarm.

Unfortunately, just as @nf0 mentions, selecting “Change next alarm only” returns 1 jan 2000 @ 00:00 and thus nothing will happen.

I wish I could contribute, but I’m a noob in coding.
Would be nice if this also can made possible.

Though, thanks for sharing your shortcut.

This is a dream… but I also find myself in the same situation described above… I wonder if it can be resolved.

I’m using ‘take 1’ but this is brilliant!

I’m also getting only 1 jan 2000 @ 00:00 on next and skip. Can you help us out?

First of all thank you for this shortcut and HA guide.
For myself I fix the 12:00 issue by simply changing my schedule every time I change my alarm. It’s already in my habits of setting an alarm every night.

I was able to get my next_alarm working, but now I needed a way to set an automation for my light to gradually open 30 minutes before that time. So with the help of chatGPT here’s what I was able to create and it did work!


Creating a Philips Hue Wake-Up Light Automation in Home Assistant

Objective

To set up a Home Assistant automation that gradually brightens your Philips Hue lights 30 minutes before your alarm time, which is set via an iOS shortcut to input_datetime.next_alarm.

Step 1: Set Up input_datetime and Create Template Sensor

We use an input_datetime to store the alarm time and create a template sensor to calculate the start time for the wake-up light, which is 30 minutes before your set alarm.

  1. Ensure Your input_datetime Is Set Up:
    Make sure you have an input_datetime entity named input_datetime.next_alarm.

  2. Create a Template Sensor in Home Assistant UI:

    Go to Settings > Devices & Services > Helpers > + Add Helper > Template Sensor and use the following details:

    • Name: Wake-Up Light Start Time
    • State template:
{{ (states('input_datetime.next_alarm') | as_datetime - timedelta(minutes=30)).strftime('%H:%M') }}

This calculates the start time for the wake-up light.

Step 2: Enable time Sensor in configuration.yaml

To make sure Home Assistant can trigger at an exact time, add the following configuration in configuration.yaml to enable the sensor.time:

sensor:
  - platform: time_date
    display_options:
      - 'time'

Restart Home Assistant for the change to take effect.

Step 3: Create the Automation for Gradual Brightness

To set up the automation for your wake-up light, create a new automation in Home Assistant using the following configuration:

alias: Wake-Up Light Automation
trigger:
  - platform: template
    value_template: "{{ states('sensor.time') == states('sensor.wake_up_light_start_time') }}"
condition: []
action:
  - service: light.turn_on
    target:
      entity_id: light.your_light_name  # Replace with your actual light entity
    data:
      brightness: 255
      transition: 1800  # Gradually brighten over 30 minutes
  • trigger: Uses a template trigger to check when sensor.time equals the calculated Wake-Up Light Start Time.
  • action: Gradually brightens your Philips Hue light to full brightness over 30 minutes (1800 seconds).

Step 4: Exclude sensor.time from the Logbook

To keep your Logbook clean and avoid minute-by-minute entries, exclude sensor.time:

logbook:
  exclude:
    entities:
      - sensor.time

Restart Home Assistant for these changes to take effect.


With these steps, you’ll have a wake-up light that gradually brightens to 100% over 30 minutes before your set alarm time, all handled automatically by Home Assistant!

Sadly I noticed that the apple shortcut got few issues with snoozed alarm and so on… I will publish my last up to date setup but I have stop using it for some time due to the issues mentioned above. Hopefully iOS 18 will help solving theses issues!