This is part two of the power of the Format Date function, and it will build on what we looked at in part 1. We’ll look at how we can utilize the Format Date function for use with the sunrise and sunset. In part three we’ll look at using this technique with offsets (for example if current time is between sunrise minus 30 minutes and sunset plus one hour).
(In case you’re using an adblocker, please consider whitelisting this site. Any revenue from Google ads goes to the costs of running this site.)
1. Getting the time of sunrise and sunset
To find out at what time sunrise and sunset occurs in your current location, we have to use the Current Weather function. The Current Weather function contains a plethora of data, such as temperature, pressure, UV index, wind speed and direction, and a lot more. Sunrise and sunset time are also part of this list.
As you use Get Details of Weather Conditions after Get current weather at Current Location, you’re able to choose between all of these data points. If we choose sunrise or sunset, we’re actually being returned a date and time value. And it’s important to know that the data isn’t updated exactly at midnight, but perhaps somewhere in the first hour of the new day.
Why is this important? If you keep the sunrise as a time and date value, and you use it in an if-statement where you compare it to Current Date, you can do conditions like is after, is before, is today etc. But if your if-statement says “If Current Date is after Sunrise Date”, and the check is being done just after midnight, the result would be false, because it would compare the sunrise of the day before instead of the new day.
This is why I suggest you use the Format Date function as we did in part 1, and convert the date and time value, extract the time, and convert it into a simple number. If we do this, we can use it exactly as we used the TimeNow variable that we created from Current Date in part 1.
In this example a light is being triggered by a motion sensor, and the following conditions apply:
- if the time is between 5:00 AM and sunrise, the lights turn on to 50%
- if the time is between sunrise and sunset, the lights turn on to 100%
- if the time is between sunset and midnight, the lights turn on to 30%
- if none of the above are true, the lights will turn on to 10%
I also assume that we’ve already created the variable TimeNow, which is a number containing the current time in military style time (as we did in part 1).
Start by adding Get current Weather at Current Location (a good tip is to actually put in your address here, as it seems to speed up the shortcut a little bit). To get the actual sunrise date, we need to use Get Details of Weather Conditions, and here we choose Sunrise Time as the detail.
We then add the Format Date action. Sunrise Time should be preselected as the Input. Press Show More, Date Format and Custom. In Format String, you replace the default value with the letters Hmm. Remember that these are case sensitive.
At this point we’ll add the Get Numbers from Input action (it often works without this step, but every now and then it doesn’t recognize the numbers as a number value, so I often use this extra step).
Next, we’ll save the number into a variable with the Set Variable action. We’ll use the name SunriseTime.
We then do the same thing for sunset, but we can omit Get current Weather at Current Location, as we are able to tap into the first query. As we add Get Details of Weather Conditions, we choose Sunset time as the detail. We now have to link this to the earlier weather query. As you can see, the Weather Conditions (in Get Sunset Time from Weather Conditions) is semi-transparent. That means it is not connected to a weather query.To do that we click semi-transparent text, choose Select Magic Variable and scroll up to Get current weather at Current Location and select Weather Conditions.
After this, we add the Format Date action, and do the same formatting as we did for the Sunrise date. Press Show More, Date Format and Custom. Next, replace the default string with Hmm.
We add another Get Numbers from Input, and finish this part by creating a new variable with Set Variable, and naming it SunsetTime.
You should now have three variables TimeNow (that we created in part 1), SunriseTime and SunsetTime.
2. Using the numbers in if-statements
Next, we’ll start with the if-statements. One good way to do them would be to use nested if-statements with Otherwise. However, I feel like this could become a bit confusing in this example, so I’ll only do subsequent if-statements instead.
We start by adding the if-statement for the time between 5:00 AM and sunrise, where the lights are turned on to 50% brightness.
The second if-statement is for the time between sunrise and sunset, where the lights are turned on to 100% brightness.
The third if-statement is for the time between sunset and one minute before midnight, where the lights are turned on to 30%.
And the fourth and final if-statement is for the time between midnight and 4:59 AM.
3. Possible problem with this shortcut
Depending on your location and time of year, it might be that this shortcut doesn’t work for you. In southern Finland around midsummer, the sunrise is before 4:00 AM and the sunset is at almost 11:00 PM. However, in northern Finland, there is actually a time in summer when the sun does not set at all for about 60 days. This would of course break the whole shortcut, since the if-statements do not make any sense at that point.
To fix this, we would need to add additional if-statements to if the SunriseTime and SunsetTime have values that work in this shortcut. If you want to see how to do that, let me know in the comment section below.
In part three we’ll see how to use offsets for sunset and sunrise (for example if TimeNow is between sunrise minus 30 minutes and sunset plus one hour).
If you’ve got any questions about what was explained in this post, please leave a comment and I’ll try to explain it more thoroughly.