Make device name in script a variable

Its a foolow up question to this discussion about implementing a remote control in HA.

I am working on my implementation of it. I use a Logitech Harmony Hub.

Sending a command is documented here and looks like this:

tv_ch_up:
  alias: TV Ch Up
  sequence:
  - service: remote.send_command
    data:
      command:
      - ChannelUp
      device: Sony-Fernseher
      delay_secs: 0.4
    entity_id: remote.harmonyhub
  mode: single

The issue I have is this: I need to specify the device (TV, Soundbar, PS4, Kodi, …). The command would stay the same. But that would mean a lot of duplicated Scripts.

Is there a way make the device-name in the code above a variable which depends on the activity status of the harmony hub? I know the activity, and have switches with also indicate the activity:

  - platform: template
    switches:        
       #Fernsehen
        fernsehen:
          value_template: "{{ is_state_attr('remote.harmonyhub', 'current_activity', 'Fernsehen') }}"
          turn_on:
            service: remote.turn_on
            data:
              entity_id: remote.harmonyhub
              activity: 'Fernsehen'
          turn_off:
            service: remote.turn_on
            data:
              entity_id: remote.harmonyhub
              activity: 'PowerOff'

So i have easy ways to make if loops to set the variable. But I actually dont know if its possible or how.
Possibly somehow with “helpers” and templates?
I would be very thankful for some help, links or example code.

Thanks a lot!

You can do this with a variable, like this:

alias: TV Ch Up
  variables:
    current_activity: "{{ state_attr('remote.harmonyhub', 'current_activity') }}"
    device_name: >
      {% set map = {
        'Fernsehen': 'Sony-Fernseher',
        'Musik': 'Stereoanlage',
        'activity name': 'device for this activity',
      }%}
      {{ map[current_activity] }}
  sequence:
  - service: remote.send_command
    data:
      command:
      - ChannelUp
      device: "{{ device_name }}"
      delay_secs: 0.4
    entity_id: remote.harmonyhub
  mode: single

In this part:

     {% set map = {
        'Fernsehen': 'Sony-Fernseher',
        'Musik': 'Stereoanlage',
        'activity name': 'device for this activity',
      }%}

Add your activities and the corresponding device to be used with this activity.