Light on/off automation based on how long motion is detected

Hello all,

I have a light switch in my guest bathroom that I wish to configure in such a way that depending on how long motion is detected in the room, the light will go off after a different time. I’m still learning automation in HA, so please bare with me.

First off, the devices. I have a Smartlife light switch, and an Aqara Motion Sensor P1.

Here’s my thinking -
Scenario 1: A person walks into the bathroom just to quickly wash their hands, no longer than a minute. Motion is detected for up to one minute. I would like the light to turn on upon motion, but then have the light turn off immediately after no motion is detected.

Scenario 2: A person walks into the bathroom and is in there for more than a minute. Motion is detected for more than a minute but potentially no motion is further detected because the person is showering and the shower curtain is drawn. I would like the light to turn on upon initial motion and stay on for 15 minutes, regardless if motion is further detected or not. After 15 minutes, if there is no further motion, the light will turn off. If within that 15 minutes, motion is again detected, the light will stay on for another 15 minutes.

The second scenario is more like those analog motion sensing light switch, where it will stay on for however many minutes you set it to until no motion is detected.

I’m not sure how I should set this automation. As I’m writing this, I’m starting to think that it might not be possible, but hopefully it is. I have a feeling that one automation will overwrite the other.

I currently tested this with 3 automations -

  1. turn on the light based on motion and that’s it
  2. When motion sensor changes from detected to clear for 1:00, turn off the light
  3. Well, I had a third one but as I was writing this, I think my third one was completely incorrect, so I don’t have a third one.

Any suggestion on how I should do this?

Also, on a separate topic, how do I create an automation where I can edit the yaml file? I tried creating a light-off automation with a delay that I add manually to the “edit in yaml” section, but whenever I save it, it disappears.

Hi @Didgeridrew, thank you for your response. How do I go about implementing this and editing it to my needs? Sorry I’m really new to HA and still figuring out how to create automations directly in YAML. Do I just go to new automation, and then click the 3 dots and choose edit in YAML?

Yes, that will work. If you’re more comfortable using the UI Editor you can use the provided YAML as a guide. There is a good (though slightly dated) primer available on the ResinChem Tech YouTube channel.

Apologies for my late reply was out of town and didn’t have a chance to check on this.

Thank you again for your guide @Didgeridrew, I will give it a try and follow up if I run into any problems.

While I have you here, I was wondering if you can help me with something as well? I have a feeling it’s in a way related with creating an automation from scratch, as you had recommended, but I want to see your thoughts on it.

I posted this topic a while back, trying to figure out a way to do a smooth fade in/out when my light switches turn on and off, similar to how Lutron does it, but nobody responded to my post. I then found this post that supposedly you can apply to any light switches that don’t support “transition”.

The poster mentions that this is a script, so do I follow this guide in the “script” section of Automation & Scenes? And again, similarly by doing it in “Edit in YAML” section?