In this post, we’ll have a look at how we can use a motion sensor to turn your lights off with HomeKit, but not before giving you a warning before doing so. I suggest that you start by looking at an earlier post about how to use a motion sensor to turn on and off your lights with HomeKit. Here, we’ll make the automation a bit more advanced by letting it give you a warning before turning the lights off.
As I used the earlier automation in the kitchen, it happened a few times that I was standing still enough while chopping vegetables or doing the dishes, that the motion sensor didn’t sense any motion for 90 seconds and the lights were turned off. I have now changed the shortcut so that it will dim the lights to 80% after 90 seconds, giving me another 30 seconds to move a bit, and the lights will go back to 100%. If there is no movement during the 30 seconds, the lights will turn off completely. You are of course able to customize the shortcut to different lengths of time and different brightness values for the lamps.
As before, I’ll go through the different steps of the shortcut.
(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.)
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. Select Create Home Automation. Then select A Sensor Detects Something, and pick the sensor in the room you want to control the lights in. Pick Stops Detecting Motion. You may change the time for what time of day this automation will run. On the next page we scroll to the bottom and select Convert To Shortcut under Advanced.
The Shortcut starts with an If statement, where we check if the light we want to turn off is on at the moment. The lamp we want to check is the Input, and Is On is the condition. Even though you can turn on and off a group of lights, you are only able to check a single light like this. In most cases this is fine. If not, you’ll need to nest several If statements. Because of this, there is a discrepancy in the names of this example (TRADFRI Driver is the work light on one side of the kitchen, while Work Light is a group containing the work lights on both sides of the kitchen).
Otherwise can be removed, and we’ll add a Repeat action next. Choose 30 times. We’ll then add a Wait action, which we’ll set to 3 seconds. Another If statement is added where we check if the motion sensor has detected motion. If so, we’ll Exit shortcut with Result. You do not need to choose any result. Again, remove the Otherwise. (We choose to use 3 seconds for the Wait action, because when a motion sensor detects motion, the motion detected “mode” will be active for a few seconds. That’s why we want to check every three seconds, so that the motion is still shown as detected when we check. If we reversed the numbers, repeating 3 times and waiting for 30 seconds, there is a huge chance that motion that was detected during those 30 seconds, would no longer show up when the motion sensor is queried.) If you want to prolong this part of the shortcut, you can run the repeat action for more times. However, if you run it for many minutes, there is a risk that the shortcut will be terminated for running too long. End If and End Repeat follow.
At this point we add the Control Home action to set the lights to 80%. By doing this, the lights dim from 100% to 80% as a warning that no motion has been detected, and the lights will be turned off if no motion is detected during the next 30 seconds.
Next we’ll add another Repeat action. Here we’ll choose 10 times. We’ll then add a Wait action which we’ll set to 3 seconds. (In case you want a longer grace period than 30 seconds, you may add times to the Repeat action. If you set it to 30 times, this part of the shortcut will run for 90 seconds.) Another If statement is added where we check in case the motion sensor has detected motion. If motion has been detected, the lights will be turned back to 100% through a Control Home action. After that follows the Exit shortcut with Result action. Again, remove the Otherwise. Next the End If and End Repeat follow.
At this point, we add another Control Home action where we choose to turn off all the lights in the room (or at least the lights that you want to turn off). At the very end, the End If (connected to the very first If statement) follows.
In case you have got more than one motion sensor to cover the whole room, you’ll have to add another If statement for each motion sensor where you check if motion is detected. You’ll add the same code within each If statement, e.g. Exit shortcut with Result, or set lights to 100% and Exit shortcut with Result. In this case you’ll have to create a “If No Motion Detected” automation shortcut for each sensor… the shortcuts themselves should be identical.