Android TV script on webhook automation to change YouTube playback speeed

I feel I’m so close, but not quite… I want to automate speeding up or slowing down a YouTube clip playing on my Google TV.
On the TV remote this takes a lot of button presses, so… let’s automate it.
I have a few problems I’d like help with.
One: I have made a script that sends the ‘correct’ series of remote commands (see below). This works… sometimes. Other times, it seems to only go down twice instead of three times or something like that. It’s inconsistent.
Is there any way to make this work consistently?

Secondly, in the same category, sometimes a YouTube clip has an extra link for donating or something, so I really need 7 right presses instead of 6.
Is there any way to know whether how many steps the video needs?
I think for both of these questions, if I could “select” the word/link instead of press, press, press… that would solve the problem, but I don’t think that’s possible… is it?

Two: The script runs consistently, albeit with the problems listed above, when I run it from a link in the HA UI. However, I’ve set up a nifty webhook automation from my TV remote using the app “Button Mapper” to send a POST request. When I do this, the automation triggered by the webhook runs the script and… chaos. I can’t tell what it’s doing, but it ends up fast-forwarding, going back to the menu… not the same as when running ‘normally’.
Why is this different than invoking the script from the UI, and more importantly, how do I solve this?

Thanks!
Here’s my code:

Script:

youtube_speed_up:
  alias: YouTube Speed Up
  sequence:
  - action: remote.send_command
    metadata: {}
    data:
      num_repeats: 2
      delay_secs: 0.4
      hold_secs: 0
      command: DPAD_UP
    target:
      entity_id: remote.theatre_tv
  - action: remote.send_command
    metadata: {}
    data:
      num_repeats: 6
      delay_secs: 0.2
      hold_secs: 0
      command: DPAD_RIGHT
    target:
      entity_id: remote.theatre_tv
  - action: remote.send_command
    metadata: {}
    data:
      num_repeats: 1
      delay_secs: 0.4
      hold_secs: 0
      command: DPAD_CENTER
    target:
      entity_id: remote.theatre_tv
  - action: remote.send_command
    metadata: {}
    data:
      delay_secs: 0.7
      hold_secs: 0
      command: DPAD_DOWN
      num_repeats: 2
    target:
      entity_id: remote.theatre_tv
  - action: remote.send_command
    metadata: {}
    data:
      delay_secs: 0.8
      hold_secs: 0
      command: DPAD_DOWN
    target:
      entity_id: remote.theatre_tv
  - action: remote.send_command
    metadata: {}
    data:
      num_repeats: 1
      delay_secs: 0.4
      hold_secs: 0
      command: DPAD_CENTER
    target:
      entity_id: remote.theatre_tv
  - action: remote.send_command
    metadata: {}
    data:
      delay_secs: 0.7
      hold_secs: 0
      command: DPAD_DOWN
      num_repeats: 2
    target:
      entity_id: remote.theatre_tv
  - action: remote.send_command
    metadata: {}
    data:
      num_repeats: 1
      delay_secs: 0.4
      hold_secs: 0
      command: DPAD_CENTER
    target:
      entity_id: remote.theatre_tv
  - action: remote.send_command
    metadata: {}
    data:
      num_repeats: 2
      delay_secs: 0.4
      hold_secs: 0
      command: BACK
    target:
      entity_id: remote.theatre_tv
  description: ''
  icon: mdi:run-fast

Webhook automation:

alias: tv-green
description: ""
triggers:
  - allowed_methods:
      - POST
      - PUT
    local_only: true
    webhook_id: tv-green
    trigger: webhook
conditions: []
actions:
  - action: script.youtube_speed_up
    metadata: {}
    data: {}
mode: single