Automation with different triggers and different actions

I am new to automations using YAML so I thought someone could get me going on the following. I want to have a single automation that has 2 triggers with 2 different actions. It will allow me to control a light with a single automation instead of one to turn it on and then another to turn if off. The following is what I am trying to implement.

If 4:00AM then
Turn Light On
else if Sunrise then
Turn Light Off

I am sure this is a simple thing but if anyone can show me typical YAML code for this it would be appreciated.

Thanks

Please try this.

alias: Light routines
description: ''
mode: single
trigger:
  - platform: time
    at: '04:00:00'
  - platform: sun
    event: sunrise
condition: []
action:
  - choose:
      - conditions:
          - condition: time
            after: '03:59:00'
            before: '04:01:00'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.light
    default:
      - service: switch.turn_off
        target:
          entity_id: switch.light

Please change the switch.light to your entity name.

1 Like

Thanks so much!! Exactly what I needed.

1 Like

you could also do it with a service template:

alias: Light routines
description: ''
mode: single
trigger:
  - platform: time
    at: '04:00:00'
  - platform: sun
    event: sunrise
condition: []
action:
  - service: >
      {% if now().hour == 4 %}
        switch.turn_on
      {% else %}
        switch.turn_off
      {% endif %}
    target:
      entity_id: switch.light

Thanks, this helps me understand YAML better.

It might also be slightly better / more easily maintainable, if you use trigger.platform to determine which trigger fired. E.g.

alias: Light routines
description: ''
mode: single
trigger:
  - platform: time
    at: '04:00:00'
  - platform: sun
    event: sunrise
condition: []
action:
  - service: "switch.turn_{{ 'on' if trigger.platform == 'time' else 'off' }}"
    target:
      entity_id: switch.light

Now, if you decide to change the turn on time, you’ll only have to make the change in one place (i.e., the trigger.)

Thanks, that does look easy to maintain.

The automation suggestions I got here were great and worked fine. I have another question related to the same triggering but using state triggers instead of time or sun. I am trying to turn on a switch when a contact sensor detects a closed contact and then turn off the switch when the contact sensor detects an open. I am not sure how to setup 2 state triggers to detect open to close and then closed to open. Any examples would be greatly appreciated.

If sensor = closed then
Switch = On
else if sensor = open then
Switch = Off

Thanks

Is it a sensor or a binary_sensor? What is the sensor’s state when the contact is closed, and what’s its state when the contact is open? I need to know what the real states of the entity are (as shown on in the STATES tab of the Developer Tools page.)

I am sorry, the sensor is a binary sensor with states of “off” and “on”. Contacts open is “off” and contacts closed is “on”.

Ok. That’s pretty easy:

- trigger:
    - platform: state
      entity_id: binary_sensor.CONTACT
      to:
        - 'on'
        - 'off'
  action:
    - service: "switch.turn_{{ trigger.to_state.state }}"
      entity_id: switch.SWITCH
1 Like

Thanks!! I am learning alot from this forum.

is there any way to change the entity.id in a template for different conditions? so when a switch is hit the template should change the state of two different lights depending on the time of the day?