Sync iOS 17 Sleep Alarm to HA

I was using this shortcut, which was working great until this morning when we switched into GMT out of BST.
Now, Home Assistant is reading the time sent (ISO 8691) seemingly ignoring the +01:00 timezone.

Any ideas how to solve this without a manual check for a time with an hour offset?

I’m using a function in node red to fire my lighting scene 2 minutes before the alarm is scheduled if you haven’t found a way yet I can dig out the code

Not having any luck with that either, it’s still pulling multiple days. Any chance you could share the one you created?

So, I ended up going a slightly different route to achieve the same result. Since I couldn’t figure out the previously mentioned issue, I set up a shortcut that will send the time of my next sleep alarm (without any date info) to a time only helper I set up in HA. I then made a script (with some back and forth assistance from ChatGPT) that will combine the time info from that helper and tomorrows date and add it to another helper I created in HA. I then set up an automation to automatically run the script any time the time info from the first helper is updated. It may be a more complicated way of doing it, but it seems to be working. Below is the script I’m using in case anyone else wants to give it a try.

service: input_datetime.set_datetime
data: {}
target:
  entity_id: input_datetime.wake_up_alarm
data_template:
  datetime: >
    {{ (now().date() + timedelta(days=1)).strftime('%Y-%m-%d') ~ ' ' ~
    (state_attr('input_datetime.iphone_alarm', 'hour') | string) ~ ':' ~
    (state_attr('input_datetime.iphone_alarm', 'minute') | string) ~ ':00' }}

Nope, I haven’t found a way yet. If you could share the code it would be great. Thanks!

Hi,

Wondering if anyone else has faced the same problem or bug in Shortcuts app? When running the shortcut, toggle sleep alarm does not turn the alarm on (or back on), if it is off by default in the schedule. Figured this out by this simple shortcut, when running it with the schedule not having alarm on by default for tomorrow. Works perfectly, if alarm is on by deault.

Tried to have two schedules with alarm on for the weekdays and off on the weekend by default, but could not make it work due to this bug. Wether having one or two schedules does not affect to the outcome.

Great, it seems to work great on my iPhone and my HA. In the next few days I will see the behaviors in the field.

I set the automation trigger when the iPhone is charged, so every night before going to sleep. Using the MagSafe, if I had to change the alarm time, I would have to detach it from the magnet, and so when I reconnect it it will update the alarm time to HA again.

Thanks for sharing Maarten!

An old automation I built also stopped working, but I found this one in the process. Will link to this one from the reddit post!

Exactly what I was looking for.
Thanks!
I used to check my wakeup time in the clock app.
So now I have an automation that is triggered when I close my clock app to fire the shortcut you mentioned.
I adjusted the helper name so it is recognized for me and HA.

Now I can automate my light (ledstrip) to act as a wakeup light starting X time before the wakeup time!

Got it to work but it took longer to implement than expected.

I am in the US and I believe that the ISO formatting is not working for me. I had to change all the adjusted date from ISO to Custom: yyyy-MM-dd HH:mm:ss.

After the change, I no longer have the issue mentioned before with multiple dates showing. I believe that time format in the iPhone is most likely the issue.

Has anyone found a way to subtract some minutes to the alarm time? Maybe @M20Lucas you know how?

Much thanks.

Aha! After some poking around, I figured out what’s going on and why people are getting more than one time entry.

For some reason, iOS is operating on the “Adjusted Date” variable differently when ISO8601 encoding is enabled. It appears to be concatenating the different time operations instead of operating on the variable like you’d expect. The solution is to configure the first two instances of “Adjusted Date” using Date Format = Medium and Time Format = Short.

The formatting must be changed to ISO 8601 when the data is written into the dictionary:

This seems to have fixed my issues! Thanks for putting this together @M20Lucas!

1 Like

Hey all! Thanks a lot for the shortcut and fixing the issues :slight_smile: I got it working after adjusting the date to medium and also after syncing the date/time.

I am however, having one issue and that’s when I create the alarm after midnight, or in the same day I will be waking up. In this case, the shortcut creates a date one day after. Any idea how to add an IF value in there? Thanks a lot!

I use Node-red with the time node!

Try changing this part of the Shortcut from “Tomorrow” to “Today”.

I am facing this also. It seems this automation will disable any health alarm that is not a part of the schedule. Rather dangerous and I almost found this bug the hard way! lol

I’ll play around and see if I can find a solution.

ETA I can’t find a solution. Seems like a bug in the “toggle” function.

Somewhat hilarious that Apple didn’t write a function to just get the current alarm status.

Hi Marcos, I’m not used to work with that, please can you tell me how you do it?

Much thanks.

Works really wel together with this blueprint! Thanks!

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

@M20Lucas thank you very much for creating this post and the automation; the Shortcut and HA work very well together. I’d like to expand to the following use case: I have a dimmer light that I want to gradually start a certain amount of minutes before the actual alarm is set.

Basically to subtract a number (20) of minutes from the Adjusted Date:

I’ve checked that the value as calculated in this step is an ISO 8601 Date/Time Format. Unfortunately this adjustment does not propagate to the Date/Time Helper in HA. Does anyone know what goes wrong here? I prefer to do the adjustment already on the iOS side preferably and have an advanced date/time in HA.

For reference, this is the successive config I’ve set up for gradual increase of the brightness:

trigger:
  - platform: time
    at: input_datetime.wekker
action:
  - service: light.turn_on
    metadata: {}
    data:
      transition: 1200
      brightness_pct: 80
    target:
      area_id: bedroom
      entity_id: light.hanging_light_bedroom

Many thanks.

So I fixed this with a solution in HA through implementing a second date/time helper.
Useful thread that helped me in this solution: Input.datetime automation to offset another input.datetime

The config I implemented:

`alias: Time - Update offset for morning routing
description: ""
trigger:
  - platform: state
    entity_id: input_datetime.wekker
condition: []
action:
  - service: input_datetime.set_datetime
    data:
      time: >-
        {{ strptime(states('input_datetime.wekker'), '%H:%M:%S') -
        strptime('30', '%M') }}
    target:
      entity_id: input_datetime.wake_up_light
mode: single`