Automation question based on previous automation

Hi, I’m trying to make an automation condition that checks if another automation has been run within a time frame. The reason for this I’d like a light to come on if certain people are present automatically and automatically turn off when they leave if it’s after dark. However, if the first prescence automation never ran because I manually turned on the light, I would not like the auto off to turn off or execute. Thanks

So you have an automation that turns lights on when presence is on, and off when presence is off, correct? You don’t want the “off when presence is off” to trigger if the light was manually turned on?

If so, you can add another condition for your off automation, using an input_boolean. How do you ‘manually turn on’ the light… local hardware (like esphome reading an input pin), or via HA gui (toggling a “relay entity”)? That will affect how you go about using the input_boolean as an automation trigger. The latter, you’d probably be best off with using an input_boolean in the gui and simply turning on manually with that (vs the relay entity). Then the off automation can be bypassed when it’s on. For the former, you might keep the boolean hidden from the gui, and have esphome toggle it in the background if the light was “on via pin change”… then off when the pin changes to off. This way you wouldn’t even need the gui to manually turn it on/off… and your other automations would still roll on with the proper off bypass when manually on.

Hope I’m making sense here…

@truglodite’s proposed solution is a typical way to tell the difference between something happening due to an automation vs manually. But to answer your question:

# Check if AUTOMATION ran within the last N_SECONDS.
- condition: template
  value_template: >
    {% set last = state_attr('automation.AUTOMATION', 'last_triggered') %}
    {{ last is not none and
       (now() - last).total_seconds() <= N_SECONDS }}

ok this make a little more sense. Thanks.

1 Like