Automation with multiple triggers and different conditions for each trigger

I have lamps in my hall which are managed by multiple automations.
The lamps should come on in the morning at 07:00 if the sun isn’t up and go off at sunrise or 08:00:

alias: 05a. Hall lamps Dawn on
description: ""
mode: single
trigger:
  - platform: time
    at: "07:00"
  - platform: sun
    event: sunrise
condition:
  - condition: and
    conditions:
      - condition: sun
        before: sunrise
action:
  - type: turn_on
    device_id: 853a83b96b3285e5b532b18c086f3ee9
    entity_id: switch.hall1
    domain: switch
  - type: turn_on
    device_id: 930f864a793d011d46b01ca4366c4278
    entity_id: switch.hall2
    domain: switch
alias: 05b. Hall lamps Dawn off
description: ""
trigger:
  - platform: time
    at: "08:00"
  - platform: sun
    event: sunrise
    offset: "0"
condition: []
action:
  - type: turn_off
    device_id: 853a83b96b3285e5b532b18c086f3ee9
    entity_id: switch.hall1
    domain: switch
  - type: turn_off
    device_id: 930f864a793d011d46b01ca4366c4278
    entity_id: switch.hall2
    domain: switch
mode: single

There are also automations to turn them on again at sunset and off at 23:45.
I want to rationalise this into one single automation using trigger ids e.g.

alias: 05. Hall lamps (everything!)
description: ""
trigger:
  - platform: time
    at: "07:00:00"
    id: "on"
  - platform: time
    at: "08:00:00"
    id: "off"
  - platform: sun
    event: sunset
    offset: 0
    id: "on"
  - platform: time
    at: "23:45:00"
    id: "off"
  - service: switch.turn_{{trigger.id}}
    data: {}
    target:
      entity_id:
        - switch.hall1
        - switch.hall2
mode: single

but I can’t figure out how to apply the sunrise logic to the first two triggers. Is it possible or do I need to stick with separate automations?

Just for your information / learning there are a couple of issues I can see in what you have posted, for example:

There is only 1 condition here and therefore you dont need the ‘and’, also as conditions are ‘and’ by default you can simply list the conditions like this: (although what you have would still work and is still valid:

condition:
  - condition: state
    entity_id: "device_tracker.paulus"
    state: "home"
  - condition: numeric_state
    entity_id: "sensor.temperature"
    below: 20

Also your example of combining the automations shows:

You cant have an action / service listed under a trigger, the correct format would be something like this:

trigger:
  - platform: time
    at: '07:00:00'
    id: 'on'
  - platform: time
    at: '08:00:00'
    id: 'off'
condition: []
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: 
        - switch.hall1
        - switch.hall2

As a quick example (there are many ways to do this) I would keep it simple and do something like this maybe:

alias: 05a. Hall lamps
description: ''
mode: single
trigger:
  - platform: time
    at: '07:00:00'
    id: 'on'
  - platform: time
    at: '08:00:00'
    id: 'off'
  - platform: state
    entity_id:
      - sun.sun
    id: 'off'
    to: above_horizon
  - platform: state
    entity_id:
      - sun.sun
    id: 'on'
    to: below_horizon
  - platform: time
    at: '23:45:00'
    id: 'off'
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: 'on'
          - condition: state
            entity_id: sun.sun
            state: below_horizon
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id:
                - switch.hall1
                - switch.hall2
      - conditions:
          - condition: trigger
            id: 'off'
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id:
                - switch.hall1
                - switch.hall2
    default: []

Thanks. The missing “condition/action” was down to a copy and paste error.
That’s a really tidy solution. I have learnt a lot :slight_smile:

1 Like