Hi, @wizbang - I suggest using timers (I avoid delays at all costs for the reason you stated). I use these all the time. In an automation, I start the timer. Then I have a second automation that handles any actions when the timer finishes.
Timers are event-based (their state is active
, paused
, or idle
). I don’t know what switch you are using or what it responds with relative to a short- or long-press, so that would have to be put into the automation. Here’s what I do with the event-based Hue dimmer switches (they have four buttons and can respond to a long- or short-press, too):
trigger:
- platform: event
event_type: hue_event
event_data:
id: hue_dimmer_switch_1
event: 1002 # This is Hues event code for a short press on button 1
mode: parallel
action:
- service: system_log.write
data_template:
message: >
Action triggered by {{trigger.event.data.id}} button {{trigger.event.data.event}}
level: warning
- service: light.turn_on
entity_id: light.xxx
- service: timer.start
entity_id: timer.switch_on_timer
For this switch, I know that the first number in the trigger.event.data.event
is the button number (1 through 4) and the last number is a 2 for a short press up and a 3 for a long press up.
Armed with that, the action section turns on the light and starts a timer when there’s a short-press. Every time it’s short-pressed, the automation will trigger and the timer will (re)start (which I think gets you to your first question).
Then you need an automation that triggers when the timer finishes to turn off the light:
- alias: Expired timer manager
trigger:
- platform: event
event_type: timer.finished
event_data:
entity_id: timer.switch_on_timer
action:
- service: light.turn_off
entity_id: light.xxx
For the second scenario (a short press followed by a long-press within the 30 minutes), you will need another automation to CANCEL the timer (not finish, otherwise it will turn off the light). Here’s what that would look like for the Hue dimmer switch:
trigger:
- platform: event
event_type: hue_event
event_data:
id: hue_dimmer_switch_1
event: 1003 # This is Hues event code for a LONG press on button 1
mode: parallel
condition:
condition: state
entity_id: timer.switch_on_timer
state: 'active'
action:
- service: system_log.write
data_template:
message: >
Action triggered by {{trigger.event.data.id}} button {{trigger.event.data.event}}
level: warning
- service: timer.cancel
entity_id: timer.xxx
Note that I added a condition
section so this will only execute if the timer is active. If you always want the light to come on indefinitely with a long-press, just delete the condition
section and I’d add a service to the action
section to turn on the light (in case it’s not already on).
Let me know if I misunderstood what you are after!