Can't play media on Plex after opening with Apple TV source change

Hi folks, I have a strange one here.

If I have Plex open on my Apple TV, I can play media on it fine, like this:

service: media_player.play_media
data:
  media_content_type: EPISODE
  media_content_id: >-
    { "library_name" : "TV Shows", "show_name": "The West Wing",
    "episode.unwatched": true, "episode.inProgress": [true, false], "sort":
    "addedAt:asc", "maxresults": 1 }
target:
  entity_id: media_player.plex_plex_for_apple_tv_mb_apple_tv

However, if I start with waking up the Apple TV, and then open Plex with the following code, I can never get it to work:

  - service: remote.send_command
    data:
      command: wakeup
    target:
      entity_id: remote.mb_apple_tv
  - service: media_player.select_source
    data:
      source: Plex
    target:
      entity_id: media_player.mb_apple_tv

It opens Plex properly, but then I get errors about HA not being able to contact the IP/port of the Plex client. I’ve tried sleeping a couple seconds, and using a wait template for the client state to be ‘idle’, but nothing is working.

Has anyone setup a script which is simply:
1 wake up Apple TV
1 open plex
1 play media to plex

Any help would be appreciated, thanks.

You’ll likely need a step which forces a scan for newly available Plex clients. You can do this by using the new Plex button.scan_clients_* entity along with the button.press service.

You may also need to add a step after the scan to wait for the state of the Apple TV Plex client to change to a non-unavailable state before playing media. You may need to add a short delay between switching sources and scanning for the Plex client.

Thanks @jjlawren ! I actually had been trying that as well, with little luck. However, since you responded, I tried again, and simplified it. The following script seems to work 100% of the time so far:

sequence:
  - service: remote.send_command
    data:
      command: wakeup
    target:
      entity_id: remote.mb_apple_tv
  - service: media_player.select_source
    data:
      source: Plex
    target:
      entity_id: media_player.mb_apple_tv
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - service: button.press
    data: {}
    target:
      entity_id: button.scan_clients_superplex
  - wait_template: >-
      {{ not is_state('media_player.plex_plex_for_apple_tv_mb_apple_tv',
      'unavailable')   }}
    timeout: '45'
  - service: media_player.play_media
    data:
      media_content_type: EPISODE
      media_content_id: >-
        { "library_name" : "TV Shows", "show_name": "The West Wing",
        "episode.unwatched": true, "episode.inProgress": [true, false], "sort":
        "addedAt:asc", "maxresults": 1 }
    target:
      entity_id: media_player.plex_plex_for_apple_tv_mb_apple_tv
mode: single

So fingers crossed that this continues to work properly. Thanks for the response!

Great, glad that’s working well. I can imagine a scenario where the Plex app takes longer to launch (after calling select_source) and the Plex client scan finishes before the app is ready to respond. If that happens you can extend the delay or perhaps use a retry loop for the scan/wait steps.

Yep, it stopped working reliably last night. I’ll have to figure out where to properly delay things.

Did you get this working? My plex client always show unavailable when not playing.

- alias: 'Outhouse Plex Movie Timer'
  trigger:
    platform: state
    entity_id: 
      - sensor.dan_iphone
    from: 'outhouse'
    for: 
      minutes: 2
  action:
    - service: media_player.select_source
      target:
        entity_id: media_player.outhouse_appletv
      data:
        source: "Plex"
    - delay:
        hours: 0
        minutes: 0
        seconds: 10
        milliseconds: 0        
    - service: button.press
      data: {}
      target:
        entity_id: button.scan_clients_nas
    - wait_template: >-
        {{ not is_state('media_player.plex_outhouse', 'unavailable') }}
      timeout: '45'
    - service: media_player.play_media
      target:
        entity_id: media_player.plex_outhouse
      data:
        media_content_id: '{"library_name": "Movies", "title": "Joker"}'
        media_content_type: movie
  mode: restart

Hi, I haven’t messed with this much since this posting. Recently I’ve run the script below, and it seems to work. But I haven’t tested it a lot to see if it’s super reliable:

alias: "MB: West Wing Watch Next"
sequence:
  - service: remote.send_command
    data:
      command: wakeup
    target:
      entity_id: remote.mb_apple_tv
  - service: media_player.select_source
    data:
      source: Plex
    target:
      entity_id: media_player.mb_apple_tv
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - service: button.press
    data: {}
    target:
      entity_id: button.scan_clients_superplex
  - wait_template: >-
      {{ not is_state('media_player.plex_plex_for_apple_tv_mb_apple_tv',
      'unavailable')   }}
    timeout: "45"
  - service: media_player.play_media
    data:
      media_content_type: EPISODE
      media_content_id: >-
        { "library_name" : "TV Shows", "show_name": "The West Wing",
        "episode.unwatched": true, "episode.inProgress": [true, false], "sort":
        "addedAt:asc", "maxresults": 1 }
      enqueue: replace
    target:
      entity_id: media_player.plex_plex_for_apple_tv_mb_apple_tv
mode: single

Hope this helps some.

1 Like