Sending a Harmony Hub device command?

Hi,

I’m trying to create a switch which sends a command to a device when I turn it on, and another command when I turn the switch off. I want to send a single command, not start an activity.
I cannot make it work, and pressing the switch does nothing, and the switch also goes back to off after pressing it (I want it to stay on until I press it off).
But first, why doesn’t my command send?

This is the switch and script:

switch:
  - platform: template
    switches:
      livingroom_xbox_spotify:
        value_template: "{{ is_state('script.livingroom_xbox_spotify_on', 'on')}}"
        turn_on:
          service: script.turn_on
          entity_id: script.livingroom_xbox_spotify_on
        turn_off:
          service: script.turn_on
          entity_id: script.livingroom_xbox_spotify_off

script:
  livingroom_xbox_spotify_on:
    sequence:
      - service: remote.send_command
        entity_id: remote.harmony_hub
        data:
          device: "30544319"
          command: "InputHdmi3"
  livingroom_xbox_spotify_off:
    sequence:
      - service: remote.send_command
        entity_id: remote.harmony_hub
        data:
          device: "30544319"
          command: "InputHdmi1"

Finally made it work, now it is sending a command when switch is turned on. But the switch goes back to off instantly. And I cannot send an off command. This is the script now:

switch:
  - platform: template
    switches:
      livingroom_xbox_spotify:
        value_template: "{{ is_state('script.livingroom_xbox_spotify_on', 'on')}}"
        turn_on:
          service: script.turn_on
          entity_id: script.livingroom_xbox_spotify_on
        turn_off:
          service: script.turn_on
          entity_id: script.livingroom_xbox_spotify_off

script:
  # https://home-assistant.io/components/script/
  livingroom_xbox_spotify_on:
    sequence:
      - service: remote.send_command
        data_template:
          entity_id: remote.harmony_hub
          device: "30544319"
          command: "InputHdmi3"
  livingroom_xbox_spotify_off:
    sequence:
      - service: remote.send_command
        data_template:
          entity_id: remote.harmony_hub
          device: "30544319"
          command: "InputHdmi1"

I believe when a script is called, it doesn’t stay “On”, so when you are trying to use the state of the script, it turns on when running and then turns off again immediately after. You should be able to use the current activity of the harmony hub. {{ is_state(‘remote.harmony_hub’, ‘xbox’)}}. You will need to adjust ‘xbox’ to be the name of your activity of course.

Or if it is not set up as an activity, I would create an input_boolean and turn the boolean on with the “livingroom_xbox_spotify_on” script and off with the other script and then use the state of the boolean for the switch.