Hello - Need some help with a timer automation. Basically, when motion is detected between a specific timeframe (condition), start the timer and turn on some lights… Once it finishes, turn the lights off. I think my automation is stuck in the conditions section as when timer.finished triggers, it won’t fall through the conditions to my Choose action. Can someone help get me over this logic hump?
condition:
- condition: time
after: "01:00:00"
- condition: and
conditions:
- condition: sun
before: sunrise
First of all your AND statement is after the first condition. So its never going do an AND compare between the 2.
Conditions are already AND so listing multiple will act like that. Conditions - Home Assistant
2nd: For some reason just putting before sunrise will cause the condition to fail, you can test this in the automation by using the Test option with it. Someone can prob explain why this is, but it works like this
condition:
- condition: time
after: "01:00:00"
- condition: sun
after: sunset
before: sunrise
Instead of turning off the switches 45 minutes after motion is detected, why not simply turn them off after no motion is detected for 2 minutes? You can use a State Trigger to do that with its for option set to minutes: 2 and don’t need a timer entity.
Or is there a requirement to have the lights on for a minimum of 45 minutes?
2 Minutes isn’t long enough in my situation… and if HA is restarted/updated during the “for” period, time is lost and the lights would never turn off… This is exactly the reason the timer helpers were created.
I have similar automation. I have currently motion sensor and I want to turn off light when there is no motion for 2 minutes. But if in 2 minutes motions is detected, again, then reset timer and start count down again.
This is what I have and it’s working perfectly fine:
It was merely a suggestion; the time period can be whatever you want. If there’s no morion detected for 5 minutes then it implies no one is present (unless they’re sitting on the toilet) and it seems prudent to turn off the lights and not wait another 40+ minutes. However, it’s your choice how you want it to work.
That’s true but if the for period is short then the chances of being interrupted by a restart are minimized (in my case, the host machine is protected by a UPS so I’m not concerned by unscheduled restarts). There are other ways to mitigate this situation on startup but it sounds like you prefer to use a timer so I won’t dwell on alternatives.
BTW, I recall a bug report claiming a timer entity’s ability to start its countdown again, after being interrupted by Home Assistant restarting, wasn’t working. Perhaps it has been fixed but have you recently tested an interrupted timer’s ability to pick up from where it left off?
That’s the usual way of implementing a motion-controlles light (and what I had suggested to Brewder). However, as pointed out by Brewder, the for’s countdown doesn’t survive a restart so the preference is to employ a timer.
OK I cleaned up my previous conditions block but I’m still struggling. The lights never go OFF. I suspect that’s because once the timer finishes, my conditions block never allows it to fall through to the Choose actions where I trigger the lights OFF.
I think I need to add an OR statement to account for situations when the time was activated between 1am and Sunrise but expires AFTER that time period.
Example: Motion is detected at 06:15am and before Sunrise at 06:30. 45 minute timer starts. Timer expires at 06:45… The timer.finished trigger activates but won’t PASS the conditions block as it’s no longer between 1am and Sunrise… How do I get it to “fall through” to my choose actions?
Why don’t you use state for a trigger like I do? Probably your event doesnt exist or doesnt exist in this form so your automation never triggers to turn off the lights.
That’s a great question. I followed another tutorial that showed the coding for “timer.finished” as the event type and I set it up my home office lighting automaton. It seems to work great there so I was duplicating the setup.
If I converted to use state, I guess the state goes from running to idle is how I would code that?
I’m pretty confident the event check for timer.finished works fine… I just re-checked my home Office automation and the timer documentation here: Timer - Home Assistant (home-assistant.io)
So I’ll just need to keep testing my hallway automation or simply add a failsafe automation that ensures the lights are off at a specific time… it’s not ideal but will work.