Automation: add action before trigger


have this automation:


  - alias: 'Turn Party Mode On'
    id: 'Turn Party Mode On'
    initial_state: 'on'
      platform: time
      seconds: '/3'
      condition: state
      entity_id: input_boolean.home_mode_party
      state: 'on'
      - service: light.turn_on
          - light.buffet
          - light.kist
          - light.home_theater
          rgb_color: ['{{ (range(0, 255)|random) }}', '{{ (range(0, 255)|random) }}', '{{ (range(0, 255)|random) }}']
          brightness: 200


  - alias: 'Turn Party Mode Off'
    id: 'Turn Party Mode Off'
    initial_state: 'on'
      platform: state
      entity_id: input_boolean.home_mode_party
      to: 'off'
      service: light.turn_off
        - light.buffet
        - light.kist
        - light.home_theater

which is working fine (though id love to create just one on/off but thats another challenge.
the thing is, this is driving my hassio/Hue integrations into timing issues, since I also have an automation.sense_lights_change that listens to the state of all lights, and runs 2 python scripts when a state-change has been detected.

Id like to disable this automation when party mode is set to On, and re-enable it when party mode is Off again.

Since the party mode automation is triggered as you can see above, it runs each 3 seconds, conditioned by the boolean being on, i’m a bit puzzled how to add the homeassistant.turn_off: automation.sense_lights_change action to it… If I simply add it to the list of actions, it would get executed each 3 seconds…
Id need to add the action before the current trigger :wink:

What would be the best way of changing this setup, maybe create an intermediary script? boolean flipped, automation_sens_lights flipped, and then party on, an vice versa? would also mean the condition could be taken out here?

please have a look?

If you don’t want to go for example the AppDaemon way, then you could try the timer component:

Party_on starts a 3 sec. timer (and disables your other automation). The timer will trigger modification of your lights and restart itself if the party is still running.
Party_off reenables the other automation.

Or you could add another condition to your sense_lights_change automation so that it only fires when party mode is off, like this:

      condition: state
      entity_id: input_boolean.home_mode_party
      state: 'off'


1 Like

ah, so simple…

yes, i think thats what i was trying to establish… @sebk-666 suggested something very elegant and simple though, which will be quite easy to implement…
thanks all!

I’m not sure if you were aware that you can specifiy multiple conditions and combine them with and/or clauses (conditions).
So if you already have a condition in you sense_lights_change automation, you’d need to “and” it with the party mode check condition, e.g.:

      - condition: and
        - condition: state
          entity_id: input_boolean.home_mode_party
          state: off
        - condition: state
          entity_id: ...
          state: ...
        - condition: ...


sure, i am, cool.
but thanks for making this very clear.

i was wondering if we can write a script with the timing as in the automation above.
that way I would create a switch instead of a boolean, with turn_on and turn_off scripts, one of these containing the timed lights.
turn-off motion sensors
turn-off automation
turn-on lights script
turn-off lights
turn-on automation

etc etc

conditions ‘and’ by default. You really only need to include ‘and’ if combining it with ‘or’ to make complex / nested conditions

You’re right, thanks for reminding me :slight_smile:
I usually explicitely write the and condition clause anyway, because that way I find it easier to read - and while experimenting with clauses you only need to swap the and for an or instead of having to reindent all the code to fit a new clause.


If you exchange “lights script” with 3-sec-timer, you’re done :wink:

hmm, how exactly? :wink:

the Turn party Mode On automations would have to be turned into the script with the timer…

was thinking along the lines of this switch:

  - platform: template
        friendly_name: Party mode switch
        value_template: >
          {{ is_state('input_select.mode', 'Party') }}
          - service: script.party_on
          - service: script.party_on_2
          - service: script.party_on_3        
          - service: script.party_off
          - service: script.party_off_2
          - service: script.party_off_3

You would have to take the “action” part of your old “Turn Party Mode On” automation and bind it to the timer.finished event of a new 3-sec timer. Additionally, add timer.start to the actions.

Turn_on would now run timer.start and Turn_off would run timer.cancel.

But eventually you’ll end up with the same scenario that you have currently, just a little different behaviour in the background. I’d go the AppDaemon route and write it down the Pythonic way :wink: