HomeKit Automation Tips

Getting more out of your smart home devices

How to use a motion sensor to turn your lights off with HomeKit (but giving you a warning first) – More advanced version

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.

12 thoughts on “How to use a motion sensor to turn your lights off with HomeKit (but giving you a warning first) – More advanced version”

  1. Hi Stefan, just wanted to thank you for this brilliant solution. My Hue bridge was beginning to get overloaded with bulbs/routines and I have been able to free it up a lot moving a few automations over to my Apple TV Homekit hub. I’ve got your site bookmarked and will check your blog every now and then. All the best, Simon

    1. Thank you Simon. I’m glad you found it useful. I promise to add some more more automations in the near future 🙂

    1. Hi KH,

      That’s actually a very good question.

      But if we look closely through the shortcut, we’ll notice that the shortcut will never run for longer than 30 x 3 seconds + 10 x 3 seconds, which equals 2 minutes. Whenever it detects motion while running the shortcut, the shortcut will terminate (Exit shortcut with Result)… if it’s in the second stage it will turn the lights to full brightness before terminating, and it will start over again the next time it stops detecting motion.

      Most times, we’ll only be a few minutes in the kitchen, but when preparing food or doing the dishes I’m sure I’ve stayed more than two hours at a time.

      The only way the shortcut will timeout is in case you change the number of repeats in the shortcut, so that the shortcut will be running much longer than the 2 minutes maximum it’s at now.

      1. So the shortcut can’t run for more than 2 minutes your saying?
        Tried your shortcut you posted above, and find the lights going off while I am sitting at my computer. Kind of annoying i have to wave my hands all over to trigger the lights to turn on again.

        1. No, I’m saying the shortcut I’m using runs 30 x 3 seconds + 10 x 3 seconds… but you can change the repeat number to higher than I have. I’m not sure what exactly the limit for a shortcut is, but it should be somewhere above 10 minutes. But, if it’s a room where you sit still for a long time, using the motion sensor to turn the lights off might not be the best idea. I used to use a smart plug to work as a variable, so that if the smart plug was on, the motion sensor would neither turn on or off the lights… that way you can force the lights to be on or off. I’ve since started using Homebridge on a Raspberry Pi for dummy switches for this kind of use.

  2. Hi Stefan, just as Simon I really just wanted to say thank you for this super-helpful automation. Works great with my Aqara motion sensors. Keep it up and be safe. Best, Marc

    1. Thank you Marc! That’s great to hear. I really try to explain the shortcuts in detail, so that if someone takes a little bit of time to think about the logic within them, they will be able to figure out how to make these shortcuts by themselves 🙂 Be safe!

  3. home security and automation NZ

    Thank you very much for sharing these ideas. I really appreciate your efforts for creating this exceptionally well content. I was looking for such content about best home remodeling ideas, you have really helped me with the same, great post!!

  4. Stefan, this is incredibly useful thank you. I’ve been struggling with the constraints of automation options, particularly as my Hue Motion sensors don’t allow a motion period to be set in the same was Eve Motion allows. Now all sensors are set to 5s and the Shortcut does the work and is configurable. I could end up with a lot of automations with shortcuts as some lights need to be triggered by 4 sensors, do you know if there’s a practical limit to the qty of automations with shortcuts?

    1. Hi Andy, thank you for your comment. I’m actually writing a post about how to use multiple motion sensors to control the same lights, as there are a few things you need to think about. I’ll try to get it ready as soon as possible.

      Regarding the limitation on how many HomeKit automations you can have, I have to preface and say that I have not gotten close to the limit myself, but I’ve seen several people talking about somewhere above 200. Of course, it becomes hard to administrate that many, since you can’t put them into folders.

Leave a Comment

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