Scripts need a "finally" sequence block

Scripts that have delays can be interrupted, but there’s no way to specify a final sequence of steps that are guaranteed to execute upon completion.

For example, the below doesn’t set the variable back to enabled unless the script runs to completion.

disable_camect_events_for_15_min:
  sequence:
    - service: vantage.set_variable_vid
      data:
        vid: 7237
        value: 0  # disable
    - delay: '0:15'
    - service: vantage.set_variable_vid
      data:
        vid: 7237
        value: 1  # re-enable

Ideally, one could write (in !included scripts.yaml):

disable_camect_events_for_15_min:
  sequence:
    - service: vantage.set_variable_vid
      data:
        vid: 7237
        value: 0  # disable
    - delay: '0:15'
  finally:
    - service: vantage.set_variable_vid
      data:
        vid: 7237
        value: 1  # re-enable

And the sequence of steps in the finally block would execute always, even if the script is terminated mid-execution.

Came here for an “Exception” or “Cleanup” block – but this fits my need. A use case we have is:

  • Disable electric fence
  • Wait 30 minutes
  • Enable Electric fence

This is connected to a button. As it is, I can’t manually re-enable the fence from the same button, and if I do, it terminates the script without re-enabling the fence. It would be nice to be able to just hit the button a second time (ending the script, but running the ‘finally’ section).

2 Likes