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

Advertisements

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. (Update: Since iOS 15, Exit shortcut is now called Stopp running shortcutYou 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. (Update: Since iOS 15, Exit shortcut is now called Stopp running shortcut 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.

Advertisements

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.

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

      1. Hi Stefan, great post 🙂 it works in my downstairs hallway but upstairs has two sensors that don’t seem to play ball. Did you ever write that other post for multiple sensors?

        1. Hi Will,
          Sorry, I think I never actually published one for that. Perhaps I should do one. However, I can help you get it working. In the very last paragraph, I’m going through the steps you need to take to make it work.

          First you need to create an automation for each motion sensor to turn on the lights. That might use a shortcut or not, depending on how advanced you want it to be.

          Then you must create a “When No Motion Detected” automation for each motion sensor. These will be a bit more advanced because you need to check for “race conditions”, which means that if one is already running and has changed the brightness to 80% (the warning sign), you want the other shortcut running to stop, so that they don’t both try to change the lights. That’s why I’m checking if the brightness is below 90% in the first part, because if that’s the case, another shortcut is already running and has gone through the first 90 seconds.

          (We start by checking if the light is on, since we only need the shortcut to run if the lights are on.)
          If Ceiling_light Is On

          (We start the first loop by waiting 3 seconds.)
          >> Repeat 30 times
          >>>> Wait 3 seconds

          (We then check if another automation has already turned down the lights, and if so, we stop this shortcut.)
          >>>> If Ceiling_light Brightness Below 90%
          >>>>>> Stop this shortcut
          >>>> End If

          (We then check if another automation has already turned off the lights, and if so, we stop this shortcut.)
          >>>> If Ceiling_light Is Off
          >>>>>> Stop this shortcut
          >>>> End If

          (We check if the first Motion sensor has detected motion, and if so, we stop this shortcut.)
          >>>> If Motion_sensor_1 Motion Is Detected
          >>>>>> Stop this shortcut
          >>>> End If

          (We check if the second Motion sensor has detected motion, and if so, we stop this shortcut.)
          >>>> If Motion_sensor_2 Motion Is Detected
          >>>>>> Stop this shortcut
          >>>> End If

          >> End Repeat
          (We have exited the first loop.)

          (We recheck that the lights are still on, and if so, we dim the lights to 80%. If the lights are off, we stop the shortcut.)
          >> If Ceiling_light is On
          >>>> Set Ceiling_light to 80%
          >> Otherwise
          >>>> Stop this shortcut
          >> End If

          (We start the second loop)
          >> Repeat 10 times
          >>>> Wait 3 seconds

          (We check if another automation / or person has turned the lights back on to above 90%, and if so we stop this shortcut.)
          >>>> If Ceiling_light Brightness Above 90%
          >>>>>> Stop this shortcut
          >>>> End If

          (We then check if another automation has already turned off the lights, and if so, we stop this shortcut.)
          >>>> If Ceiling_light Is Off
          >>>>>> Stop this shortcut
          >>>> End If

          (We check if the first Motion sensor has detected motion, and if so, we turn the lights back to 100% and stop this shortcut.)
          >>>> If Motion_sensor_1 Motion Is Detected
          >>>>>> Set Ceiling_light to 100%
          >>>>>> Stop this shortcut
          >>>> End If

          (We check if the second Motion sensor has detected motion, and if so, we turn the lights back to 100% and stop this shortcut.)
          >>>> If Motion_sensor_2 Motion Is Detected
          >>>>>> Set Ceiling_light to 100%
          >>>>>> Stop this shortcut
          >>>> End If
          >> End Repeat

          >> Set all the lights in the room to off

          End If

          *********

          I just did this one in my head now, but I think the logic checks out. I have done these before and checked that they actually work 😊 If there’s anything you wonder about, please let me know.

            1. Sounds great 🙂 Let me know if you have any problems. By the way, for these kind of automations, it would be really great if you could copy and paste the whole shortcut. I wish this is something Apple will add at some point. It’s a bit annoying redoing the exact same shortcut several times (especially when they are a bit longer).

              1. Just following up I’ve noticed my Abode sensors take 60 seconds to no longer show motion detected. So I presume setting the wait to 61 seconds and repeat twice will give me two minutes as opposed to your 30 and 3 being 90seocnds?
                Then the second repeat set to wait 60 repeat once

                1. Hi Will,
                  And thanks for the follow up. If the Abode sensors take stay in the “motion detected” state for 60 seconds, you want to check more often than 60 seconds to be on the sure side that your shortcut detects it. If you’re using 61 or 60 seconds, you might be unlucky and just miss it. You need to account for that there is a small overhead of time in running the actual steps in the loop.

                  You could probably use a wait time of 30 seconds, but I would rather keep the 3 seconds, as that will stop the shortcut from running longer than necessary. If motion is detected within the first seconds of no motion detected, and you only wait for 3 seconds, the shortcut can be stopped after 3 seconds, and doesn’t need to keep running for a whole minute.

                  But, you can of course try different combinations and try what works best for you 😊

  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!

  6. HI Stefan Great tutorial there. Really appreciate you taking time to write this up. I have a question though. What if in the last “If” I want to set it back to the original brightness.
    i.e. From the top I have to record the brightness as a variable I suppose, it then dim to 1% when motion is not detected, then when a motion is detected it will then set it back to the original brightness.

    Thanks in advance!

    1. Hi Alex!
      You’ve got a really good question. Being able to set the brightness of a light from a variable is sadly something that is not possible at the moment in HomeKit. This is something I’ve wanted to do for several of my shortcuts, but as of right now it’s not implemented.

      However, there are a few ways around it.

      If you usually set the lights’ brightness values to specific values (I use Hue Dimmer Switches with shortcuts to toggle between different light levels), you know the different light levels your lights probably will be at. In that case you can use a few nested if-statements to check if the variable is one of them, and then set the light to the correct light level.

      If you usually use your phone to set the brightness levels, you can use nested if statements checking for ranges. For instance, if variable is between 1-10, set lights to 5, if variable is between 11-20, set lights to 15… and so on. It’s a bit more work, and it’s not perfect, but you’ll have something that works.

      If you’ll do either of these solutions, there’s something you need to think about. Brightness level values and on/off are two different values that are not connected. Even if your light is off, you can query its brightness and it will be whatever the brightness level was before you turned it off. If you turn a light on, it will be at the same brightness as it was when you turned it off. So, at the beginning you need to check both the brightness value of the light, as well as if the light was on or off, and save these results into two different variables.

      I hope this helped.

  7. Thanks Stefan for all the helpful information that you post here. You understand HomeKit better than most resources I’ve found on the web. I have been using this shortcut you designed for months in several rooms of my house. It works really well. Unfortunately, a few days ago, all of the lights using this shortcut stopped turning the lights off. I’m wondering if it’s a change in the Hue sensors or a change in HomeKit. I’m guessing that it’s HomeKit. Any suggestions you have to fix this issue? Thanks for your time!

    1. I’ve seen a few threads on Reddit about it. There has also been a few people that have contacted me with these same issues, and all of them have been running the beta, so I’m guessing there are some bugs in the beta. When they took away the beta, the automation shortcut started working again.

      1. Just a quick one. Does there need to be 2 automations set, that is one to turn a light on when the sensor detects motion and then a separate automation with your above shortcut? If so, for the first automation, what would I set the “turn off” function to?

        1. You are correct. You need one automation to turn on the lights (triggered by motion detected by the sensor), and the automation above to turn off the lights (triggered by the sensor stops seeing motion). The first automation turning on the lights shouldn’t have any “turn off” function. It only handles the turning on lights part.

  8. I’m trying to understand the logic here, why is there a check that turns the light back to 100% – isn’t there already a different automation to set the light to 100% when motion is detected ?

    1. Hi Kay,
      In 99% of the cases, you are absolutely correct that the turning the light back to 100% in this shortcut is redundant. However, there is a possibility for a race condition (where several things happen at the same time, with different outcomes), where the other shortcut turns the light to 100% just as this shortcut sets the lights to 80%. In that case the lights would stay at 80% until we go through no motion and motion detected, which could take many minutes or longer if there was continuous motion.

      So, the benefit for doing this is this possible race condition. There is also no disadvantage to doing it like this.

  9. Thank you!!! I also use your turn on automation and after reading this one i was able to add more! now if a certain device is on it wont use the motion sensor to turn on the lights because that device is a fan which means i am sleeping and i don’t want the lights on! this is perfect thank you!

  10. Hi Stefan. Great automation! I have been using it for months bud suddenly it is not triggered anymore when no motion is detected from my hue sensor. Any idea? Something changed in io 16?

    Thx

    Alex

    1. Hi Alex,
      It’s very rare that I’ve got a problem with this automation. Have you tried a full system reboot? When I do that, I pull power from the router, the Hue and other bridges, and all the HomeKit Hubs (AppleTVs and HomePods). I then power them back on in this order (with 30 – 60 seconds between them):
      1) the router
      2) the Hue bridge
      3) the HomeKit Hubs

      If there’s any instability in the system, this usually fixes it.

  11. This is a little complex for me, any chance if you could link me to your shortcut so I can see how it’s presented in the app please?
    Thank you

    1. Hi Alex,
      Automation shortcuts are not shareable the same way as personal shortcuts are. This is a shortcoming of automation shortcuts that I hope Apple will address at some point.

      Perhaps I can explain in more detail if you let me know if there is some specific part of the automation that seems complex and hard to understand.

  12. Hello, Stefan
    I googled and found this post. First of all, thank you for sharing. I think this is what I have been looking for a long time. Then I followed your guide to create two automation. One is when motion sensor detect, then turn on the lights and set turn off is never. The other one I copied from you. Basically the only different is that I set different light level, for example, you set the light level is 80% if motion can’t detect people. I set it to 50% and so on. Then I tried to test the automation. I found that after 90 seconds, when sensor detect no people, lights will become dark for a while. Also, during this time, if the sensor detects people, the lights will change to bright. There is no problem in this phase. However, after becoming dark, if sensor still can’t detect people(after 90 seconds again). The lights won’t turn off. I don’t know what’s the problem? Would you give me some advice? thanks
    PS: My homekit hub is Apple TV HD. The TVOS version is 16.5.

    1. Hi Alexis,
      I’m glad you found my site. It sounds like you’ve got it almost working. Is it possible you’ve got the last “Exit shortcut with Result” (now called Stop This Shortcut) in a slightly wrong position? As the shortcut will change the lights back to bright after being dimmed if motion is detected, we know it runs at least that far.

      1. Hi Stefan
        I enabled and tested the automation again today. It works fine. I don’t know why?
        I just changed my ipad language back to my native language. Since my native language is not English, to easily follow your homekit code. Due to Apple localizing the homekit command, sometimes I got confused in the meaning of local homekit term. That’s what I suspected. All in all, the end result is good.
        I also found out that your automation can work with Aqara FP2. Since FP2’s behavior is constantly triggering, as soon as you leave the “monitor area”, the light immediately turns off. It’s annoying. Sometimes you only leave for a short time, for example, I go to the bathroom while playing a video game.
        Finally, I have another question about this kind of shortcut automation. I found if I want to implement your way to different motion sensor, I can’t use copy and edit way to do it. Therefore, I have to write repeatedly for each sensor. any way to fix this? or am I missing some feature? please advice , thanks

        1. Glad to hear that it started working. Sometimes these shortcuts behave a little curiously.

          Yes, these automations with shortcuts should work with most motion sensors. The only problem I’ve heard about is if you use a camera acting as a motion sensor. These are sometimes so sensitive, that when the lights are turned off, the camera believes the light disappearing is motion, and they end up in a loop.

          And yes, I’m afraid you’ll have to recreate the shortcut for the other sensors. There is no way to copy a shortcut at the moment (this is very annoying and I really hope Apple will give us that ability soon).

          1. Before visiting your website, I’d originally planned to post some question on Reddit’s r/homekit. However, after clicking post, this group system always deleted my post immediately. I don’t know why, as I’ve posted on r/hue and r/Aqara before without any problems. Do you know why? or does this group have some rule about not allowing newbies to post ?
            Finally, Thanks for your great work and support. Without this guide, I can’t succeed

            1. I’m glad you found this site 🙂 And you are welcome to ask if you have any other question.

              Reddit’s r/homekit has a minimum 50 post karma limitation to reduce spam.

  13. Hello.

    I have a slight issue with this shortcut using iOS 17.

    I’ve set up the automation but for some reason the lights will not turn off. They will dim like they’re supposed to but they won’t turn off. I’m using the sonoff motion which shows “motion detected” for 60 seconds. I’ve tried and failed for several weeks now. Could you please assist in which of the repeat I has to change to the light actually will turn off?

    1. Hi Kai,
      I don’t have a Sonoff motion sensor, so I can’t test this for myself, but I would start by changing the second repeat loop to run 30 times just like the first repeat loop and see if that helps. It will of course mean that it will take 90 seconds after the lights have dimmed before they turn off, but if it works it might be a possible solution. Doing some reading about the Sonoff motion sensor, I’ve understood that the 60 seconds to re-trigger has been a problem with some automation.

      Were you using this automation with iOS 16, and has the behavior changed since then?

      It would be great to hear if this solved your issue so that I know for future questions, and if not we’ll have to figure out some other solution 🙂

      1. Hey Stefan,

        Im using the automation with the latest ios.

        I gave up trying to figure it out. Sometimes they turn off after dimming and sometimes they hang and will stay dimmed for hours even when no one is home.

        The result i came up with was to remove the diming so they go from 100% to 0% when no motion is detected… Its a shame since i really loved the idea that they give you a warning before turning off…

        Tried with diffrent wait and repeat and its the same result with 30 repeats and 45…

        1. Hi Kai,
          I’m sorry it’s not working for you. Have you tried a full system reboot since you last updated everything to the latest version (the iPhone, iPad, appleTV, homePod)? With that I mean power cycling your router, all the bridges, and hubs.

          I’m running the latest iOS version, and it’s working 99% of the time for me. I have a “belt and suspender” solution, by using a dummy switch (Homebridge) which is turned on every time motion is detected. The dummy switch has a 5-minute timer to turn back off, so every time motion is detected, this is delayed by another 5 minutes. I’ve set up another automation for when this dummy switch is turned off, which turns off the lights. The few times the automation described here doesn’t work, this will turn off the lights.

  14. I’m using homebridge, would you mind sharing the screenshot of the switch config as well as the automation?

    You could send me via mail that would be great ☺️

Leave a Comment

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