HomeKit Automation Tips

Getting more out of your smart home devices

Using both time of day and sunrise as a criteria in a shortcut

I was asked to create a HomeKit automation that turns on a certain light at 4:30AM if the sun has not already risen. This is a good exercise to play with Weather and the wealth of useful information therein.

We’ll start by opening either the Home or Shortcuts app. We then choose Automation (at the bottom) and press the plus sign (+) in the top right corner to add a new automation. Select Create Home Automation. Then select A Time of Day Occurs and set the Time of Day to 04:30. Instead of choosing a certain light or device, we scroll all the way down to Advanced and press Convert To Shortcut.

We’ll start the shortcut with Get current weather at Current Location. (You can put your address in the Current Location if you want to.) We put Get Details of Weather Conditions after that, and choose Sunrise Time as the detail we want.

Now we need to add an If statement. As Input we choose Current Date and as Condition we set is before, and for the last Date we choose Sunrise Time. We’ll add a Control Home action within the If statement where we choose the lamp or lamps we want to turn on, and set them to the brightness we wish. Finally we can remove Otherwise.

And that’s how simple it is. The current weather action has a lot of different data points that can be useful in many shortcuts. Your imagination is the limit.

18 thoughts on “Using both time of day and sunrise as a criteria in a shortcut”

  1. Somewhat related to this I would like to open a shade 30 minutes after sunrise. I would like to close a shade 45 minutes before sunset. These are Lutron based shades inside the house. And if I get the hang of this perhaps add a Phillips Hue outdoor motion sensor to take outside lux readings into consideration as well. The problem to solve is shades are open when it is too dark outside.

    I have Phillips Hue lights and motion sensors, Yale locks, First Alert Onelink, Lutron switches and shades, Trädfri shades, Abode security system that reports if doors are opened or closed to HomeKit, and even a LG television in HomeKit.

    Even though I have a lot of items in HomeKit I only use rudimentary automation and I am a beginner. For instance some of my Hue sensors are controlled by Hue for lighting automation as I could not do as well in HomeKit. If I get the hang of this I would like to tackle larger issues like a huge garage with multiple Hue motion/lux sensors, IKEA shades and Lutron light switch, but for now it is over my head. I also use the Hue motion sensors, not configured in Hue, to make sure my insulated garage stays above freezing.

    1. I think this might be impossible to do utilizing only HomeKit Shortcuts. However, if you’re willing to add a Raspberry Pi and run Homebridge on it (Homebridge can also be run on a Windows PC or Mac), you should be able to get it to work. Running it on a Raspberry Pi is comfortable, since it has to be running 24/7. I decided to go with Hoobs, which is a very user-friendly version of Homebridge.

      Running Hombridge, you could then install the Suncalc plugin, which will let you create triggers utilizing sunrise, and sunset. It will also let you set an offset, so that the trigger happens a certain before or after these events.

      You could always try it out running Homebridge on your computer to start with, and then when (and if) you get it to work the way you want to, you could think about getting a Raspberry Pi (they are very cheap) to run it on longterm.

      The Suncalc plugin will only work as the trigger, you would then create a shortcut to do whatever you want to as it is triggered.

  2. Hi Stefan,
    Thank you for all your tips, I’ve found them very useful and have used them as a basis for some of my own scripts.
    I wonder if you can help me please, I’m trying to get my head around something similar to this and am really struggling.
    I’m trying to set up a shortcut/script/automation based on a number of criteria including time of day, sunset and light level.
    I have a Hue motion (and light) sensor in an area which isn’t naturally bright but doesn’t need lights until an hour or so before sunset but I don’t want them coming on after midnight, however I do want it controlled by the ambient light sensor! Confused? I’ll try and sum it up.

    From an hour before sunset until midnight, if the ambient light is below xx lux and if motion is detected, the light should come on.

    I’ve spent the last couple of days working on this but I just can’t get the script right.
    I think if you could do a tip for this please, it’d be very useful and quite popular.


    1. Hi Keith, and thank you for the feedback and the question.

      I’m working on a solution for your problem, and I’ll write a blog post as an answer if that’s ok 🙂 It’s almost done, I just have to wait until midnight to see how it handles some of logic. I’ll let you know when the post is online, but it shouldn’t take long.

  3. Your blog is amazing! I am looking for an automation that sets different light intensity at different time of the night between sunset and sunrise when motion is detected.

    For example in my bathroom:
    Sunset to 0000: 80%
    0000 to 0230: 50%
    0230 – Sunrise: 30%

    I cant seem to get my head around it, and I’m wondering if you have any idea on how to script it?

    Thank you so much in advance 🙏🏽🙏🏽🙏🏽

    1. Hi Eli,
      Thank you so much. I’m happy you like it 🙂

      I made a shortcut that should work for you. I’m using the Format Date function (that I’ll do a blog post about soon) which is very powerful in this kind of shortcuts. I’ll write a short explanation what is going on in the shortcut, so you have an easier time to customize it as you see fit.

      The main idea here is really to get three different time values (sunrise time, sunset time and current time), turn them into number values, and save them into variables that we can use in if-statements.

      We start by getting the current weather at the current location. A good tip here is to put in your actual location instead of current location. It speeds it up a bit as it doesn’t need to figure out your location first.

      We then get the Sunrise Time from Weather Conditions.

      Next, we’ll format the date of the sunrise into a custom string with the format Hmm (it’s important that we use capital H, but lowercase mm). This will strip out the date and only give you the time in 24-H format.

      After that we’ll use Get numbers from input to convert the text value into a number value.

      And then we can take that number value and put it into a variable, which I named SunriseTime.

      Now, when we do the same for Sunset Time, you’ll need to link the Weather Conditions to the very first action you did (Get current weather at … location). You select “Choose magical Variable”, scroll all the way up and select Weather Conditions that is below the first action and select it.

      After this we do the same steps for Sunset Time as you did for Sunrise Time and save it into the variable SunsetTime.

      Next, we use the Format Date, using Current Date as the input and do the same formatting and steps as for Sunrise Time and Sunset Time. After we’ve saved it into a variable called TimeNow, we can start with the real logic in this shortcut.

      Now, I’ve nested three if-statements inside each other, but it’s also possible to just put three if-statements after each other, removing the Otherwise. If you do that, just end one if-statement before you start the next.

      In the first if-statement we check if TimeNow is between 0 and 230 (in reality midnight and 2:30 AM). If this is true, we’ll turn on the lights to 50%.

      In the second if-statement we check if TimeNow is between 231 and Sunrise Time (we use 231 here because “is between” is inclusive, and we don’t want the possibility that more than one if-statement can be true). If this is true, we turn on the lights to 30%.

      In the last if-statement we check if TimeNow is between SunsetTime and 2359. If this is true, we’ll turn on the lights to 80%.

      If none of the if-statements are true, nothing will happen. If you want the lights to turn on to a fourth brightness level if TimeNow is between sunrise and sunset, you would need to add a little bit.

      I hope this helps. Please let me know if you get it working or if you’ve got any other questions 🙂

      Shortcut for Eli

          1. Do you only want them to shut off for between SunriseTime (or did you mean SunsetTime?) and 2359? Since you’re using the motion sensor to turn them on, I would suggest using the motion sensor to turn them off with a different shortcut like here: https://homekitautomationtips.com/how-to-use-a-motion-sensor-to-turn-on-and-off-your-lights-with-homekit/

            If you do that, you can make it turn off the lights 3 minutes after no motion is detected anymore. However, if during those 3 minutes it sees motion again, it will cancel the shortcut to turn them off, and wait another 3 minutes after the time it stops seeing motion.

  4. Stumbled across this amazing post while trying to setup and automation to turn on lights if the day is cloudy. Seems like a simple task, but it hurts my simple brain. Any advice would be so appreciated. Here’s my goal:

    One hour after sunrise and when someone is home, check weather conditions at my location, repeat check every hour, if cloudy turn on lights, if sunny turn off lights, stop this process at sunset.

    Respect to your intellect and experience. Thanks in advance!

    1. Hi Tim, thank you for your great question.

      The short answer is that as far as I know, it’s not possible to do this in only HomeKit in a “simple” way. The longer answer is, it’s complicated 😁

      First, it’s absolutely possible to use a shortcut to check for a certain weather condition and to use that as a condition to turn a light on or off. It’s also trivial to run this one hour after sunrise when someone is home.

      The real problem in HomeKit is to trigger this every hour (starting an hour after sunrise) until sunset, because HomeKit doesn’t have a way of repeating an automation. One solution would be to create many automations running an hour apart, where you check both if it’s an hour after sunrise but before sunset, and what the weather condition is. The problem with that is, it’s a pain to write 12-ish automations that do the exact same thing, and if you want to change something, you have to do the same change in every automation.

      A slightly simpler solution would be to use a motion sensor to trigger the shortcut that checks the time and weather in combination with the initial automation that is triggered one hour after sunrise. If you would put the motion sensor in an area where there is often movement, this could work.

      If you want to go for writing many automations or using a motion sensor, let me know, and I’ll try to help you with the specific shortcuts.

Leave a Comment

Your email address will not be published. Required fields are marked *