Reusable triggers and conditions

I have a few trigger and conditions that i copy and paste a few times,

- alias: turn on room x lights after sunset
  trigger:
  - platform: sun
    event: sunset
    offset: "-00:15:00"
  - platform: state
    entity_id: group.residents
    state: 'home'
  condition:
    condition: and
    conditions:
    - condition: time
      before: '22:55:00'
    - condition: and
      conditions:
      - condition: sun
        after: sunset
        after_offset: "-00:15:00"
      - condition: or
        conditions:
        - condition: state
          entity_id: input_boolean.other_on_vacation
          state: 'on'
        - condition: state
          entity_id: group.residents
          state: 'home'

Now this is error prone, if i find a bug i have to fix it in all the places where i used it!

So my idea is to turn this into something like this

reuable_trigger:
  sunset_lights_on:
    trigger:
    - platform: sun
      event: sunset
      offset: "-00:15:00"
    - platform: state
      entity_id: group.residents
      state: 'home'
    condition:
      condition: and
      conditions:
      - condition: time
        before: '22:55:00'
      - condition: and
        conditions:
        - condition: sun
          after: sunset
          after_offset: "-00:15:00"
        - condition: or
          conditions:
          - condition: state
            entity_id: input_boolean.other_on_vacation
            state: 'on'
          - condition: state
            entity_id: group.residents
            state: 'home'

That then could be used like this

- alias: turn on room x lights after sunset
  trigger:
  - platform: reuable
    entity_id: reuable_trigger.sunset_lights_on

Is there any way to do this currently, if not it would be nice if it was added!

Scripts support conditions, so you can create a script that does most of it and just call that script. Since you also can pass variables to a script, you can customize for instance entities and other variable parameters.

You could setup your trigger & conditions to turn on/off an input_boolean.

The input_boolean can be used just like you want to use the “reusable” trigger.

- alias: turn on room x lights after sunset
  trigger:
  - platform: state
    entity_id: input_boolean.residents_home_after_sunset
    state: 'on'
  action:
    [ do something ]
1 Like