Reliable automation for playing media on PleX

Hi all,

I’ve set-up NFC cards, with homemade printed stickers, which trigger automations to turn on the Apple TV, open PleX, and play different things (e.g. Bluey).

My kids can then scan these on my Adonno tag reader.

The issue is it only works sometimes. The Apple TV always turns on, PleX is always loaded up, but it will often not play anything and will instead just stay on the User selection screen.

To a lesser extent, it doesn’t auto next play the next episode when triggered this way, but when played normally it does.

The script I have is below, but I’m interested in how other people have achieved this with a much higher success rate.

Thanks in advance!

alias: Play Bluey
sequence:
  - service: media_player.turn_on
    target:
      entity_id: media_player.apple_tv
    data: {}
  - service: media_player.select_source
    target:
      entity_id: media_player.apple_tv
    data:
      source: Plex
  - wait_for_trigger:
      - platform: state
        entity_id: media_player.apple_tv
        to: "on"
    timeout:
      seconds: 10
  - service: plex.scan_for_clients
    data: {}
  - wait_template: >-
      {{ not is_state('media_player.plex_plex_for_apple_tv_apple_tv',
      'unavailable') }}
    timeout: "00:00:10"
    continue_on_timeout: false
  - service: media_player.play_media
    target:
      entity_id: media_player.plex_plex_for_apple_tv_apple_tv
    data:
      media_content_id: >-
        {  "library_name": "TV",   "show_name": "Bluey",   "episode.unwatched":
        true,   "episode.inProgress": [true, false],  "resume": 1,   "sort":
        "originallyAvailableAt:asc",   "maxresults": 1}
      media_content_type: EPISODE
  - wait_for_trigger:
      - platform: state
        entity_id:
          - media_player.plex_plex_for_apple_tv_apple_tv
        to: paused
    timeout:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
    continue_on_timeout: false
  - service: remote.send_command
    target:
      device_id: 30c718d554ce47f87d116279fae80fc6
    data:
      num_repeats: 1
      delay_secs: 0.4
      hold_secs: 0
      command:
        - play
mode: single
icon: mdi:television-classic

I have this same problem.

Tangentially, the scan_clients button does not work at all and I have to use the service.

Shame it doesn’t work properly. As far as I can tell Jellyfin doesn’t have a decent enough integration for this stuff to work either.