Shared Switch Configuration Question

I have a HDMI switch which needs to be turned on when I’m watching TV in the living room or bedroom, I’ve got what i need working perfectly, however I believe it may be an ugly solution as I’ve broke it down into 5 parts.

Right now I’ve got rules for:
Switch on Living Room TV (Switches on The HDMI Switch)
Switch off Living Room TV
Switch on Bedroom TV (Switches on the HDMI Switch)
Switch off Bedroom TV
Switch off HDMI Switch (Delays 5 seconds, checks to make sure both Living Room TV and Bedroom TV is off and then turns off)

For each of these rules I’m using a key press (Its from a Flirc).

I’m about to continue automating a lot of what happens in the house and therefore wanted to run this first bit of configuration by the community to make sure i’m not writing ugly rules going forward!

 - id: '1548640452965'
  alias: Toggle TV Stack Bedroom On
  trigger:
  - event_data:
      key_code: 59
    event_type: keyboard_remote_command_received
    platform: event
  condition:
  - condition: state
    entity_id: switch.30384467cc50e315c79d
    state: 'off'
  action:
  - data:
      entity_id: switch.30384467cc50e315c79d
    service: homeassistant.turn_on
  - data:
      entity_id: switch.30384467840d8e956996
    service: homeassistant.turn_on
- id: '1548640452964'
  alias: Toggle TV Stack LivingRoom On
  trigger:
  - event_data:
      key_code: 62
    event_type: keyboard_remote_command_received
    platform: event
  condition:
  - condition: state
    entity_id: switch.30384467840d8e9578cd
    state: 'off'
  action:
  - data:
      entity_id: switch.30384467840d8e9578cd
    service: homeassistant.turn_on
  - data:
      entity_id: switch.30384467840d8e956996
    service: homeassistant.turn_on
- id: '1548640452963'
  alias: Toggle TV Stack LivingRoom Off
  trigger:
  - event_data:
      key_code: 62
    event_type: keyboard_remote_command_received
    platform: event
  condition:
  - condition: state
    entity_id: switch.30384467840d8e9578cd
    state: 'on'
  action:
  - data:
      entity_id: switch.30384467840d8e9578cd
    service: homeassistant.turn_off
- id: '1548640452965'
  alias: Toggle TV Stack Bedroom Off
  trigger:
  - event_data:
      key_code: 59
    event_type: keyboard_remote_command_received
    platform: event
  condition:
  - condition: state
    entity_id: switch.30384467cc50e315c79d
    state: 'on'
  action:
  - data:
      entity_id: switch.30384467cc50e315c79d
    service: homeassistant.turn_off
- id: '1548651296387'
  alias: keyboard log
  trigger:
  - event_data: {}
    event_type: keyboard_remote_command_received
    platform: event
  condition: []
  action:
  - data_template:
      message: Key Pressed {{ trigger.event.data.key_code }}
    service: notify.filenotify
- id: '1548656624166'
  alias: Toggle TV Stack HDMI off
  trigger:
  - event_data:
      key_code: 59
    event_type: keyboard_remote_command_received
    platform: event
  - event_data:
      key_code: 62
    event_type: keyboard_remote_command_received
    platform: event
  condition: []
  action:
  - delay: 00:00:05
  - condition: state
    entity_id: switch.30384467cc50e315c79d
    state: 'off'
  - condition: state
    entity_id: switch.30384467840d8e9578cd
    state: 'off'
  - data:
      entity_id: switch.30384467840d8e956996
    service: homeassistant.turn_off

I must admit that I haven’t looked through your code snippet, but I do have one recommendation.

If you’re going to be writing a lot of repetitive automations that only have a few parameters or entities change out between them, you might want to take a look at AppDaemon if you’re even vaguely familiar with python. You can write up the automation as a template then with very few lines of code just instantiate it with the individual variables it needs. I can give an example if you want, but I don’t want to go too offtopic. It’s saved me from writing many hundreds of lines of repetitive automation code recently.

Thank you for the advice. I did look at some python scripts but shyed away from it thinking it wasn’t the done thing!

I don’t program but and edit and splice together.

One use case for me was a semi-complicated motion detection automation. The typical motion triggered lights, but also with multiple motion detectors, multiple lights, rules of when their allowed to trigger, how long to stay on, re-triggering, motion lockout if manually turned off, etc. It was a 200-300 line automation for each room. Appdaemon turned that into 60 lines of python, with ~10 lines of parameters for each room that got automated.

The documentation is a dry read, but helpful, and the example code page is a great starting point to get an idea of the flow.