Suggestions on handling overlapping automations

Hi,

I thought I’d ask the community before diving into trying to solve this myself :slight_smile:
I am still fairly new to HA and fast coming up to speed with the many ways there are to do the same thing.

I want to do the following with a light switch:

  • Short press: Turn on lights for 30mins
  • Long press: Turn on lights indefinitely i.e. normal lights_on

How would I handle the following use cases?

  • Short press followed by another short press within the first short press 30min window extends light_on for another 30mins
  • Short press followed by a long press within the first short press 30min window turns the lights on indefinitely

I’m concerned about how I cancel the first automation and replace it with the second.
If I use a delay of 30mins in the short press automation how do I cancel it?
If I don’t use a delay what other approach is there?
Instead of having a long running automation (using a delay etc.) is there some other method to turn on the light and queue a separate job to turn it off 30mins later?

Any thoughts or feedback would be really welcome.

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!

Hi @KSC,
Thank you. That is exactly what I was looking for.
This gives me the ability to control my lights in predictable way.

I am using a Hue Dimmer switch, but made the decision this week to move away from the Hue Hub and switch to ZHA for controlling the switches and motion sensors. I was fed up with the 5+ second delay the motion sensors have in informing HA they have detected an occupant.

I am assuming that the entity_id used when starting the timer is a string and can be timer.<anything>?

I really appreciate your quick and detailed response. I plan to try out a few simple things this evening before converting the switches over to timers this weekend :slight_smile:

1 Like

That is a total pain!! I did start using a custom_component fasthue that lets you adjust the 5 seconds to about anything that is an integer (but not zero). Of course, the downside is higher battery use - I’m waiting to see how big of an impact that is. But even at a 2 sec delay some family members are still (understandably) annoyed… (if you’re interested in the meantime, here’s the link to it within the HA community: Custom integration to set polling interval for Friends of Hue devices - Configuration / Zigbee - Home Assistant Community (home-assistant.io)

Exactly - but you have to set up the timer, first. I think I forgot to mention that. Just add this to your configuration.yaml, and then reload timers from the configuration/server controls panel (creating the timer name you want - these are three that I use):

timer:
  mbr_vaporizer_startup_timer:
    duration: '00:00:20'
  main_vaporizer_startup_timer:
    duration: '00:00:20'
  test_timer:
    duration: '00:00:01'

Sounds like a fun Friday evening! :grinning:

Hi KSC,

So I’ve got a simple re-triggering of my motion sensed lights working this evening thanks to your help.

I also found the timer integration documentation, so figured out the need for timers to be added to the configuration.yaml file. Do you know if there are any drawbacks to having lots of timers e.g. >30
I am assuming that they consume resources, but do not have any impact on the system unless they are in use.

Here is my first attempt at making my hall motion sensor re-trigger and reset the timer if the light is already on, but it detects further motion i.e. keep the light on until the occupant moves out of the hall.
I also choose a different brightness level if its the middle of the night :slight_smile:

- id: '1610751939413'
  alias: Hall Light - Motion Response
  description: Turn on hall light when dark and motion is sensed
  trigger:
  - platform: state
    entity_id: binary_sensor.philips_sml001_hall_occupancy
    from: 'off'
    to: 'on'
  condition: []
  action:
  - choose:
    - conditions:
      - condition: state
        entity_id: light.hall_light
        state: 'on'
      - condition: state
        entity_id: timer.hall_light_motion
        state: active
      sequence:
      - service: timer.start
        data: {}
        entity_id: timer.hall_light_motion
    - conditions:
      - condition: numeric_state
        entity_id: sensor.philips_sml001_hall_illuminance
        below: '5'
      - condition: time
        after: '23:00'
        before: '06:00'
      sequence:
      - service: light.turn_on
        data:
          brightness_pct: 5
        entity_id: light.hall_light
      - service: timer.start
        data: {}
        entity_id: timer.hall_light_motion
    - conditions:
      - condition: numeric_state
        entity_id: sensor.philips_sml001_hall_illuminance
        below: '5'
      sequence:
      - service: light.turn_on
        data:
          brightness_pct: 75
        entity_id: light.hall_light
      - service: timer.start
        data: {}
        entity_id: timer.hall_light_motion
    default: []
  mode: queued
  max: 5

and the timer finished event also restarts the timer if there is still an occupant in the hall:

- id: '1610748665403'
  alias: Hall Light - Timer Expire
  description: What to do when the hall light timer expires
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.hall_light_motion
  condition: []
  action:
  - choose:
    - conditions:
      - condition: state
        entity_id: binary_sensor.philips_sml001_hall_occupancy
        state: 'on'
      sequence:
      - service: timer.start
        data: {}
        entity_id: timer.hall_light_motion
    default:
    - service: light.turn_off
      data: {}
      entity_id: light.hall_light
  mode: single
1 Like

I haven’t run into a limit yet, and I use timers – a lot. They typically don’t run that long (a few minutes at most) but I do have some that run for a couple hours – I haven’t run into any issues yet, and don’t expect to.

And I like your use of the choose feature - that looks great!

Glad it’s working for you!