← Blog

Link iOS Sleep Schedule with Hue Light Bulbs using Shortcuts and Hue API

iOS
Shortcuts
Productivity
published on:
📚  This article is part of a series on 
iOS
Shortcuts
, and 
Productivity
:

After my recent endeavor of creating a home screen widget for the number of visitors currently present in my gym, I decided to take a look at the Shortcuts app again. This time, though, I wanted to link my sleep schedule setup in iOS with my Hue light bulbs1. Until now, I configured my sleep schedule in iOS to be different on weekdays and weekends. Accordingly, I configured two wake-up automations in the Hue iOS app to start waking up naturally shortly before the alarm clock of the sleep schedule. However, in practice, I tend to adapt the sleep schedule daily depending on my plans for the next day. Therefore, I want to link the sleep schedule with the Hue light bulbs in a way that the wake-up automation is automatically adjusted when I change the sleep schedule in iOS such that I do not have to manually configure the wake-up automation every time.

Recognize the Sleep Schedule with Shortcuts

Unfortunately, automatically recognizing (a change in) the sleep schedule of iOS seems to be impossible. Hence, I evaluated other possible triggers for automation in iOS Shortcuts:

  1. Alarms/Wake-Up: I could automatically trigger a shortcut when the alarm clock of the sleep schedule is triggered. However, this would not allow me to let the light bulbs go off gradually before the alarm clock rings.
  2. Wind Down Begins: The wind-down period of the sleep schedule is supposed to help you to get ready for bed. In Shortcuts, it is possible to trigger a shortcut when the wind-down period starts. I often change my sleep schedule shortly before going to bed. Therefore, this is not a reliable trigger for my use case.
  3. Time of Day: Since I do not work shifts and have a time window between 3 am and 6 am in which I am usually asleep, I can use the time of day as a trigger for the shortcut.

So, I create an automation in Shortcuts that runs at 04:30 am to get the time at which the sleep schedule is set to wake me up. Here is another problem, though. Getting the time of the next alarm that is part of the sleep schedule is not straightforward. I could not find any action that would return the time of the sleep schedule's next alarm through Shortcuts. However, the sleep schedule happens to create an alarm for the next wake-up time. Enumerating all alarms in Shortcuts returns this alarm along with the associated wake-up time. Unfortunately, this enumeration contains the alarms for the most recent wake-up time and for the sleep schedules on weekdays and weekends. Apparently, the first alarm is the next wake-up time, the second alarm is the wake-up time of the sleep schedule on weekdays, and the third alarm is the wake-up time of the sleep schedule on weekends. If the next wake-up time does not deviate from the corresponding sleep schedule, the enumeration only returns two alarms for the wake-up time of the sleep schedule on weekdays and weekends. In that case, the first alarm is the one to get on weekdays and the second alarm is the one to get on weekends. Together, the following actions return the time of the next alarm that is part of the sleep schedule and store it in a variable called alarm:

The actions of a shortcut to get the time of the next alarm that is part of the sleep schedule

The actions of a shortcut to get the time of the next alarm that is part of the sleep schedule

Use the Hue Schedule API for the Wake-Up Automation

Now that I have the next wake-up time, I can schedule the light bulbs to turn on at that time using the Hue API v1 which requires a Hue Bridge1. Using the Schedules API, I can create a schedule that turns on the light bulbs at the next wake-up time. Inspired by a wake-up sunrise simulation for Home Assistant, I create two schedules to gradually fade in the lights. The first one turns on the light bulbs at the next wake-up time with the lowest possible brightness and a red hue:


{
"name": "wakeUpOffset",
"command": {
"address": "/api/<username>/groups/1/action",
"body": {
"on": true,
"bri": 1,
"ct": 500,
"xy": [0.7, 0.375]
},
"method": "PUT"
},
"localtime": "<date>T<timeOffset>"
}

The first schedule, named wakeUpOffset, runs at the timestamp specified in localtime where <date> corresponds to the date of the next wake-up alarm and <timeOffset> corresponds to the point in time 2 minutes and 31 seconds before the wake-up alarm. That is one second before the second schedule runs. The command to issue addresses the group of lights with the ID 1 (refer to the Groups API) and turns on the lights with the lowest possible brightness at the lowest possible color temperature with a red hue. In address, <username> corresponds to my username for the Hue Bridge.

The red hue corresponds to the chromaticity coordinates of the Hue light bulbs in the CHI chromaticity diagram from the Developer documentation:

The CHI chromaticity diagram of the Hue light bulbs

The CHI chromaticity diagram of the Hue light bulbs

The second schedule, named wakeUp, is very similar to the first one. Instead of <timeOffset>, the schedule runs at <timeTransition> corresponding to the point in time 2 minutes and 30 seconds before the wake-up alarm. Over five minutes (i.e., 3000 × 100 ms), the brightness increases from 1 to 175 and the hue changes from red to yellow:


{
"name": "wakeUp",
"command": {
"address": "/api/<username>/groups/1/action",
"body": {
"on": true,
"bri": 175,
"ct": 500,
"xy": [0.5, 0.4],
"transitiontime": 3000
},
"method": "PUT"
},
"localtime": "<date>T<timeTransition>"
}

I create these schedules by sending two API requests in Shortcuts. The two actions to do so look as follows:

Two Hue API requests in iOS Shortcuts to create schedules

Two Hue API requests in iOS Shortcuts to create schedules

Besides the discussed variables, there is bridgeIP which stores the IP address of the bridge that I got by navigating to discovery.meethue.com. You can find the complete shortcut here.

Footnotes

  1. This is an Amazon affiliate link. If you buy something through this link, I will get a small provision. This does not change the price for you. 2