Automation: add action before trigger

HI,

have this automation:

party_on:

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

party_off:

  - alias: 'Turn Party Mode Off'
    id: 'Turn Party Mode Off'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_boolean.home_mode_party
      to: 'off'
    action:
      service: light.turn_off
      entity_id: 
        - 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?
thx,
Marius

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:
      condition: state
      entity_id: input_boolean.home_mode_party
      state: 'off'

Sebastian

1 Like

ah, so simple

thanks!

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:
      - condition: and
        conditions:
        - condition: state
          entity_id: input_boolean.home_mode_party
          state: off
        - condition: state
          entity_id: ...
          state: ...
        - condition: ...

Sebastian

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.

switch.party
turn_on:
turn-off motion sensors
turn-off automation
turn-on lights script
turn_off:
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.

Sebastian

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:

switch:
  - platform: template
    switches:
      mode_party:
        friendly_name: Party mode switch
        value_template: >
          {{ is_state('input_select.mode', 'Party') }}
        turn_on:
          - service: script.party_on
          - service: script.party_on_2
          - service: script.party_on_3        
        turn_off:
          - 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: