Help with template!

Hey guys

I’m getting tired of copy pasting code, so i thought it was time to jump into templating. Now that have read the site documentation, i think i still need a kick start from one of you to make it happen

So here’s what i have and what i’m trying to achieve

I added the hdm_cec component and defined the devices that have been detected in my configuration.yaml file as below

hdmi_cec:
  devices:
    1:
      1: Kodi
    2: Videotron
    3: Blueray

I then tested it out with the service call within HA by settings using the following

Domain
  hdmi_cec
Service
  power_on
Service Data ( JSON, optional)
{ "device": "Blueray" }

That worked perfectly

Then i built an input select section to be able to trigger each of the devices from a drop down

input_select:
  tvsource:
    name: Television source select
    options:
      - Kodi
      - Videotron
      - Blueray
      - None
    initial: None

For which i added some automations in the automation.yaml file , which works as well

  - alias: Switch TV input1
    trigger:
      platform: state
      entity_id: input_select.tvsource
      to: "Videotron"
    action:
      service: hdmi_cec.select_device
      data: { "device": "Videotron" }
  - alias: Switch TV input2
    trigger:
      platform: state
      entity_id: input_select.tvsource
      to: "Blueray"
    action:
      service: hdmi_cec.select_device
      data: { "device": "Blueray" }
  - alias: Switch TV input3
    trigger:
      platform: state
      entity_id: input_select.tvsource
      to: "Kodi"
    action:
      service: hdmi_cec.select_device
      data: { "device": "Kodi" }

But although it’s working, it’s causing a lot of code duplication that i would like to avoid.

If one can help me out and send me in the right direction with a working example based on my config , i think i should be good to start from there for my future needs.

Thanks in advance guys