Motion sensor - turn off when no motion, but light has been manually turned on


I have a hue motion sensor and a hue light, and I’ve setup a shelly in detached mode. This means that I can detect when the light switch is pressed but the switch doesn’t physically switch the light, it’s done through home assistant instead.

Everything is working well:

  • I have an automation that listens for the light switch toggle, and toggles the hue light.
  • I’ve used the built in motion sensor template, and this works well to turn on the hue light when motion detected, and then turn it off when no motion detected after a specified time.

What’s missing is that I want the light to turn off if no motion is detected, after I have manually switched it on using the switch.

How do I go about this?

This turns on the light when motion is detected and off when no motion has been detected for x minutes. The light should turn off when it was switched on manually too, provided motion is detected after the lights have been turned on. (So if it’s possible to flip the switch manually and not go into the room, it may not work.)

Motion activated lights automation.

You may need to adjust your toggle automation, though, to make sure it keeps in sync with the actual state of the light.

Thank you @Stiltjack.

I spent some time comparing the script you provided with the inbuilt template and understanding the differences between them.

The script above is clever in that it works for both on and off, with minimal code. It takes advantage of the “for” function so is bound to some sort of event that fires after the state of the sensor has been off for two minutes.

Alternatively, the template also turns the light on and off, but uses the mode:restart to restart the script if motion is detected within the delay. So unlike the first option which seem to be bound to an event for motion not being detected, this one just continually resets the script again when the motion is detected. The template uses more code and isn’t as elegant.

As you alluded to, in both cases, the motion countdown doesn’t start until motion is detected to begin with so pressing the button (without the motion sensor seeing you) or turning the light on via another automation or app will not kick off the motion detection. But as soon as you enter the room and motion is detected, the motion countdown starts.

Neither solve issue of turning the light on by non-motion sensing e.g. an automation, it will not turn off if no motion detected. I’ve put that in the two hard basket as most of the time I’ll enter the room to kick off the countdown, and each day I turn all my lights off via an automation when I go to bed so that will turn it off daily as a last resort.

Hope this helps someone trying to determine the difference between the two approaches.

1 Like