Action when automation ends

I want to create an automation that turns on a switch when certain triggers and conditions are met, however I’m not seeing a way to ensure the switch is turned back off when those conditions are no longer met and the automation ends.

I’m sure this is simple and I’m just not finding something in my searches

Create another automation with the opposite triggers and conditions to turn the switch off.

@tom_l I guess I was hoping to keep it all within one automation and not duplicate all my conditions. I prefer not to have to update them in two places anytime they change.

Is there not some kind of after hook? I guess I could create an automation that tracks the state of the other automation?

An automation is triggered only once, if triggers change. It does not keep running until the triggers are no longer met.

What’s your use case, I guess something like turning lights on on movement?

I have a ESP8266 that triggers a relay which turns on a breaker for my well pump. HA just see’s this device as a “switch”

The well pump pulls 3000W (i’m off grid) so I only want it to turn on when certain conditions are met: batteries fully charged, sun is out, at least 2 hours before sunset, water tank < 90% full…etc

There are a few more nitpicky conditions and I just don’t want to have to duplicate them in two different places to turn off the “switch” when the conditions are no longer met especially because I’m still futzing with it and they’re all subject to change. It also seems silly to have to duplicate them. As a programmer this goes against everything I was taught :slight_smile:

Show the automation you got and explain what it should do then we can help you.

I just started writing the basics of it but haven’t created it. I was scratching my head at my question above before writing it.

I think I solved my own issue. I can just create an automation that is triggered to turn the switch off when the main automation goes into a state of off

- alias: Stop well pump after automation
  trigger:
  - platform: state
    entity_id: automation.well_pump_on
    to: 'off'
  action:
  - type: turn_off
    device_id: 2838b165dc15bafb0795d3ab0743d782
    entity_id: switch.well_pump_switch
    domain: switch
  mode: single

And what turns the automation off in the first place? On/off for an automation means that it is enabled/disabled. An automation can only run when it is on. When an automation is triggered and does some actions it doesn’t turn the automation off at the end.

Then please explain what you want to do exactly.

On/off for automation means enabled/disabled, not running or stopped.

My recommendation is to make every criteria a trigger of your turn-on automation and repeat every criteria as a condition in the same automation. Because in the trigger section any of the conditions triggers the automation, while in the condition section all conditions must be met to execute the automation’s action.

Then, make a second automation to watch for any of the criteria above to go below threshold (like battery charge or watertank level) to turn the pump off again. If that second automation goes off, the first one will not re-trigger as some of the conditions are no longer met.

I was trying to put as much into a single automation as possible, but you end up with a mess including excessive scripting in the automation’s action. Sometimes it’s easier to use multiple. simple automations.