Play Next Episode - Script to play next unwatched Plex show on Android TV

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

The blueprint works by selecting 3 media player entities and a title of a show.

The “Media Player” is the media player entity that is attached to the TV. This one is given a “Turn On” signal.

The “Media Player Remote” is the Android TV remote integration, for starting the plex app.

The “Plex Media Player” is the Plex media player entity, which actually plays the content.

Finally, the Title is the name of the show, like “Adventure Time” or “Futurama”

After creating an instance of the Script, simply call into it with an action from any trigger, like pressing a button on a zigbee remote

blueprint:
  name: Play Next Episode
  description: >-
    A script that sends a request to play a show to a targeted media player.
  domain: script
  author: Ken Roecks
  input:
    shield_player:
      name: Media Player
      description: The media player to control
      selector:
        target:
          entity:
          - domain:
            - media_player
    remote:
      name: Media Player Remote
      description: The media player to control
      selector:
        target:
          entity:
          - domain:
            - remote
    plex_media_player:
      name: Plex Media Player
      description: The media player to control
      selector:
        target:
          entity:
          - domain:
            - media_player
    title:
      name: "Title"
      description: "The title of the button shown in the notification."
      default: ""
      selector:
        text:
mode: single
variables: 
  show_title: !input 'title'
  shield_id: !input shield_player
  remote_id: !input remote
sequence:
  - service: system_log.write
    data:
      level: info
      message: >
        "Playing {{ show_title }} - Shield= {{ shield_id }} Entity= {{ shield_id.entity_id }}"

  - choose:
      - conditions:
          - condition: template
            value_template: "{{ is_state(shield_id.entity_id, 'off') }}"
        sequence:
          - service: media_player.turn_on
            metadata: {}
            data: {}
            target: !input shield_player
          - delay:
              hours: 0
              minutes: 0
              seconds: 0
              milliseconds: 100
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ is_state_attr(shield_id.entity_id, 'app_id', 'com.plexapp.android') }}"
        sequence: []
      - conditions: []
        sequence:
          - service: remote.turn_on
            data:
              activity: plex://
            target: !input remote
            enabled: true
          - delay:
              hours: 0
              minutes: 0
              seconds: 3
              milliseconds: 0
  - service: media_player.turn_on
    target: !input shield_player
  - service: remote.turn_on
    data:
      activity: plex://
    target: !input remote
    enabled: true
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - service: media_player.play_media
    data:
      media_content_type: EPISODE
      media_content_id: >-
        plex://{ "library_name": "TV Shows", "show_name": "{{ show_title }}",
        "episode.unwatched": true, "episode.inProgress": [true, false],
        "resume": 1, "maxresults": 1 }
    target: !input plex_media_player
1 Like