HomeKit Automation Tips

Getting more out of your smart home devices

How to use time of day as a parameter when writing HomeKit shortcuts (part 2)

In part two we’ll look at a more advanced example of using time of day as a parameter when writing a HomeKit shortcut. You can find part 1 here. If you need a more in-depth explanation on how to get started, please check the first Do more with the Hue Dimmer Switch (or any switch) in HomeKit post.

Advertisements

Example 2 (more advanced)

Once more we’ll use a Hue Dimmer Switch button as the trigger for the shortcut.

We’re building on the same idea as in example 1, but we’re using three different time periods with different actions. We’re also adding the ability to toggle between different dimmed levels and off.

Here’s what we want the shortcut to do: If the time is between 2:00 a.m. and 5:59 a.m. at night, the button will toggle between setting the lamp to 2% brightness and a warm color temperature, and off (if somebody needs to find the way to the bathroom at night, but doesn’t want to have lights very bright). If it’s between 6:00 a.m. and 3:59 p.m., the button will toggle between 30% brightness and a cool color temperature, 80% brightness and a cool cooler temperature, and off. If it’s between 4:00 p.m. and 1:59 a.m. the button will toggle between 30% brightness and a warm color temperature, 80% brightness and a warm color temperature, and off. The name of the lamp in this example is Steam lamp.

As in example 1, we need to note that we have to use the 24-hour time format.

A limitation in the shortcut scripting means we can’t use Else If, which is available in most scripting and programming languages. Instead we’ve only got Otherwise. This means that we need to use more nested If / Otherwise statements.

We’ll start with the Format Date action, and choose Current Date. We then need to go to Show More and set the Date Format to None and Time Format to Short. This will give us the current time (hours and minutes) in the 24-hour format. We then use Get numbers from input, and the Formatted Date will be preselected. This will get us the number of the current hour. So far everything is exactly as in the example 1.

At this point we’ll save the current hour into a variable, so that it is easier to later retrieve it. We’ll use the action Set Variable, and name the variable TimeNow. The variable should contain Numbers from the previous action.

Next we’ll add an If statement, and when setting the Input we’ll choose the variable TimeNow we just created. As condition for the If statement we choose is between, and set the numbers 2 and 6 as parameters.

We then add another If statement to check if the lamp Is Off. If true we set the lamp to 2% brightness and a warmer color temperature. Otherwise the lamp we’ll be turned off. At this point we end the current If statement with End If. Now we continue below the outer If statement’s Otherwise with another If statement, where the Input is the variable TimeNow, the condition is between and the parameters are 7 and 15. Below that we add yet another If statement where we check if the lamp Is Off. If so the lamp is set to 30% brightness and a cool color temperature. We then add another If statement after the Otherwise, where we check if the lamp’s brightness is less than 32%. If so the lamp is set to 80% brightness and a cool color temperature. Otherwise the lamp will be turned off. This is followed by two End If and one Otherwise. At this point we know that the time is between 16:00 afternoon and 1:59 in the morning. We add an If statement to check if the lamp Is Off, if so set the lamp to 30% and a warm color temperature. Otherwise check if the lamp’s Brightness is less than 32%. Set the lamp to 80% brightness and a warm color temperature. Otherwise, turn the lamp off. This is then followed by four End If.

Because it can be hard to follow the text above, and the screenshot of the shortcut is quite tall, I’ll also include code-section with indented code that might be easer to follow.

 

Format Current Date (Date Format: None, Time Format: Short)
Get numbers from Formatted Date
Set Var TimeNow to Numbers

If TimeNow is between 2 and 6
    If Lamp is Off
        Set Lamp to 2% and warm color
    Otherwise
        Set Lamp to Off
    End If
Otherwise
    If TimeNow is between 7 and 15
        If Lamp is Off
            Set Lamp to 30% and cool color
        Otherwise
            If Lamp Brightness is less than 32%
                Set Lamp to 80% and cool color
            Otherwise
                Set Lamp to Off
            End If
        End If
    OtherWise
        If Lamp is Off
            Set Lamp to 30% and warm color
        Otherwise
            If Lamp Brightness is less than 32%
                Set Lamp to 80% and warm color
            Otherwise
                Set Lamp to Off
            End If
        End If        
    End If
End If

Advertisements

Leave a Comment

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