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:

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

94 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

Leave a Comment

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