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.

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

      1. I can’t seem to create this automation. when i get to the scripting section, i put if current date and then choose before. after that it will only seem to let me pick a date. i also notice that my scripting section does not have that line that connects it to the weather above it??? any ideas?

        1. Hi Michael,
          And thank you for the question. Apple changed the behavior a little bit in one of the updates. You now need to press and hold “Date” to get a small menu where you can choose “Select Variable”. You then get to select a “magic variable”, and at this point you should select “Sunrise Time” below Weather Conditions.

          Hope this helps 🙂

          1. Awesome thanks so much, that worked. Any thoughts when I test it why it seems to hang at the first statement; get current weather at current location?

            1. Could it be that the Home app doesn’t have permission to get your location? You can check in the Settings app under Privacy & Security > Location Services, and see if the Home app is in the list and with permissions. Usually, it’s actually better to set your location in the automation instead of current location. This way, the automation doesn’t need to query your location every time it runs.

              1. Home app is set to “while using the app”. I did try changing to set my location and that works. Wonder why if I leave it for “current location it stalls? Thanks for all your help.

                1. Sounds like you have it set up the right way. Hard to say why it’s not working… perhaps it’s a bug. But good that it works when you manually set the location. That’s usually a good idea to do, making the automation run a little bit faster and also one less thing to fail. Glad you got it working 🙂

  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:

            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.

  5. Appreciate your detailed examples, they have been the source of many ideas. Messing around with this arrangement I’m finding that during the evening hours weather will report the following days sunset time which screws a breather than test up. Any thoughts?

    Example, I’m messing with this at 10 pm on December 1st. It will report the sunset time as December 2nd 5:00 pm, not a Dec 1 sunset time. Based on that result Current time is less than the sunset date, not greater than as we would expect If I flip to custom Hmm formatted variables it works, but that approach requires quit a bit more coding.

    Is this weather response new in iOS 15? If not what have I done wrong?

    1. Hi Bryan,
      And thank you for your comment and question.

      You are completely correct in that this is a weakness of this automation. I think the solution is to use Get Daily forecast at Current Location (instead of Get current weather at Current Location) in combination with Get Sunset Time from Weather Conditions. This should only return the values for the active day.

  6. Hi Stefan,

    Great automation! I have one problem however, not sure if this is due to some iOS updates since your post.
    In the ‚if‘ part, it won’t allow me to choose Sunrise time as the date condition. Any thoughts? Thanks!

    1. Hi Daniel,
      I hadn’t even noticed that change since the iOS 15 update. If you just tap Date in the If-statement, it will give you a calendar to pick a date from. However, if you press and hold on Date (long press), it will give you a menu where you can select Magic Variable.

      Another tip: since writing this post, I’ve come to the conclusion that it’s better to use Get Daily forecast at Current Location instead of Get current weather at Current Location, because getting current weather will give the time of the sunrise next day if you query it later in the day. Asking for the daily forecast will always give you the sunrise time for the current day.

  7. Stefan, you’re my last hope! 😅
    I’d like a shortcut so when my phone connects to my home wifi it turns my living room lights on, but only if it’s past sunset. I can’t seem to figure this out! Any tips?
    Thank you so much and keep up the amazing work.

    1. Hi Gabriel,
      And thank you for an interesting question. As you want to use your phone connecting to the home wifi as the trigger for the automation, it means that this will be a Personal Automation, instead of a Home Automation. The difference is that this will run on your phone instead of on the HomeKit Hub. If you wanted it to run on your hub instead, you could use the location-based automations which uses geofencing to recognize when you arrive at home.

      For this kind of automation, we’ll use the methods that I wrote about in the three part series “The power of the Format Date function and how to use it for shortcuts using time of day or sunrise/sunset”.

      The only question I’ve got is if you want the lamps to come on if it’s between sunset and midnight, sunset and sunrise, or perhaps between sunset and a specific time. Depending on which you choose, the shortcut automation will look a little bit different. If you let me know, I can absolutely help you with this one 😉

  8. Is the shortcut above (using weather app for time of day automation) broken with iOS 16? I had success with iOS15, but now it is not running the automation!

    1. You are correct! There seems to be a problem in iOS 16 with these. I’m able to run them in the Home App, but not when using something else to trigger them 🙁

    2. How are you triggering the automation? As I’m looking more into this, it seems like whatever bug is present in iOS 16 is a bug with the triggering of the automations, not the automations themselves.

  9. My trigger is the weather app as you have in the above sample. I did revert back to running individual time base automations successfully via the Home app (iOS 16); i.e. when I arrive home between 1200 and 1600hrs turn lights on…

  10. This is a bit unrelated, but I’m really hoping you can help.
    I’m trying to build an action that will turn a smart plug on only if the outside temp is between -1ºC & -9ºC (30.2ºf & 15.8ºf). Here is what I have….

    Get current weather at *my address*
    Get *Temperature* from *Weather Conditions *
    If *Temperature* is greater than or equal to: 15.8ºF
    Set *Smart Plug* to turn on
    End if
    If *Temperature* is less than or equal to: 30.2ºF
    Set *Smart Plug* to turn off
    End if

    I’m just not sure if this is the proper way to do this. Will the one IF cancel out the other?

    1. Hi Kiriako,
      It will work, but perhaps not exactly as you want. If it’s warmer than 15.8ºF but colder than 30.2ºF, the smart plug will briefly turn on before it turns back off. What you probably want to use is the condition “is between” in the if-statement. That way you can do everything within one if-statement.

      How are you triggering the shortcut?

  11. Hi Stefan,
    Love you blogs. They are so helpful.
    I need help with automation for shades. I want to close the shades in the morning after sunrise but if it is only sunny outside and open the shades after the sun is not in the room.
    I don’t know if this is possible but I think you are the best person to help with this.

    Thank you in advance

    1. Hi Priten,
      Thank you for your kind words 🙂
      The first part of your automation is actually quite easy to do. You start by creating a new automation where you choose “A Time of Day Occurs” under Events. On the next screen, you select Sunrise.
      New Automation - Sunrise

      On the next screen you scroll all the way down to “Convert to Shortcut”.

      You’ll start the shortcut by getting the current weather for your current location. You can enter your home address if you want to, this will speed up the shortcut a little bit as it doesn’t need to query your location. You then get the Condition from Weather Conditions, which is a text string like “Sunny”, “Mostly Sunny”, “Cloudy”, etc. At this point, I would turn the text string to lowercase, just to make the matching easier (especially in case there are some changes made to how the weather conditions are written in a later update). You then create an if-statement, where you check if the Updated Text contains sunny. Remember to write sunny with all lowercase letters here. Inside this if-statement, you’ll set the controller to close the shades.
      Close Shades Shortcut

      Now, for the second part, you will need something to trigger the opening of the shades. Perhaps a lux sensor on the other side of the shades could work, but this would be a lot trickier and will need a lot of testing and trying.

      1. Thank you Stefan. I will try this and I was looking to purchase the Eve Motion and light sensor to try with this. Hopefully it will do the job. I will let you know if it works.

  12. Stefan, I have another scenario that I need your help with.
    In my garage, I have camera that has motion detection and I have a separate motion sensor in a different part of the garage.

    I want the lights in the garage to turn on when either motion sensors detects motion. I have set it up for 1 sensor but I can’t figure out how to do both.

    Your help would be much appreciated.

    1. Hi Priten,
      To use two different motion sensors to turn on the same lights, you need to use one automation per motion sensor. One is triggered by motion seen by one sensor and the other is triggered by motion seen by the other sensor.

      If you want to set up an automation to turn the lights off when neither sensor has seen motion for 2-5 minutes, you need to use two separate automations for that, but these two need to be somewhat integrated into each other.

Leave a Comment

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