Can same Automation run in multiple instances?

Bit of a bizarre question, but: if you have an Automation with a Delay and run it twice, do you get two instances of the automation running, or is there something preventing multiple-instances of the same automation running at the same time?

Why am I asking? See in-line comments for details, but in essence, I’m trying to over-load (in a programming sense) the function of a Wemo light-switch. Single press / click toggles the fan, Double click activates a script. I manage this assuming two instances of the Automation script running at the same time (but updating the same input_boolean). Currently not working… I see the flag toggle via mobile app, but my test light doesn’t turn_on.

My guess is that HA is overriding/stopping the First Click as soon as I initiate the Second Click, so that I’m never getting the right toggle value.

alias: Fan X2 Puppy Potty Track
initial_state: true

trigger:
  # when fan is toggled
  - platform: state
    entity_id: switch.master_bedroom_fan

action:
  # Here is the full logic flow, since this is a bit confusing...

  # GOAL:  Double-clicking a Wemo wall-switch for ceiling fan will
  #        activate a script.  Single-clicking will simply toggle
  #        the fan, not run the script.  This is a toggle, not on/off,
  #        i.e., I want a double-toggle to leave fan in original state.
  #        For testing purposes, below turns on a nearby light.

  #        Currently, I see the flag toggle, but the light doesn't come on.
  #        Perhaps HA is killing the First Click when the Second fires


  # WITH ONLY 1 PRESS WITHIN DELAY TIME:

  # -- Default state: boolswitchstate = OFF
  # -- First Click:   Initiated by switch.master_bedroom_fan
  # -- First Click:   Toggle#1: boolswitchstate = ON
  # -- First Click:   Delay starts, automation on-hold
  # -- time...
  # -- First Click:   Delay ends, automation continues
  # -- First Click:   Toggle#2: boolswitchstate = OFF
  # -- First Click:   Condition: does boolswitchstate == ON?  False
  # -- First Click:   Automation Aborts
  # -- default state restored:  boolswitchstate = OFF


  # WITH 2 PRESSES WITHIN DELAY TIME:

  # -- Default state: boolswitchstate = OFF
  # -- First Click:   Initiated by switch.master_bedroom_fan
  # -- First Click:   Toggle#1: boolswitchstate = ON
  # -- First Click:   Delay starts, automation on-hold
  # -- time...
  # -- Second Click:  Initiated by switch.master_bedroom_fan
  # -- Second Click:  Toggle#1: boolswitchstate = OFF
  # -- Second Click:  Delay starts, automation on-hold
  # -- time...
  # -- First Click:   Delay ends, automation continues
  # -- First Click:   Toggle#2: boolswitchstate = ON
  # -- First Click:   Condition: does boolswitchstate == ON?  True
  # -- First Click:   Target switch turned on
  # -- First Click:   Turn_off boolswitchstate = OFF
  # -- First Click:   Automation Complete!
  # -- time...
  # -- Second Click:  Delay ends, automation continues
  # -- Second Click:  Toggle#2: boolswitchstate = ON
  # -- Second Click:  Condition: does boolswitchstate == ON?  True
  # -- Second Click:  Target switch turned on (again, but OK)
  # -- Second Click:  Turn_off boolswitchstate = OFF (again, but OK)
  # -- Second Click:  Automation Complete!
  # -- default state restored:  boolswitchstate = OFF



  # Toggle#1:
  - service: input_boolean.toggle
    data:
      entity_id: input_boolean.boolswitchstate

  # this will eventually be 1s, but set to 5 for debugging
  - delay: '00:00:05'

  # Toggle#2:
  - service: input_boolean.toggle
    data:
      entity_id: input_boolean.boolswitchstate


  - condition: state
    entity_id: input_boolean.boolswitchstate
    state: 'on'

  # turn on other target light:
  - service: switch.turn_on
    entity_id: switch.master_bedroom_light

  # assign boolswitchstate to OFF, to restore default state
  - service: input_boolean.turn_off
    data:
      entity_id: input_boolean.boolswitchstate

I’m sure I can find another way to do this… but it raised the question: does HA kill duplicate Automations?

No, the second trigger will simply skip the automation that is currently delayed forward to the end of the action.

Personally I think this is a bug, but there are so many potential ways people might want this to work its hard to see how this can be fixed.

If you think of a simple automation with a 10 minute delay before turning on a light that I trigger at midnight, then the trigger trips again at 5 past.

Current behaviour is that the light switches on at 5 past.

But, should it switch on at 5 past, 10 past or quarter past?

If it switches on at 10 past and I switch it off again manually at 12 past, should it switch on again at quarter past?

Dunno.

Personally I think it should ignore further triggers and go on at 10 past only. Then if you know that you can work with it.

Or, it should overwrite the first trigger and go on at quarter past only. Then if you know that you can work with it.

As it stands currently, it will go on at 5 past (which is the one time that I definitely don’t want it to go on!) but again, knowing that I can work with it and take steps to code around it.

Hope this helps.

1 Like

Interesting - so in my scenario, the Second Click effectively ends the First Click delay. That explains why the flags toggle, but the condition is never true / the light never turns on.

I would prefer (and obviously expected) each automation be run as its own instance, completely asynchronous from any other instances of the same code running. That way, it’s up to the HA owner to ensure automations don’t step on themselves, or if they do, to manage accordingly.

But whatever - I don’t really care that much… like you said, now that I know the behavior I can make this work another way.

Morale of the story: don’t overlap automations :slight_smile:

Thanks!

1 Like