How to use a motion sensor to turn on and off your lights with HomeKit

Using motion sensors to turn your lights on and off is both comfortable and can help you save energy. This works best for areas in the home where you pass through, or rooms where you are not perfectly still for a longer period of time. We’ve got a small kitchen that is mostly used for preparing food. There is no table or chairs where we would sit still for a long time. So the kitchen is an ideal candidate for using a motion sensor to control the lights for us. Entrances and hallways are also perfect areas for motion sensors.

In this example I’m going to be using the Hue Motion Sensor, which comes with the added benefit of also including a temperature sensor and a light sensor. In the example below, I’m actually using the light sensor to check if there is sufficient natural light in the room, before turning on the lights. In this example we’re only automating the under-cabinet lights, but you could easily add other kitchen lights to the same shortcut.

UPDATE: As of the iOS 15 / tvOS 15 update, this shortcut will not work. There is a bug in iOS 15 / tvOS 15 which breaks the conditional if statements where we check for the brightness level of the room (lux). But there is a workaround the bug. You can read more about the workaround here.

UPDATE: It appears that the bug has been fixed with iOS 15.1 / tvOS 15.1 / HomePod Software Version 15.1.

In iOS 14, Apple fixed the default behavior for the turn off after x minutes function. It now behaves in the expected manner that every time the motion sensor senses motion, the delay to turn off is reset. This means that if you’re going to do a simple turn on and turn off automation, you no longer need to use a shortcut to do so. However, there are still many situations where you want to turn on lights using shortcuts triggered by motion sensors, and in these cases the solution below is still useful.


We need to create two automations: one to turn the lights on, and another to turn them off. The function to turn them off after a specific period of time does not work exactly as one might think. It will not turn off after a specific time from when it has stopped seeing motion, but rather after a specific time from when it first saw motion. This is why we need a second automation to turn them off.

The automation for turning the lights off is triggered by the absence of movement. It will then wait one and a half minute, checking every 3 seconds for motion, before finally turning off. If it detects motion, it will cancel the automation to turn off the lights.


Turning the lights on

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 we want to control the lights in. On the next screen select Detects Motion. The Time and People settings are optional. If you want different automations for times of day, you can select at what time this automation should work. For us, HomeKit knows if my wife or myself is at home, so we have the People setting set to When somebody is home. We then go to the next page. Now, if you’re using a motion sensor that doesn’t have a light sensor, at this point you can only choose the light and turn them on to whichever brightness level you want. If you’re using a Hue Motion Sensor, you should scroll down to Advanced and select Convert To Shortcut.

The shortcut for turning on the lights is very simple. We’ll start by adding an If statement. We’ll choose the light sensor as Input, Current Light Level as Name, and is less than as Condition. I’ve chosen 80 lux as the parameter for now, but I haven’t tested enough to know which would be the best value. We can remove Otherwise and add a Control Home action where we choose the light and the suitable brightness level and color. I’m using 100% brightness. The shortcut then ends with End If.

Turning the lights off

We’ll now start on the automation for turning off the lamps. We’ll start by creating a new Home Automation (just as we did for turning the light on). We then select A Sensor Detects Something, and choose the motion sensor that is in the same room as the lights. This time we pick Stops Detecting Motion, and again you may change the time for what time of day this automation is running. On the next page we scroll to the bottom and select Convert To Shortcut under Advanced.

The shortcut starts with an If statement, where the lamp we turned on in the first automation now is the Input, and Is On is the condition. You can however only check if a specific light is on, while you can turn on a group of lights. This is why there’s a discrepancy in the names in the this example.

Otherwise can be removed, and we will 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 in case 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 shortcut) You do not need to choose any result. Remove the Otherwise. End If and End Repeat follow. At this point we add the Control Home action to turn off the lights, and the last End If follows that. 


In case you have to use more than one motion sensor to cover the whole room, you’ll need to create one automation per sensor for turning the lights on. You’ll also need to create one automation per motion sensor to turn them off. You have to change the shortcut to something like this:

If Light Is ON
Repeat 30 times
Wait 3 seconds
If Hue motion sensor 1 Motion Is Detected
Exit shortcut with Result
End If
If Hue motion sensor 2 Motion Is Detected
Exit shortcut with Result
End If
End Repeat
Set Light (OFF)
End If

This means that the shortcut will check each motion sensor for motion, and exit the shortcut if any of them detects motion.

You might have to experiment with how long the shortcut for turning the lights off should run, however, if the shortcut’s runtime is too long, the shortcut risks getting terminated.

If you’re looking for a slightly more advanced version of the “turn off lights” automation, where the lights will give you a warning by dimming a little bit before turning off, have a look at this post.

174 thoughts on “How to use a motion sensor to turn on and off your lights with HomeKit”

  1. Hey!

    To begin with, thanks for being so responsive to previous posts made these people!

    I’ve got a bit of another issue, not involving your explicit shortcut/automation, but still the Hue motion sensor.
    I have grown tired of Smartthings app unresponsiveness on my Apple devices. So I thought I’d give Homekit a try. Now to the problem. My hue motion sensor is setup as the standard automation where “if motion is detected then turn on Lamp” and then an added “if no motion turn off”. The thing is that it’s very sporadic when it actually turns the light on or not. And I’ve found that it works 100% of the time when I unlock my iPad (which is my HomeKit hub), vs having its screen off.

    Is this a problem connected to having an iPad as the only hub?

    Additional info:
    – iPad Pro (2018) running latest iPad OS.
    – 1 Hue Hub, latest firmware
    – Lights I use here are IKEA Tradfri
    – Distance to sensor and the lights is approx 5-6m.
    – The sensor indicates motion in the Home app, but the action to turn on the lights isn’t always triggered.

    Thanks in advance,

    1. Hi Fredrick!

      I’m glad you found the site.

      I must admit that I haven’t tried using the iPad as the HomeKit Hub even though we have a few of them here at home. However, I have read quite a few threads online where people have had some issues with reliability when going that route. Going only by what I’ve read, it seems like the AppleTV and HomePod work more consistently.

      Personally, I like to hardwire anything I can (as far as networking equipment are concerned), and that’s why I’ve always recommended using an AppleTV connected by ethernet as the HomeKit Hub.

      However, reading about your setup, I’m wondering if you’ve connected the IKEA Trådfri lights directly to the Hue Bridge or do you use a IKEA Trådfri Gateway for the Trådfri lights?

      Have you setup the automation within the Hue app or the Home app?

      1. Thanks for a quick response!

        Yeah, I like things hardwired too for a more stable experience. But since im just testing to see wether it’s worth making the switch from ST or not.

        I’m using the Trådfri lights connected to the Trådfri hub, and then synced to HK. Originally I had them connected to the Hue bridge, but the lights didn’t show up in HK for some reason. Is it possible to have the Trådfri lights connected to Hue somehow?

        The automation is set in Homekit, since the Trådfri lights aren’t connected to hue.

        1. Of course, for testing purposes it’s always best to try with what you’ve got available. The default suggestion is to turn the router, bridges, gateways and HomeKit hub off, then turn them on again… first the router, then bridges and gateways, and finally the HomeKit hub. But I’m pretty sure you’ve already done that. So in your case, it seems likely that it is the use of an iPad as the HomeKit Hub that is causing the problem.

          The only other thing I can think of, is how the Motion Sensor is set up. When using it with HomeKit, it should not be configured in the Hue app, but only added to the Hue app. You can check this by opening the Hue app, go to settings and then Accessory setup. It should say “Not configured” under the Motion Sensor. This is so that there aren’t two systems trying to use it, which could create conflicts.

          As both the Hue and Trådfri system are using Zigbee, it is (as you noticed) possible to add Trådfri lights to the Hue hub. However, if you do so, you will only be able to control them within the Hue app. They will not show up in HomeKit. I’m not completely sure if this is limited by Hue or HomeKit.

  2. Yes, I’ve tried all the basic things like switching everything off and setting it up in the right order etc. And I more or less come to the conclusion that it must be the iPad as a hub that’s the problem. Since the automations and everything works 100% of the time if I unlock my iPad and have it ”active”. And when it’s in idle it works very sporadic instead.

    I have the sensor set as ”Not configured” and still the same results.

    It’s a bummer they don’t recognize the Trådfri lights as ”trusted” enough to let it show up in HK through the Hue hub. Since they apparently work through IKEA’s hub.

    I think the solution for me is to get an Apple TV or on of the speakers to work as a hub. And I think I might in the near future setup a HomeBridge solution aswell.
    I’ll be back when I stumble across issues in that matter! 😉

    Thanks for your support Stefan!

    1. I’m pretty sure getting a different HomeKit Hub will solve the problem.

      About Hue and Trådfri, it’s the same the other way around. You can connect Hue lights to the Trådfri Gateway, but they won’t either show up in HomeKit. So there’s clearly a limitation that does not allow devices from one party to be connected via a bridge/gateway of another party to be connected to HomeKit. I assume it has to do with requirements that all devices need to be tested and certified to work with a certain bridge/gateway.

      I’m definitely no HomeBridge expert, but I’ve been using it (I went with HOOBS) for some months, and had no problems so far. I especially like the ability to use dummy switches for more advanced automations.

      Will be interesting to hear how it all works out 😃

  3. Hi Stefan, thanks for the great article! I managed to use your first shortcut so my hallway light comes on (to 99% when it’s bright, and 29* when it’s dark.) upon detecting motion.

    However, I chose 99% and 29% as they are unique values, and I wanted the absense of motion to turn off the lights only when the lights were triggered by motion in the first place (i.e. if the lights come on because I set a particular scene, they will not turn off despite absence of motion. However, I was not able to get the 2nd shortcut working. Can you advise if I did something wrong with my 2nd shortcut below?

    Home Automation: A Sensor Stops Detecting Motion

    If Light Brightness is 99%
    Repeat 30 times
    Wait 3 seconds
    If Motion Detected
    Exit shortcut with result
    End If
    End Repeat
    Set Light to OFF
    If Light Brightness is 29%
    Repeat 30 times
    Wait 3 seconds
    If Motion Detected
    Exit shortcut with result
    End If
    End Repeat
    Set Light to OFF
    End If
    End If

    1. Hi Dave! I’m glad you liked it 🙂

      First, I want to say that your code and logic is perfect. There is nothing wrong with it. Second, I had something very close to this working last year. However, I did some testing now, and it’s not working anymore. So clearly there is a bug, but the bug is not yours. I even tried “If brightness between 100% and 0%…” and that didn’t work either.

      However, I managed to create a workaround. If you start by checking the brightness value of the light, formatting the number and then setting it to a variable and then using the variable for the if statement, it works. To check the brightness value, you’ll use “Get the state of Home” action.

      The full code should be like this:

      Get LightName Brightness
      Get numbers from Brightness
      Format Numbers to 0 decimal places
      Set variable BrightnessLevel to Formatted Number
      If BrightnessLevel is 99 (no % here)
      Repeat 30 times
      Wait 3 seconds
      If Motion Detected
      Exit shortcut with result
      End If
      End Repeat
      Set Light to OFF
      If BrightnessLevel is 29 (no % here)
      Repeat 30 times
      Wait 3 seconds
      If Motion Detected
      Exit shortcut with result
      End If
      End Repeat
      Set Light to OFF
      End If
      End If

      This worked for me. Let me know if it works for you as well.

      1. Thanks Stefan for the quick and helpful response. Your diagnosis was spot on! After your post I did some tests and “If brightness” is not working at all.

        Your recommended workaround is working perfectly for me. Thanks again for finding a workaround and for your super helpful articles on this page!

        1. Thank you Dave for your feedback and kind words. And thank you for coming back to tell how it worked out. I’m inspired by your idea and the bug you encountered to write a post about it 🙂 I hope that’s ok.

  4. Hello

    Firstly love this site very helpful.

    However I’m having issues with the automation for turning lights off.

    It works perfectly when I do 30 repeat and 3 seconds but if I want to do it for longer it doesn’t work. I’ve read through your posts and it mentions I should be able to get up to 10-12 mins. I have followed your instructions to the letter so I don’t understand what’s wrong 🙁


    1. Hi Luke,

      I’m glad you like the site.

      I must admit, I had never myself checked the time limit of a shortcut. When I wrote 10-12 minutes, it was what I had read. However, tonight I took the time to test what the limits are.

      I used the shortcut that dims the lights after 90 seconds, and then 30 seconds later turns them off (the more advanced motion sensor shortcut I’ve posted) as a base.

      When I used 200 * 3 seconds (10 minutes) for the first part, it did not finish successfully. However, when I used 180 * seconds (9 minutes) for the first part, it finished both the first part and the 10 3* seconds second part. However, when I timed how long it took for the shortcut to run, it actually took about 10 minutes… so there is a little bit of overhead for checking if motion has been detected for 190 times (180+10). In other words, it seems like the hard limit is 10 minutes (at least for me, when I use the Apple TV 4K as the HomeKit hub) for the whole shortcut to run.

      I hope this helps, and I’m sorry I mislead you with the 10-12 minutes. It would be great to know if you get it working with 180 * 3 seconds and if you have any other troubles.

      1. Thanks for your reply 🙂

        I have tried this and even shorter times but nothing over the 90 seconds works.

        I am using an iPad that is always connected as a hub though! Do you think that could be it?

        Thanks again

        1. And you’re always adding to the number of loops (repeats), and not the wait time? So for instance if you have it repeat 80 times and wait 3 seconds each time (that would be 4 minutes), it will not finish? Are you running iOS 14 on the iPad? Maybe using the iPad as the hub might be the problem then.

  5. Yes that’s correct.

    Anyway I’ve purchased a HomePod mini to check…. I’ve plugged it in and the automations work perfectly now.

    I’m using the latest iPad Pro with up to date software but Seems the iPads have limitations with automations. The most I could get out of it was 90 seconds.

    Thanks for your help

    1. Ok, good to hear that it works with the HomePod mini. If you ever do a test to see how long shortcuts you’re able to run with it, I would be very interested to hear 🙂

  6. In your “turning the lights” off automation, the last step “At this point we add the Control Home action to turn off the lights” is not an option in my shortcuts. The + sign pulls up scripting options, but “Home Control” is not there.

    1. Hi Sven,

      This is the Control Home action I’m talking about. If it doesn’t show up directly, you’ll find it by searching.
      Control Home action

  7. Hello Stefan, thanks a lot for your guide, I hope it will finaly make my sensor work correctly.
    Just a two question. First is about 30x 3 sec. What is the logic behind it? Does it mean that the sensor is looking for a movement for these three seconds? or is it just that after three seconds sensor triggers and if it see movement it will end? Let’s say that I have a sensor in my kitchen and I don’t want to turn off my lights so I wave with my hand to restart the sequence. Do I have to wave for 3 seconds, or be waveing exactly when 3 seconds ends, or can I wave anytime and it doesn’t matter. And second question. Sometimes happend that instead of turning the lights off it just dimmed to like 10%. If I moved it went back to 100% but then again 10%. I had to manualy turn it on and off to make it work again. Thank you and sorry for my english (not my first language)

    1. Hi Petr,
      I’m glad that you like the guide 🙂

      First about the logic for using 30 x 3 seconds. Every time the motion sensor detects motion, it will be set to the state “motion is detected”. This state remains about 5 seconds after the motion has stopped. After that it will trigger the “no motion detected” state, and then go back to the default state. As we check every 3 seconds if motion has been detected, we are certain to catch it. If we would use a wait value of 10 seconds, there would be a chance that we would miss it.

      About the rather interesting behavior where the lights go to 10% instead of turning off. Double check that the last control action before “End If” is set to turn the light off. Also check that you don’t have more than one automation turning the lights off.

      Also, you might like this version as well 😉

      And you should absolutely not apologize for your English! It’s great! English isn’t my first language either.

  8. Hi Stefan,

    Your automations are amazing and thank you for post them.

    But I have 2 problems:

    1) when I set the first automation for light on it works anyway also with others lux parameters (I put 30 lux)

    2) about light off automation it doesn’t work….I don’t understand why it is perfect but it works only when I play the automation in the command (when I do the shortcut I mean)

    I use fibaro HomeKit sensor and one hue Philips light for make the automation

    Can you help me please?
    Thank you

    1. Hi Fede,
      I don’t have any experience with the Fibaro HomeKit sensor, but I assume it works the same way as the Hue Motion Sensor I’m using. First thing I would do is double-check that the automation is supposed to run when the sensor stops detecting motion. You could set it to turn off the lights directly just to confirm that the action is triggered. Since it works when you run the shortcut manually, it seems like your shortcut is correct.

      1. Hi Stefan,

        Thank you for reply.

        Yes your command worked before, but now no. Fibaro is egual at hue motion.

        Yes it work if I set fibaro for normal light off when there aren’t movements….so I don’t understand why your command doesn’t work.

        Have you others idea for resolve that? Maybe I must re instal the sensor ?
        Help me please

        1. What are you using as your HomeKit Hub? HomePod, AppleTV or an iPad? Is it updated to the latest firmware?

          1. Hi Stefan,

            Sorry for delay.

            I have Apple TV iPad HomePod ho pod mini and for homebridge I have raspberry….so much hub so I don’t know what is the problem. All with latest firmware yes.

            I repeat this SC worked before but now it doesn’t work…I don’t know why


                1. I wonder if that could be part of the problem. I’ve been trying to find out what could cause this, and I noticed that people running the beta has had problem with shortcuts using if-statements.

                2. The problem is beta iPadOS and use ipad like an hub in app home.

                  I deleted it and now the automation works, yeah!!!!!!!!!!

                  I’m much happy now 😊

                  Thank you for you help Stefan, please share others automations or SC automations so I can take inspirations 😊

                3. Great! I’m really happy you got it working! And yes, I’ll post some more automations with shortcuts in the near future 🙂

  9. Hey, really love your shortcut cut since yesterday it stopped working after I updated to iOS 15 for iPhone, HomePod and Apple TV. Do you have any idea ?

    1. Hi Andreas,
      I’m updating everything myself right now. I’ve get back to you as soon that I’ve solved it. I got feedback earlier that this was happening with the beta as well, but I hoped Apple would have solved it for the real version.

    2. Andreas, I can confirm that it stopped working for me as well as soon as I updated the AppleTV to tvOS 15. However, it was only the shortcut that turns on the lights that stopped working, the shortcut turning off the lights had no problem.

      I tried deleting the automation and then redo it. It now works, but only if I omit the part where I check how bright the room is. As it is in this automation, you might as well take away the shortcut for turning on the lights, and only use a normal automation (since the checking for the room brightness seems broken right now), and just use simple automation to turn lights on.

      I’m using a slightly more advanced shortcut at home, where it checks the time of day and in case we’ve gone to bed. In that case, it only turns the lights on to 1% in the kitchen. This part worked without a problem.

      So in short, it seems like using the lux meter in the motion sensor as a conditional is broken in iOS 15 / tvOS 15. Hopefully Apple will get it fixed.

        1. I’ve found a workaround. You cannot use the lux directly in the if statement, but if you follow this, it will work:

          Use the Get the state of Home action, and choose the light sensor and current Light Level. Then use the Get numbers from Input action and use the previous Current Light Level as input. Then use Set Variable, use a name (LuxLevel) which you set to Numbers from the previous action.

          Now you can use the variable in the if statement, and this will work. This workaround also works for using lamps brightness levels in if statements. It’s a bit cumbersome, but at least you can get the same functionality.

            1. Of course! 😊
              Creating a variable is actually an unnecessary step, you could just use the Magic Variable thingy, but I think this makes it even easier to understand. Hope it helps 🙂
              Updated shortcut for turning on lights

      1. Hi dear Stefan,
        Pity but with iOS beta 15.2 the shortcut for light turning off the light doesn’t work.
        Have you some suggestions for to help me please?

        I have always problems 🤣 sorry

        1. Hi Fede,
          The iOS 15.2 beta isn’t out yet, do you mean the iOS 15.1 beta 2 for developers?

          Have you checked my answer to the comment just above this with the screen shot?

          1. Yes sorry for mistake, 15.1 beta 2. Now I tried to make the sc again and all is ok.
            I want ask you one think: is there a solution for put light on for example in the night (specific %) in the afternoon in the morning? Make a Sc about lux but set light on in different % for different part of the day….morning evening night.
            I hope my English is good for make you understand ihih
            Thank you

            1. No problem. Yes, you can absolutely build a shortcut like this, and I can help you with it if you give me some specifics.

              Do you want to use both time of day and lux as conditionals? For instance:

              Between 0:00 – 5:59 (night)
              >> If lux is less than 80
              >>>> Turn on lamp to 20%

              Between 6:00 – 9:59 (morning)
              >> If lux is less than100
              >>>> Turn on lamp to 70%

              Between 10:00 – 17:59
              >> If lux is less than 300
              >>>> Turn on lamp to 100%

              Between 18:00 – 23:59
              >> If lux is less than 200
              >>>> Turn on lamp to 50%

              Something like this? If you let me know, I’ll create a screenshot of it for you 😉
              The solution will use partly this technique:

              1. Hi Stefan, thank you for all your time and availability.

                Yes I’d like that when the sensor see the movement and the lux is less than ?% the light on…but in the morning the light on at 9% , in the afternoon light on at 35% and the evening light on at 25% and in the night at 9%

                Can you make the SC like your example? And I will change the lux and % light for my use 🙂
                I’d like also ask you if this long sc would be fast or you think is slowly for active ?
                Thank you

                1. Hi Fede,
                  Sorry for the delay. Here’s the base of the shortcut. I hope you getting it working nicely. A friendly tip: the Home app has been quite unstable for me since the iOS 15 update, so I suggest “saving” the shortcut as you’re making it by pressing next up in the top right corner, and then go back into it to continue.

                  A few comments on this shortcut. The best way to do this is to do the different time conditionals nested, but it gets a bit harder to follow when you do it that way. Instead, I added the Stop running shortcut action in a few places, so the conditionals before that is True, and the lamps have turned on, it can terminate the shortcut at that point. And this should be very quick to activate. The shortcut is not “heavy” as far as how much computing is needed.

                  Let me know if you get it to work, or in case you’ve got any other questions about it 🙂

                  Time and Lux shortcut

                2. Hi Stefan,

                  Sorry for delay.
                  Your SC is amazing and it works 🙂
                  Sometimes it isn’t much fast but it’s ok, maybe the problem is that you use Philips sensor and it have hub while I’m using fibaro sensor that use HomePod hub and it is different.

                  If I have others questions or request I will let you know
                  Thank youuuuu so much

                3. Great 😀 Actually, my automations running shortcuts have all become a bit slower since iOS 15… I hope they will get back to their normal with future updates. And yes, if you’ve got any questions, just ask and I’ll do my best to help 🙂

                4. Hey Stefan,

                  Yes I have another question ihihi

                  can I put in the same SC that you suggested also the SC that you posted about light off after tot seconds (or continue the light on if it says movement)? Or I need to make another Automation?
                  Thank you

    1. You need to have two shortcuts. One for turning on the lights when the motion sensor detects motion, and a second for turning off the lights when the motion sensor detects that motion has stopped. You use the one I posted for you for when motion has been detected, and the one at the top in the post for when it stops detecting motion.

      1. Hi Stefan,
        Im trying a Sc with only “TimeNow between” but it doesn’t work if I put between 1730 and 200 (17.30 and 2:00) and another TimeNow between 201 and 800 (2.00 and 8.00)

        Have you a solution of the why it doesn’t work?

        I can’t add the photo about SC sorry, I don’t know why ihih

        1. Hi Fede,
          Yes, that is expected behavior. As far as the shortcut is concerned, you are only comparing two numbers, not time values. If between 1730 and 200 is really the same as If between 200 and 1730 as far as the shortcut is concerned. You need to split it up into two If-statements:

          If TimeNow is between 1730 and 2359
          >> If LuxNow is less than X
          >>>> Set lamps to X
          >> EndIf

          If TimeNow is between 0 and 200
          >> If LuxNow is less than X
          >>>> Set lamps to X
          >> EndIf

          I wrote about it at the end of this post under the headline Common mistakes.

          Hope this helps.

          1. Hi friend 🙂 I hope I can call you so after much messages 🤣
            With so much time the SC doesn’t work….so the only problem is put 4 number with 4 number and 3 number with 3 number

            So is better if between 17.30 and 23.59 if between 0 and 200 and if between 201 and 810…..
            If I would put 10.00 and 1.00 The correct SC is between 10.00 and 0059, if between 0 and 100 right ?

            I hope it’s correct and I understood ihihi

            1. You are correct. If you want to check between 10:00 in the morning and 01:00 at night, you need to divide it into two conditionals:
              if between 1000 and 2359
              if between 0 and 100.

              You don’t need to put 4 digit numbers together and 3 digit numbers together. If you wanted to check between 08:00 in the morning and 15:00 in the afternoon, you would just put:
              if between 800 and 1500.

              The only case where you need to be careful is when you go past midnight, and that’s when you need to divide it into two if-statements.

              I hope this helped 🙂

  10. Hi Stephan,

    First of all, thank you for all the helpful examples and shortcuts you have been posting.

    I have used the example as you showed it in the original article, and in most cases it works as intended.

    My question is whether there is a way to stop the shortcut of looking for No Motion if I manually turned on a light (or through a scene).

    What I mean is, if I go in a room then Motion is detected hence, lights turn on.

    If I sit at my desk then the 3×30 starts running and turns off the lights. Is there a way that if I manually turn on the light and/or enable a scene to delay the No Motion shortcut even further?

    Thank you.

    1. Hi Andreas,

      Thank you for your comment and your great question. Unfortunately, what you’re asking for doesn’t work directly in HomeKit. However, I’ve solved the problem by using an extra smart plug 🙂 If you happen to have an extra smart plug (or are able to buy one), you can use that as the enable / disable switch for the motion sensor.

      The smart plug needs to be plugged into power, but nothing is plugged into it. You could name it “Disable motion sensor” and add it to the scene that you will trigger when you want the motion sensor to be disabled.

      You then just add the following to the top of the shortcut that runs when no motion is detected:

      If “Disable motion sensor” (the smart plug) Is On
      >> Stop running shortcut
      End If

      By doing this, the shortcut will start by checking in case that smart plug is turned on (which you’ve done in the scene), and if so, stop the shortcut and leave the lights on.

      It’s a bit sad that you need to get an extra smart plug for this, but at the moment it’s the only way to solve it. I used to have 3 or 4 smart plugs just for these kind of things, but has since started using a Raspberry Pi running Hoobs (Homebridge) so that I can use virtual dummy switches to do just this.

      If you need help setting it up, just let me know and I can run you through it step by step.

      1. Hi Stefan,

        I’m back. I finally found time to tried your suggestion but it doesn’t seem to work.

        What I’ve done is: (for this I used a door sensors rather than a plug or a dummy switch)

        If door contact sensor state is open >
        Stop Running Shortcut >
        And then it had an end if here but it did work, so I move the rest of the shortcut above the end if which became
        If Light A is On >
        Repeat 30 times >
        Wait 3 seconds >
        If Motion sensor Motion is detected >
        Stop running shortcut >
        End if >
        End repeat >
        Set lights to off >
        End if >
        End if >

        I’ve messed it up somewhere because it doesn’t turn the lights off if the door sensor stated is closed and No Motion is detected.

        Can you spot the issue?

        Thank you in advance


        1. Hi Andreas,

          So, if the contact sensor’s state is open the light should stay on, and if the state is closed the shortcut should test for 90 seconds if motion is detected, and if no motion detected during that time, the lights turn off.

          The flaw in your shortcut is that by moving the End if, the lights will never turn off. As you step through the beginning of the shortcut:
          If door contact sensor state is open
          >> Stop Running Shortcut

          The shortcut will never get past this since the whole logic is inside If door contact sensor state is open, and if that is true the first thing it does is stopping the shortcut. However, if you use the Otherwise after Stop Running Shortcut, it will work.

          (By the way, I’m using the “>” character to show indentation, just to make it more readable.)

          If door contact sensor state is open
          >> Stop running shortcut
          Otherwise (if the contact sensor is not open)
          >> If Light A is On
          >>>> Repeat 30 times
          >>>>>> Wait 3 seconds
          >>>>>> If Motion sensor Motion is detected
          >>>>>>>> Stop Running shortcut
          >>>>>> End if (linked to If Motion sensor Motion is detected)
          >>>> End repeat
          >>>> Set lights to Off
          >> End if (linked to If Light A is On)
          End if (linked to If door contact sensor state is open / Otherwise)

          This should work. If you have any problems with it, please let me know 🙂

          1. So if i have 2 lights (A and B) in a room and door is must open . My motion sensor is usually detected motion for every 1 min
            (Automation 1) :When i walk in the Room > light A is turn on for 90s
            (Automation 2) :
            If i’m still in there for next 90s => light B turn on and keep A on
            If I’m not => all light turn off

            Can you help to check (Automation 2) below
            If door contact sensor state is close
            >> Stop running shortcut
            >> If Light A is On
            >>>> Repeat 3 times
            >>>>>> Wait 30 seconds (just don’t know if reverse the repeat and wait working?)
            >>>>>> If Motion sensor Motion is detected
            >>>>>>>>Turn on Light B
            >>>>>>>> Stop Running shortcut
            >>>>>> End if
            >>>> End repeat
            >>>> Set lights A+B to Off
            >> End if

            1. Hi Daniel,
              So, I made a simple pseudo-shortcut to see if I understood correctly what you want to do. This would all be in one single automation. Does it look right?

              If motion is detected:
              >> Turn on light A
              >> Wait 90 seconds

              If door is closed, stop running shortcut
              During the next 90 seconds,
              >> if light A is on and motion is detected
              >>>> turn on light B
              >>>> stop running shortcut
              if time runs out
              >> turn off lights A and B

              And yes, there is a difference in the wait loop if you check every 3 seconds or every 30 seconds. If motion is sporadic and not constant, the motion sensor state will go between three different statuses: motion detected, motion stopped and no motion (they might have slightly different names). If a single motion is detected, the status will change from no motion to motion detected. If no further motion is detected, it will go to motion stopped after about 4 – 5 seconds, and then after another 4 – 5 seconds it will go back to the default status: no motion. By checking the status every 3 seconds, we are able to catch these when they happen. If you only check every 30 seconds, there’s a possibility that the whole cycle (no motion -> motion detected -> motion stopped -> no motion) happens while your shortcut is taking the 30 second wait.

              1. Hi Stefan,

                Thanks for your quick reply. but seems i can’t combine in one short cut i have to separated into 2 automotions
                For “During the next 90 seconds”

                If i choose repeat 30 for 3s => motion detected when i walked in and turn on light A , at first 3 second => motion detected still there and light B turn on

                So i only have 1 choice that to turn on light B for the 2nd motion detected is know the exact period and put in
                Repeat 1
                Wait (period , e.g: 60-65s)

                Do you have any idea to check at the 2nd detection (without know the period) then light B turn on

              2. Ah i can do it now .
                Just still use during 90s by after waiting first90s
                repeat 30
                wait 3
                > turn on B
                >endif/end repeat
                turn off A

                Thanks alots, work like a charm now

                1. Yes! That’s exactly how I would do it 🙂

                  Just one thing to remember if you want to lengthen the wait time. The total shortcut runtime can’t be over 10 minutes if you use a AppleTV or HomePod as a HomeKit Hub, and even shorter in case you use an iPad as the hub.

  11. Thank you for your reply and sorry for spelling your name wrong :/

    I’ve got a few Meross plugs lying around so I can try what you have suggested but I am also using homebridge for SwitchBot Bots, so maybe the dummy switches route might be easier.

    Thank you again!

    1. No problem 😉

      If you’ve already got Homebridge running, then dummy switches are definitely the way to go. And there’s so many uses for them 😁 I’m actually writing a new blog post about how to use dummy switches (or extra smart plugs) for different things.

      It might be a good idea to create a new room in HomeKit (I’ve called min Automations) for the dummy switches, so you don’t crowd your real rooms with them.

      1. Hi friend Stefan 😊

        I can’t wait to see you post about dummy.

        So will be good to do a dummy for to stop the motion sensor, for example:

        Call dummy “lights on block” and I put the action disable motion sensor (int he room that I want) ———> all this for block the motion sensor and not have time 3×30

        After it I must disable the dummy and the action comes back at origin

        Does it right? If it’s ok I will do immediately 😊

      2. Ah I have also another question, saw that there is a possibility to have Timer dummy I’d like to ask you some examples for to use Timer dummy for the house. It’s the same use for light on and after 3 minutes put light off?

        1. Hi Fede,

          Yes, you have a dummy switch called “Lights on block”, which you turn on when you don’t want to override the sensor. However, you need to add to an if-statement to the shortcut that turns the lights on with the sensor and an if-statement to the shortcut that turns the lights off with the sensor.

          You would start both shortcuts with this:

          If Lights on block Is On
          >> Stop Shortcut
          End If

          This way it starts by checking if you have disabled the motion sensor, and in that case just stops the shortcut.

          Now, depending on how you do this, you could build in a timer function for the “Lights on block”, so it goes back to normal even if you forget to turn it off. If you are using a dummy switch in Homebridge or Hoobs, you are able to set a Time value for how long the Dummy switch will wait before turning off. This number is set in milliseconds. So depending on your use case, it might be useful to put it to auto turn off the “Lights on block” after a few hours, in case you forgot to turn it off yourself.

          If you’re not using a dummy switch in Homebridge or Hoobs, but rather using a smart plug to do the same thing (when the smart plug is turned on, the motion sensor is turned off), you are not able to do the exact same thing. But if control the smart plug from a button (like a Hue Dimmer Switch), you can set an auto turn off timer that way.

          Am I correct in thinking that the Timer dummy you’re talk about it the Timer function on the dummy switch to turn it off? In that case, yes it can be used as a timer to turn lights off. You can even set it to be “Resettable”, which means that when you turn the Dummy switch on, it starts the countdown to turn off. But, if you turn it on again (even though it is already on), it will reset the timer.

          I’m actually using this as a “belt and suspenders” solution for the kitchen lights that are usually completely operated by the motion sensor. I’ve noticed that after the iOS/tvOS 15 update, every now and then the turning off the lights when no motion has been detected doesn’t work. So, I’m using a dummy on a timer, which resets every time the motion is detected, which turns off the lights when the dummy is turned off. The timer on the dummy switch is set to 5 minutes. So, in the rare case when the normal shortcut to turn off the lights after 2 minutes with no motion doesn’t work, the dummy switch with the timer turns them off a few minutes later.

          Sorry for the long answer.

          1. Hi Stefan,

            Don’t worry your reply it’s very good for explain everything, thank you.

            You told me that both shortcut for light on and for light off, but I use your shortcuts for make it.

            One shortcut for lights based on the light sensor and one shortcut for light off when motion sensor see no motion; so I think I need put your

            “If Lights on block Is On
            >> Stop Shortcut
            End If”

            Only in the light off shortcut right?

            However I use in the bathroom one hue switch so I can do that when I push the button light on I put also the action for dummy “lights on block” ON and when push the button for lights off the action for dummy “lights on block” OFF

            What do you think? It’s it correct?
            Thank you

            1. It depends. If you only want to prevent lights from turning off, it’s enough to put it in the one shortcut. However, if you want to use it to disable the motion sensor completely, you need to put it into both shortcuts. I want to have the ability to use only natural light in the kitchen on a sunny day and keep the motion sensor from turning the lights on, and that’s why I’ve put it in both shortcuts.

              1. Hi bro Stefan,

                I don’t know if you tried this shortcut that you done for help me with the new iOS 16 beta.

                I had to redo everything cause it doesn’t work anymore.

                But I have this problem, it’s working now but at every time in day it turns on 10% light ….I settled it only for 0 and 959 but in every time of the day it turns on 10% light.

                It’s also weird that the first shortcut that we done it works only if I open it and I force the automation, and it works very good and not set 10% light…..the oddities of life ahahah

                Have you some advices for me for help me please ?

                Thank you bro

                1. Ooooooooook I wrong to put the info in the current data and now everything work 🙂

                  Sorry for bother and I can’t wait to see others good ideas from you for HomeKit.

                  Thank you and have a nice weekend

  12. Is this still needed with iOS 16.2? I tried with the default “turn off after n minutes” but this doesn’t work if the light was switched on manually with a Hue Button.

    1. I guess it depends. As you noticed there are cases when the default doesn’t work. I also know that many people want to be able to customize this function, and doing it within a shortcut gives you that freedom. For instance, you can use a Hue Button to set the lights to toggle between 100% and 95% brightness and then set the shortcut to not turn off the lights if the lights are at 95% brightness (a sort of override). Perhaps you want different behavior depending on the time of day, or if it’s the weekend. And many use the more advanced version I posted about, where the lights first dim down giving you a warning, and then 30 seconds or 60 seconds after that turn off in case no motion has been detected.

  13. Hi Stefan.

    I hope you can help me. I have just installed a Aqara G4 doorbell which has a motion sensor inside. I’m using it to control my outside door light however when the light turns off the doorbell thinks it sees movement and as such the light comes straight back on. I have currently an automation which is half working. Basically it’s one automation for when it detects motion. It will turn the light on. Wait 60 seconds turn the light off wait 6 seconds to overcome the doorbell sensing movement when the light turns off. However I would like an automation which resets a timer while it’s still on then finally once no movement detected it turns the light off with my delay. Any ideas how to achieve this?

    1. Hi Steve,
      This sounds like an interesting problem to solve. Is the outdoor light dimmable? I’m wondering if the motion sensor would activate if you turned it off a bit more slowly. Starting after 60 seconds: wait 3 seconds to go down to 90%, wait another 3 seconds and go down to 80%, and so on.

      If that’s not working, do you happen to be using Homebridge or Hoobs? This would probably be a place where using a dummy switch could work.

      1. Thank you for replying back to me. Yes the outdoor light is dimmable. I have even tried setting it at 1% but it still activates the motion sensor off. Aqara have told me that when the G4 is connected via HomeKit it turns the pir off and uses image based detection instead. I’m not sure I agree with this as if something moves in the wind it doesn’t set it off.

        My home setup is simple.

        Apple TV as hub connected via Ethernet.
        2 x Aqara G4 doorbells. Front and back
        1 x HomeKit led bulb. Fitted outside in the light fitting at the front door
        1 x Hive thermostat.

        No dummy switches no other hubs or bridges

        1. Do you mean that if you go from 1% to off, it still triggers the motion detected, or going from 100% to 1% it triggers the motion detected status?

            1. That will make it a bit harder to solve then. I guess putting something like a second Hue motion sensor outside would be out of the question (I’ve been using them with no problem).

  14. Dear Stefan:
    I have 2 Hue lights that turn on when motion detected using Logitech Circle Camera. This part works fine. The Logitech circle camera’s motion sensor (pixel based sensor) exposed to the automations detects changing light as motion. This leaves my light in a constant cycle of on / off.

    Is there a way to modify the shortcuts to avoid this issue without adding a PIR sensor. I am using 2 automations, one to turn on the lights and another one to turn them off (using your ideas).
    Thanks in advance for your help.

    1. Hi Arun,
      And thank you for the question. This seems to be a problem with at least the Logitech Circle Camera and the Aqara G4 doorbell. The Aqara G4 doorbell was so sensitive, that even if you had the light at 1% brightness and turned it off, it would trigger motion detected. This is a test you could do with the Logitech Circle Camera. If you have the lights at 1% brightness, and then turn them on, will this trigger motion detected?

      If you can get away with that, you could change the automation for turning the lights off to dim them to 1% for 10 seconds or so before actually turning them off.

  15. Hi Stephan:
    Thanks so much for your quick reply. Is there a way to write an automation. Ignore first motion detected (that could be light turning off) and then the second motion detected turns light on. I will try your suggestion and get back to you shortly. Thanks

  16. Hi Stephan:
    Sorry for the multiple messages. With the Logitech only a final delay of 3 seconds (not 2 or 4) after dimming lights to 1% before turning the lights off works. The motion sensor still tries to turn back the lights on (brightness increase back to 100%0 but after maybe 1 second the lights turn off. Can’t make sense out of this. Is there any way to address this. Very grateful for all your help. Thanks

    1. Haha, yeah, that behavior of the Logitech is a bit strange.

      As you alluded to earlier, it is possible to make a shortcut to run when motion is detected that waits to see if further motion is detected before turning on the lights. I just tried doing this with my Hue motion sensors just as a test case. The problem is that I need to put a delay of 4 or 5 seconds for it to work (since the motion sensor’s state “Motion is Detected” stays on for just over 3 seconds). That means it could be a little bit annoying for you if it always takes 5 seconds for the lights to turn on after you’ve entered the area.

      The way I wrote it was:

      Wait 4 seconds.
      If Motion sensor Motion is Detected
      Turn on light
      End If

  17. Hi Stephan:
    Thank for testing the shortcut with the delay to detect motion. In your previous suggestion of dimming lights to 1% before turning them off; here is what I think is happening.
    1. When the lights dim from 100% to 1%, Logi Circle Camera treats that as motion detected and tries to turn the lights back on (the brightness increases to 100%).
    2. Then I have a 3 second delay – This is where it goes into Motion Clear (no motion detected state)
    3. It’s in this precise interval (or no motion) the last part of the shortcut is able to turn off the light
    Your thoughts? Again, extremely grateful to you
    Basically, after dimming lights (running your shortcut with repeat, no motion detected etc.)
    Wait 3 seconds.
    Power Lights off

    1. Ok, good to know what exactly happens. And you can solve it by adding a check in the “When motion is detected, turn on the lights” automation. If you turn that into a shortcut, you can start that automation by a nested conditional (if-statement) which first checks if the lights are on, and then if the lights’ brightness level is less than 5%. If both these are true, you’ll just stop the shortcut. Below this, you’ll turn on the lights to 100%

      If Lamp Is On
      >> If Lamp Brightness is less than 5%
      >>>> Stop this shortcut
      >> End If
      End If
      Turn lights on to 100% brightness

  18. Stefan, this was very useful not that I have. HomeKit system set up but I am looking to move over from HomeSeer. In that system I had to code a number of things to get a wife friendly system and was wondering if I would need to do the same. One of those was using multiple motion sensors and multiple light sensors to determine if I had move,ent in the room or keeping the average lighting. This coding looks as if it will allow me to do that. In HomeSeer I had to also code in persistence which forced the expected state every 2 mins as my hue lights occasionally missed the command.

    Now to the point of my message do you have a basic introduction to HomeKit as whilst I have HomeKit with one device I do not know where my hub is or the differences between shortcuts, automations etc. Also does any hue hub work with HomeKit and if so how do you connect it, so I can experiment?


    1. Hi Steve, and welcome to the HomeKit world 😁

      I don’t have a basic instruction post here (perhaps that would be a good idea), but I can give you a short answer here.

      The HomeKit Hub is either an AppleTV, HomePod, or HomePod Mini (you used to be able to use an iPad as a HomeKit Hub, but I don’t think that’s the case anymore, and that wasn’t the best idea anyway). If you’ve got more than one of these at home, they can all be hubs, and the system decides which one is the active hub, and the others will be standby hubs. You are able to see your hubs and bridges in the Home app by tapping the three dots in a circle in the top right corner and choosing Home settings.

      As to the Hue Bridge, I believe that the old round Hue Bridge v1 is not compatible with HomeKit. The newer one is rectangular with rounded corners.

      There are two ways to connect the Hue bridge to HomeKit and the simplest is probably to open the Home app, press the + (top right), Add Accessory, and scan the QR code on the back of the Hue bridge.

      As it comes to terminology and the difference between an automation and a shortcut: in HomeKit an automation is an action triggered by an event. An event could be a specific person arriving or leaving, or the last person leaving and the first person arriving. It can also be a time of day, a sensor detecting something, or an accessory being controlled (a lamp turning off).

      If the desired result of the automation is simple, that is a HomeKit accessory being controlled (a light being turned on or off or set to a specific color or brightness, or perhaps music starting playing or being paused), you can easily configure that in the automation menu. However, if the result of the automation needs more logic (conditionals and computation), for instance, you want to check if some other accessory is turned on or check if the room is already bright enough before turning on a light, then you are able to convert the result of the automation into a shortcut which let you add this logic (it’s like a sort of scripting).

      If you’re using Hue Dimmer Switches, you are able to configure them within HomeKit, and here Shortcuts can be quite powerful. For instance, each button can have a different action depending on the time of day, or if it’s a weekday or weekend.

      I hope this is a small introduction to your HomeKit journey 🙂 And if there’s something you’re wondering about, I’ll try to help.

  19. I just added a humidity sensor to turn on the exhaust fan when the shower is on. Now was wondering if there is a way to incorporate another if statement to keep the lights on while fan is on…because the motion sensor does not see us In the shower.

    Is that possible? Thanks a lot…love your site

    1. Hi Anthony,

      Yes, that should be pretty easy. You could just add an if-statement to the top of the shortcut that turns the lights off when the motion sensor stops detecting motion.

      If exhaust fan is On
      Stop this Shortcut
      End If

      And then the rest of the shortcut.

  20. It’s March 2024. Is this still the best way to do turn off lights X minutes after last motion is detected?

    1. I’m using a variation of this more advanced version, so I haven’t actually tried the simplest way to do it for a long time as I have needs for a lot of conditionals. I’ll check on the simplest way to do it, and get back to you later this week.

Leave a Comment

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