Break automation when button was pressed

Hi there,
I would like to build an all off automation. When I press a button, the routine should start, then wait 10 seconds. If the button is pressed longer or pressed again during these 10 seconds, the routine should be aborted. If the button is not pressed within these 10 seconds, the routine should continue. What is the best way to do this?

Thx.

I think one way to achieve this is by using a helper (in the example input_boolean.start_and_abort).
Disclaimer, this is a quick theoretical PoC that I didn’t test. See it as an inspiration to achieve your goal.

What it does:

  1. When the button is pressed, it will wait 10 seconds for you to abort, before it continues. It will turn off all lights of the helper input_boolean.start_and_abort is turned off
  2. If the button is pressed the second time (while the automation is still running), and so the automation is running twice, it will turn on the helper. With the helper on, the wait condition is matched and it will continue. Since it doesn’t match the condition ( input_boolean.start_and_abort = off) to turn the lights off the lights, it does nothing in the first run.
  3. So the first run of the automation will finish without turning off the lights
    The second run will not turn the lights off either, cause the helper still states on, but since the value of current now is 1 (since the automation is running only once now) it will go to the sequence that says input_boolean.turn_off.
alias: Start and abort
description: ''
trigger:
  - device_id: <ID of your button, but basically could be any trigger>
    domain: deconz
    platform: device
    type: remote_button_short_press
    subtype: turn_on
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ state_attr("automation.start_and_abort","current") > 1 }}'
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.start_and_abort
    default: []
  - wait_template: '{{ is_state("input_boolean.start_and_abort","on") }}'
    timeout: '0:00:10'
    continue_on_timeout: true
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.start_and_abort
            state: 'off'
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.all
      - conditions:
          - condition: template
            value_template: '{{ is_state_attr("automation.start_and_abort","current",1) }}'
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.start_and_abort
    default: []
mode: parallel
max: 2

In the following automation, when the input_button is pressed, the automation waits 10 seconds and then performs the assigned action (in this example it simply sends a notification). If the input_button is pressed again during the 10-second delay, the automation is restarted thereby canceling the delay that was in progress (because mode is restart).

alias: button test
mode: restart
trigger:
  - platform: state
    entity_id: input_button.test
action:
  - choose:
      - conditions: "{{ (now() - trigger.from_state.state | as_datetime | as_local).total_seconds() >= 10 }}"
        sequence:
          - delay: '00:00:10'
          - service: notify.persistent_notification
            data:
              title: '{{ now().timestamp()|timestamp_custom() }}'
              message: 'Ten seconds expired. Performing assigned action.'
    default: []

Interesting.
Reading the code I got stuck, so I copied the code and did a test run.
Unfortunately without success. If you tested it successfully, I probably do something wrong.

I am curious if @herbert1910 comes back to his question :innocent:
Anyway, he has some food for thought

I tested it, successfully, before posting it. However, I am open to the possibility that there’s some aspect I failed to test. Which specific test failed to work for you?

FWIW, I suspect your example is probably closer to what herbert1910 wants, namely the “button” is a physical button on a remote-control whose activity is reported via an event.

I triggered the automation but nothing happened.
But as I said, if you tested it, I probably did something wrong.

It was just our of curiosity. Busy times at the moment unfortunately, so no time to put a serious effort into it.

Hi,
sorry for the late reply.
I have now implemented it via the service automation_turn_off / turn_on.

In the first automation I set a wait of 10 seconds.
Then I built another automation. which, after pressing the off button, switches the automation off and 2 seconds later back to on.

This seems to be working so far.
Maybe there is a better way :wink:

Possibly because if it’s a new input_button, on the very first press it has no previous state value (i.e. no datetime representing the last time it was pressed). The automation’s calculation needs this information and if it doesn’t exist, nothing will happen.

Programmatically turning automations on/off is an anti-pattern (not recommended design practice). Why? Because you lose some control, namely the ability to manually enable/disable an automation (it might get turned back on/off). Neither of the two examples posted above use that technique.