this post was submitted on 27 Jan 2024
16 points (100.0% liked)

homeassistant

14277 readers
23 users here now

Home Assistant is open source home automation that puts local control and privacy first. Powered by a worldwide community of tinkerers and DIY enthusiasts. Perfect to run on a Raspberry Pi or a local server. Available for free at home-assistant.io

founded 2 years ago
MODERATORS
 

I have a bunch of presence and motion sensors (like four, but shush) and when people leave rooms I would like to turn the lights off after five minutes, however if someone returns to the room before that five minutes is up, I'd like to start that countdown again. Is there a grateful way to do this that isn't me just doing if no activity for five minutes, turn off the lights, else wait five minutes and then turn off the lights. Because that's ugly, rigid and not very smart at all.

all 31 comments
sorted by: hot top controversial new old
[–] [email protected] 10 points 1 year ago (2 children)

You should be able to handle this in HA within a single automation:

Trigger: Room is occupied (occupancy > 0)
Condition: Light off
Action:

  • Call service: Turn Light On
  • Wait for Trigger: Room is clear for 5 minutes (occupancy < 1)
  • Call service: Turn Light Off

If you are basing your occupancy on more than one sensor state, you could build a helper to combine the states into a single sensor value, which itself might need a Hysteresis helper.

I've started moving some of my own automations over to this method. It works pretty well, but it is susceptible to being interrupted by restarts or reboots. You may need to build in additional logic to reset things to a known state on startup.

[–] [email protected] 4 points 1 year ago* (last edited 1 year ago) (1 children)

Bloody hell! So I saw this and it was like my vision cleared! I'm actually lost for words, but thank you. I don't know why I didn't realise what wait for trigger did, but holy fuck, you're beautiful, thank you so much!

[–] [email protected] 3 points 1 year ago (2 children)

It's also important to set the mode of the automation to "restart", then it works flawlessly

[–] [email protected] 1 points 1 year ago (1 children)

Aha! THAT'S what that does. Thank YOU too!

[–] [email protected] 2 points 1 year ago (1 children)

I don't want to set a misunderstanding: this does not solve the state on reboot issue, maybe "flawlessly" is not the correct word. On a reboot, all automations are always stopped, so that does not help here.

[–] [email protected] 1 points 1 year ago

No, no. I do sometimes have to contend with race conditions and reentrant triggers. I never considered looking into whether it was possible to change the execution mode, even if I did read that documentation without understanding what it meant.

Thanks for the pointer back to it. Now I grok it.

[–] [email protected] 1 points 1 year ago (1 children)

What do you mean? Is that different than repeat until?

[–] [email protected] 4 points 1 year ago (2 children)

Are you doing all the logic in HA or are you using NodeRED?

I used to have a similar timer reset for my "going to bed" button that I could cancel/snooze before the events started firing. It was done in NodeRED, though, and HA just glued that logic to the UI and buttons.

[–] [email protected] 1 points 1 year ago (1 children)

How did you get into NodeRed?

[–] [email protected] 2 points 1 year ago (1 children)

I was complaining to a friend about how clunky it was to setup automations in HA, and they told me about NodeRED. lol. Haven't looked back.

[–] [email protected] 1 points 1 year ago (1 children)

This was before the UI or recently?

[–] [email protected] 1 points 1 year ago

About 5 years ago, so probably before.

[–] [email protected] 4 points 1 year ago (2 children)

hi logically i thing you are on the right track. A timer is defiantly the right way to go. You can try my blueprint for your automation. Hope it helps

[–] [email protected] 1 points 1 year ago

Thank you, I imported it and plan to learn from it.

[–] [email protected] 1 points 1 year ago

Hey, this is pretty cool! I need to study this, I'm trying to get the idea of blueprints into my head.

[–] [email protected] 1 points 1 year ago (1 children)

@sabreW4K3
Create a timer that you set to 5 minutes.
Create an automation with 3 triggers:

  1. Motion is detected
  2. Motion clear
  3. Timer changes to idle

Choice of three actions:

When 1:
Switch light on
Cancel timer (this takes care of motion being detected again during the 5 minute delay)

When 2:
Start timer (And let’s wait for 5. Minutes for timer to end)

When 3:
Check that motion is clear (as motion also cancels the timer)
If so, switch light off

[–] [email protected] 1 points 1 year ago

This broke my brain at first. I had to keep coming back to it. Thank you so much.