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?
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:
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
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.
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.
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).
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
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.
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
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.