Problems with script template to launch TV shows on Android TV through ADB commands

Hello there!
I’m having trouble adapting the Chromecast Radio with station and player selection code (to be exact, I followed the tips to split the code from this reply) to use it with ADB and Netflix. The idea is to have two input selects (one for the TV Show and the other for the Android TV streaming device) that are the sources to select the entity_id and command in the script.

The script works fine without the template (i.e., hard-coding entity_id to one device and command to execute an ADB string), otherwise, when I execute the script, it does nothing (no error in the log, nothing).

At first I tried to debug it with the Developer tools: the Model section returned the right results (apart from the “>” character after entity_id and command), but trying to call the Service returned different errors (I later discovered that you cannot use the Service tab with templates…?).

Is there someone who can spot the problem? I thought that it might be feasible to create many different scripts for each pair (TV Show + Streaming device), and filter them with the input selects, but it will result in a lot of (messy) code.
Another “solution” that I thought of is to use Node-RED, but I wanted to try to make it “natively”.

input_select:
  tv_program:
    name: 'Select TV Show:'
    options:
      - '24'
      - 'Disenchantment'
    initial: '24'
    icon: mdi:filmstrip-box-multiple

  receiver:
    name: 'Select receiver:'
    options:
      - Google TV
      - NVIDIA Shield TV
    initial: Google TV
    icon: mdi:television-classic

script:
  tvshow:
    alias: Play TV show
    sequence:
      service: androidtv.adb_command
      data_template:
        entity_id: >
          {%- if is_state("input_select.receiver", "Google TV") %} media_player.google_tv
          {%- elif is_state("input_select.receiver", "NVIDIA Shield TV") %} media_player.nvidia_shield_tv
          {%- else %} media_player.google_tv
          {% endif %}
        command: >
          {%- if is_state("input_select.tv_program", "24") %} 'am start -n com.netflix.ninja/.MainActivity -a android.intent.action.VIEW -f 0x10000020 -e "amzn_deeplink_data" "70133008"'
          {%- elif is_state("input_select.tv_program", "Disenchantment") %} 'am start -n com.netflix.ninja/.MainActivity -a android.intent.action.VIEW -f 0x10000020 -e "amzn_deeplink_data" "80095697"'
          {% endif %}
1 Like

Does it actually contain two instances of data_template or is that a copy-paste error?

Sorry, copy and paste error! :sweat_smile:

I don’t have the means to test it but try this version and see if works (or not):

script:
  tvshow:
    alias: Play TV show
    sequence:
      service: androidtv.adb_command
      data:
        entity_id: "media_player.{{states('input_select.receiver').replace(' ', '_')|lower}}"
        command: >
        {% set shows = {'24': '70133008', 'Disenchantment': '80095697' } %}
        am start -n com.netflix.ninja/.MainActivity -a android.intent.action.VIEW -f 0x10000020 -e "amzn_deeplink_data" "{{shows[states('input_select.tv_program')]}}"

NOTE

I noticed that it’s called

     - 'Disenchantment'

in the input_select but your template refers to it as:

"Disincanto"

I don’t know if that’s in your actual code or just something you may have overlooked when translating your code into English for posting in this forum.

Still a typo, I translated the strings from Italian when I pasted the code.
Thanks anyway!

In that case, use this version:

script:
  tvshow:
    alias: Play TV show
    sequence:
      service: androidtv.adb_command
      data:
        entity_id: "media_player.{{states('input_select.receiver').replace(' ', '_')|lower}}"
        command: >
        {% set shows = {'24': '70133008', 'Disincanto': '80095697' } %}
        am start -n com.netflix.ninja/.MainActivity -a android.intent.action.VIEW -f 0x10000020 -e "amzn_deeplink_data" "{{shows[states('input_select.tv_program')]}}"
1 Like

I’ll try your code later in the evening, thanks!

Thanks a million, it works! :partying_face:

1 Like