Simplify 4 automations into 1

I have two Wemo smart plugs that control lights under my kitchen cabinets. I have them grouped in HA so that one toggle controls both, but occasionally guests, or my wife will push the switch on the Wemo to control the lights.

I have created 4 automations that turn on/off the other switch when one is pressed. But I’d love to put them all into one.
Screenshot 2023-11-02 at 13.50.27

Here’s the YAML for one of the automations.

alias: Turn off underlight L when Underlight R turned off
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.kitchen_underlight_l
      - switch.kitchen_underlight_r
    from: "on"
    to: "off"
condition:
  - condition: state
    entity_id: switch.kitchen_underlight_l
    state: "on"
action:
  - service: switch.turn_off
    data: {}
    target:
      device_id: ad622e8c82b30bec924b25cd5f5c289c
mode: single

How can I get one automation to do this instead of four separate?

Have you tried four separate triggers with trigger ids, then a “choose” action based on which trigger fired?

This is a great case to use templates and data from the trigger. Something like this (I typed this freehand, so there might be a syntax error or other mistake, but it should point the way).

alias: Turn off/on underlight when other changes
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.kitchen_underlight_l
      - switch.kitchen_underlight_r
action:
  - service: switch.turn_{{ trigger.to_state.state }}
    data: {}
    target:
      entity_id: >-
        {% if trigger.entity_id == 'switch.kitchen_underlight_l' %}
          switch.kitchen_underlight_r
        {% else %}
          switch.kitchen_underlight_l
        {% endif %}
mode: single

Hadn’t played around with that yet. Looked at the docs for trigger IDs and it was super simple! Thanks for the suggestion. This is what I have now and it works great.

alias: Under Cabinet Lights
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.kitchen_underlight_l
    id: left.on
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - switch.kitchen_underlight_l
    id: left.off
    from: "on"
    to: "off"
  - platform: state
    entity_id:
      - switch.kitchen_underlight_r
    id: right.on
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - switch.kitchen_underlight_r
    id: right.off
    from: "on"
    to: "off"
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - left.on
          - condition: state
            entity_id: switch.kitchen_underlight_r
            state: "off"
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.kitchen_underlight_r
      - conditions:
          - condition: trigger
            id:
              - left.off
          - condition: state
            entity_id: switch.kitchen_underlight_r
            state: "on"
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.kitchen_underlight_r
      - conditions:
          - condition: trigger
            id:
              - right.on
          - condition: state
            entity_id: switch.kitchen_underlight_l
            state: "off"
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.kitchen_underlight_l
      - conditions:
          - condition: trigger
            id:
              - right.off
          - condition: state
            entity_id: switch.kitchen_underlight_l
            state: "on"
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.kitchen_underlight_l
mode: single