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.

22 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.

  5. Hi Stefan,
    Thanks for this great tutorial. However, I’m trying to figure out how to add a mode in which the motion sensors would not turn off the lights. The first thing that popped up to my mind was using specific brightness on my special nightlight, serving as an indicator light for this very purpose. So for example, if my nightlight is set to 20%, this shortcut of “turn off lights when motion sensor stops detecting motion” should not run. I tried so many varieties by changing and adding your above example, but none worked. I even used the Home+ 5 app to set some things through the “Under These Conditions” menu, but that just broke the automation altogether. Deleting the condition(s) would make it go back to normal. Any help would be greatly appreciated! Thanks again!

    1. Hi Winston,
      This is a great idea. The way to do it is to add a short if-statement to the very beginning of the shortcut, in which you check in case the light is at a certain light level, and if this is true you just exit the shortcut which terminates it. It would look like this:

      If TheLamp Brightness is 20%
      Exit shortcut with Result
      End If

      And then the rest of the shortcuts follows 🙂

      1. Hi Stefan,
        Thanks for the input. I can confirm that it’s not working for me. Even after I had set my Nightlight to the specific brightness I specified, my motion sensor still turned off like it’s supposed to, before the added short if-statements.

        Any idea why?

        1. This sounds a lot like a bug that I thought had been fixed. I’ve had the same problem sometimes when writing new shortcuts. My way around it was to convert the brightness of the lamp into a number, and then save it to a variable that I check in the if-statement.

          So, at the very beginning you’ll use the “Get the state of Home” action.

          Get TheLamp Brightness
          Get numbers from Brightness
          Set variable LampBrightness to Numbers

          If LampBrightness is 20
          Exit shortcut with LampBrightness (it doesn’t matter what you exit it with)
          End If

          And then the rest of the shortcut follows… Does this solve the problem?

          1. Hi Stefan,
            Really appreciate the quick replies! That did work to keep the lights on, but there’s another issue going on. The lights wouldn’t automatically turn off at times! Say I have that Nightlight to 20%. I go in, lights would stay on.
            Then if I manually turned off the Nightlight as well as all the other lights through HomeKit; and step out, lights wouldn’t turn off like they’re supposed to. Give it some time (like a few minutes), and sometimes it would work again. But more often than not, the automation would break again. Is this a known issue with HomeKit? If it helps, I have less than 10 automations, and none of them conflicts each other (I tried isolating it to only this shortcut running by itself).

            1. I’m glad if I can help 🙂
              When the lights don’t turn off automatically, this after someone has walked past the motion sensor?
              If the system seems unstable, I would try rebooting everything: your router, the Hue bridge and the HomeKit Hub. This usually helps.

              1. Hi Stefan,
                Correct. After someone has walked past it again, thereby triggering the motion sensor. I can’t be sure to call the system unstable — even though I’ve tried rebooting those more than once. Again, if I remove the additional Ifs (apart from the ones you laid out in the original example), everything immediately goes back to functioning normally.

                Any idea what’s going on?

                1. That’s really strange. The small addition shouldn’t make it unstable. Without seeing it, it’s really hard to guess on what’s going on.

    1. Great that you got it solved. Now I feel dumb 🙄 That should have been obvious, but it’s always a bit harder when you can’t see the whole thing at once 🙂 Thanks for sharing your fix!

Leave a Comment

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