Syncing sensors and switches

I have several lights that I want to turn on when a door opens and turn off when the door closes. I have binary sensors on the doors and smart switches for the lights. I know I can create two automations, one to turn the lights on when the door opens and the other to turn it off when the door closes. But, is there a simpler way to do this? Some way to just tell HA if the door is open the light should be on and if the door is closed the light should be off?

Something like this?

trigger:
  - platform: state
    entity_id: binary_sensor.door
condition: []
action:
  - service: >-
      light.turn_{{'on' if trigger.to_state.state == 'on' else 'off' }}
    target:
      entity_id: light.your_light

I tried this code, but it doesn’t work. Here’s the exact code I have put in:

- id: '1630214021631'
  alias: Mud Room Laundry - Turn light on with door open
  description: ''
  trigger:
    - platform: state
      entity_id: binary_sensor.mud_room_laundry_door
  condition: []
  action:
    - service: >-
        light.turn_{{'on' if trigger.to_state.state == 'on' else 'off' }}
      target:
        entity_id: switch.main_floor_laundry_light
  mode: single
- id: '1630214021631'
  alias: Mud Room Laundry - Turn light on with door open
  description: ''
  trigger:
    - platform: state
      entity_id: binary_sensor.mud_room_laundry_door
  condition: []
  action:
    - service: >-
        switch.turn_{{'on' if trigger.to_state.state == 'on' else 'off' }}
      target:
        entity_id: switch.main_floor_laundry_light
  mode: single

Your entity_id is not a light but a switch. Change it like the above.

THANKS! That worked! I need to slow down an read the code a little closer. Or not try to do this while sippin bourbon :slight_smile: