Home Assistant automations with conditions and duration triggers have a limitation: if an entity has been in a state longer than the specified duration when a condition becomes true, the automation won’t trigger.
Example Use Case: I want lights to turn off after being on for 30 minutes, but only during nighttime hours (10PM-6AM). Currently, if a light is turned on at 9PM and left on, it won’t turn off at 10PM despite being on for >30 minutes because the duration trigger fired at 9:30PM when the time condition wasn’t met.
Proposed Solution: Add an optional “or longer” checkbox to the “for” duration field in automations that would:
- Re-evaluate the trigger when conditions change from false to true
- Check at Home Assistant startup if the conditions are met and the duration has elapsed
- If needed, allow periodic checks for long-duration states meeting both trigger and condition requirements
Technical Implementation: Add an or_longer: true/false
option to the state trigger’s “for” parameter in YAML, and a checkbox in the UI.
Benefits:
- Makes automations more intuitive, especially for beginners
- Eliminates the need for workarounds using multiple triggers, templates, etc.
- Reduces the learning curve for new Home Assistant users
- Supports more reliable presence-based and time-based automations
Backward Compatibility: This would be an optional parameter, so existing automations would continue to work without modification.
Documentation: The “or longer” field could be described and documented as follows:
Ordinarily, a trigger (When) with a “for ” specifier will only fire once when the trigger state has been true for the specified time. If “or longer” is checked, then the trigger will fire again if the trigger state has been true for the specified time or longer and any of the conditions (And if) change state. Likewise, the trigger will fire at Home Assistant startup if the trigger state has been true for the specified time or longer.
For example, if the trigger (When) is “When the Kitchen Light changes to On for 30:00 or longer” and the condition (And if) is “If the time is after 10:00 PM and before 6:00 AM”, then the trigger will fire when the light has been on for 30 minutes AND at 10:00 PM if the light has been on for 30 minutes or longer.
Background:
I want automations that automatically turn off lights or lock doors after a certain duration, but limited to a time window (say 10 pm to 6 am), or conditions like no recent motion. I’ve read the docs, checked the community forum, talked to an experienced HA user, asked two AIs, and researched HACS integrations (like Entity Controller) without finding a great solution.
An automation to turn off a light that has been on for 30 minutes will not run if the light was turned on more than 30 minutes before the time condition window.
I tried to fix this by adding another trigger for “time = 10:00 pm”. This solves the problem if the lights are left on from before 9:30 pm, but it will force the lights off at the trigger time, even if you just turned them on 10 seconds ago, failing the 30-minute duration requirement.
Next, I created a template sensor to report the on-duration of the kitchen light and an automation that triggers once a minute with the conditions: time between 10:00 pm and 6:00 am AND the light on-duration >= 30 minutes. This works! But it requires custom YAML, creating an extra sensor for each such entity, and although it may be negligible, I’m troubled by firing events once a minute (or even every five minutes) to perform this test. It means my automation trace will be filled with pseudo-events, and I suspect it also results in a lot more logging. This is tricky enough that many users won’t figure it out and won’t get the behavior they want.
I did more experiments using value templates, different trigger times, and different conditions. Some worked, some did not, they were all a bit tricky and unintuitive.