HomeKit Automation Tips

Getting more out of your smart home devices

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.

Update

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.

Advertisements

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. 

Advertisements

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.

141 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,
    /Fredrick

    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
    Otherwise
    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
      Otherwise
      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 🙁

    Thanks

    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 😉 http://homekitautomationtips.com/how-to-use-a-motion-sensor-to-turn-your-lights-off-with-homekit-but-giving-you-a-warning-first-more-advanced-version/

      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: https://homekitautomationtips.com/the-power-of-the-format-date-function-and-how-to-use-it-for-shortcuts-using-time-of-day-or-sunrise-sunset-part-1/

              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
          EndIf

          If TimeNow is between 0 and 200
          >> If LuxNow is less than X
          >>>> Set lamps to X
          >> EndIf
          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
              and
              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

        Andreas

        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 🙂

  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.

Leave a Comment

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