Sanity check and questions about my contact closed automations

I have a gate with a contact sensor installed on it. When the gate is open, it triggers a smart bulb to turn on red. When the gate is closed it checks if the bulb is supposed to be on, it changes to color to the normal color and if the bulb is supposed to be off, it turns the bulb off. The bulb is scheduled to turn on at 6pm and turn off at midnight.

I had to make two separate automations for when the gate is closed to account for whether the bulb is supposed to be on or off.

alias: Gate Closed while Lamp is Scheduled Off
description: ''
triggers:
  - trigger: door.closed
    target:
      area_id: back_yard
    options:
      behavior: each
conditions:
  - condition: schedule.is_off
    target:
      entity_id: schedule.living_room_lamp_schedule
    options:
      for: '00:00:00'
actions:
  - action: light.turn_off
    metadata: {}
    target:
      area_id: living_room
    data: {}
mode: single
alias: Gate Closed while Lamp is Scheduled On
description: ''
triggers:
  - trigger: door.closed
    target:
      area_id: back_yard
    options:
      behavior: each
conditions:
  - condition: schedule.is_on
    target:
      entity_id: schedule.living_room_lamp_schedule
    options:
      for: '00:00:00'
actions:
  - action: light.turn_on
    metadata: {}
    target:
      area_id: living_room
    data:
      rgb_color:
        - 255
        - 255
        - 255
      color_temp_kelvin: 5500
mode: single

First question is can I consolidate these two automations into one automation (if/then + elseif/then)?
If yes, should I consolidate the automations (best practice)?

Yes, these would be reasonable candidates for combining.

The best practice is to build automations that you understand. Beyond that it’s mostly personal preference.


There are a number of previous threads that cover this topic with a variety of examples of how different users approach combining automations:

Thanks for answer and the additional info.

An update for anyone trying to use the yaml, rgb_colorand color_temp_kelvin cannot be used at the same time (the gui currently allows both to be set but the automation still error out). Apparently color_temp_kelvin does set the bulb to “white” at that color temp.