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:

6 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!

Do you know if iOS18 affected this? I tried to implement this on my iPhone running iOS18 but it does not seem to work (granted I am very new to HA so it might be on my end too)

These 3 simple steps work well with iOS 18:

STEP 1 - Create an automation with a webhook trigger:

alias: Wake-up automation
description: ""
triggers:
  - allowed_methods:
      - POST
      - PUT
    local_only: true
    webhook_id: "-pxW2lkwbqfy3gXC0qfPy0jIW" # define your own secret webhook_id
    trigger: webhook
actions:
  - variables: # time_ahead is the amount of seconds you want the below actions to be triggered before the alarm goes off
      time_ahead: 120
  - action: persistent_notification.create
    data:
      title: Wake-up trigger confirmation
      message: >
        {% set time = strptime(trigger.json.message.replace('.', ':'), "%H:%M")
        %} {% set new_time = (as_timestamp(time) - time_ahead) |
        timestamp_custom("%H:%M") %} The next wake-up actions will be triggered
        at {{ new_time }}.

        This message will self-destruct at that time.
      notification_id: "1234"
  - wait_template: >-
      {% set alarm_time = trigger.json.message.replace('.', ':') %} {% set now =
      now() %} {% set today = now.date() %} {% set alarm_datetime =
      today.strftime("%Y-%m-%d ") + alarm_time %} {% set alarm_timestamp =
      as_timestamp(strptime(alarm_datetime, "%Y-%m-%d %H:%M")) %} {% if
      alarm_timestamp < as_timestamp(now) %}
        {% set target_datetime = (today + timedelta(days=1)).strftime("%Y-%m-%d ") + alarm_time %}
      {% else %}
        {% set target_datetime = alarm_datetime %}
      {% endif %} {% set target_time = as_timestamp(strptime(target_datetime,
      "%Y-%m-%d %H:%M")) %} {% set wait_time = target_time - time_ahead %} {{
      as_timestamp(now) >= wait_time }}
  - action: persistent_notification.dismiss
    metadata: {}
    data:
      notification_id: "1234"
mode: restart

The automation above checks whether the sleep alarm received from the iOS Shortcut automation is less than now (e.g. 8 am if the automation is triggered at 10 am). If this is the case, it defines the target_datetime as tomorrow at 8 am. If not, the datetime is today.

The automation uses a wait_template which subtracts a defined number of seconds (time_ahead) from the target_datetime, thereby triggering the desired actions with any desired offset before or after the alarm.

By using a webhook and local IP you automatically ensure that the wake-up automation only runs when you are at home. If you are travelling and in different time zones, your family will not suffer any unintended wake-up automations.

STEP 2 - install this simple iOS shortcut

https://www.icloud.com/shortcuts/029ab5f2503a4a3eaa549f7fdeae1048

STEP 3 - Set an automation on your iPhone to run the shortcut every night

Since iOS shortcuts cannot automatically cancel any existing alarms set i HA if a sleep alarm is skipped or deleted, I found that the safest solution for most people is to have the iOS shortcut (only) run at 3 am every night. This way the automation will only run if the alarm is still enabled at 3 am. It also means that the wait_template will only run for a limited amount of hours and is unlikely to have been interrupted due to automation modifications or server updates.

The above also works even if you modify the sleep alarm time for a single day as long as you make any final alarm adjustments before 3 am.

This worked like a charm; until IOS 18.2 in my case.
HA always received an empty string from the IOS shortcut.

I had to change “Set variable sleepAlarm to Time”
And in the “Time” value select the following: