Plex- Play the next Unwatched InProgress episode

Hello,

i tried to play the next unwatched in Progresss Episode and resume the point where i stop, but he always starts the episode from the begining and not from the last position.

in the wiki is this code, but that plays always from beginning.

{ "library_name": "Serien", "show_name": "halo", "episode.unwatched":
    true, "episode.inProgress": [true, false], "sort": "addedAt:asc", "maxresults": 1 }

Link to the wiki Plex Media Server - Home Assistant (home-assistant.io)
greets

Add "resume": 1 to the query. It looks like the docs need to be updated to reflect this.

1 Like

i searched for resume but found nothing. A Docs update will be nice.

Yep, suggested here: Document resume & offset in Plex `play_media` by jjlawren ¡ Pull Request #22477 ¡ home-assistant/home-assistant.io ¡ GitHub.

1 Like

do you use the function too?

i run in a little problem, i want to start the next unwatched inProgress Episode of a tvshow, but when there is no matching entry, i want that he plays a random episode of the tvshow, but i dont have any ideas if its possible to check if there is a episode with the first condition i dont believe or?

or gives a another way to do this?

my code for the first condition:

media_content_id: >-
    { "library_name": "Serien", "show_name": "{{
    states('input_text.firetv_kueche_tvshow_name') }}", "episode.unwatched":
    [true, false],"resume": 1, "episode.inProgress": true, "sort":
    "addedAt:asc", "maxresults": 1 }

EDIT

okay i found a way.
i check after the first Play command if the player is_idle when this is true, he sends a second play command with a random Episode of the TvShow.

alias: 'Serie abspielen - Firetv KĂźche (PLEX) '
description: |-
  1. starte angefangene Ungesehene Episode 
  alternativ 
  2. starte zufaellige episode
trigger:
  - platform: state
    entity_id: input_text.firetv_kueche_tvshow_name
condition: []
action:
  - choose:
      - conditions:
          - condition: device
            device_id: 06c3a968a177cfaacf95843823bf7f5e
            domain: media_player
            entity_id: media_player.fire_tv_192_168_0_42
            type: is_on
        sequence:
          - service: media_player.play_media
            data:
              media_content_type: EPISODE
              media_content_id: >-
                { "library_name": "Serien", "show_name": "{{
                states('input_text.firetv_kueche_tvshow_name') }}",
                "episode.unwatched": [true, false],"resume": 1,
                "episode.inProgress": true, "sort": "addedAt:asc", "maxresults":
                1 }
            target:
              device_id: dbfdcfee3a5f0423368baffdbd0da578
      - conditions:
          - condition: device
            device_id: dbfdcfee3a5f0423368baffdbd0da578
            domain: media_player
            entity_id: media_player.plex_firetv_kueche
            type: is_idle
        sequence:
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - service: media_player.play_media
            data:
              media_content_type: EPISODE
              media_content_id: >-
                { "library_name": "Serien", "show_name": "{{
                states('input_text.firetv_kueche_tvshow_name') }}", "shuffle":
                "1" }
            target:
              device_id: dbfdcfee3a5f0423368baffdbd0da578
    default: []
mode: single

Glad you found a workaround. There is a way to write more complex searches (e.g., logical “or” searches for fallback) as shown in the “Using Advanced Filters” section here: Library plexapi.library — Python PlexAPI documentation.

That being said, I haven’t validated that “and”/“or” search syntax can be passed through as-is, but it seems like it should be possible without any changes on the HA side.

I was happy too early, it worked once, then it was over.is also clear, the first condition is always true. can’t I omit the condition from the choose so that it only executes the code? this then runs into an error, because it does not find an appropriate episode, then it should skip to the other option

the problem is that he ends the automation after the first send command dont work, when he than execute the second there was no problem.

okay can you give me a little example how the call must be with this advanced filters?

Sorry, looks like the advanced filter might not fit in the current payload well.

There will be a new feature in 2022.5 which can continue automations/scripts on a service call error: Add ability to continue scripts/automations on error by frenck · Pull Request #70004 · home-assistant/core · GitHub. You’d probably still need to check the state of the media_player to make sure it’s not playing anything before retrying, but perhaps it will solve your specific need.

okay i will try it out, but first i must update my homeassistant.

i tried last night to do it over a script, call a script with the first condition, but then he stops too :face_with_head_bandage:

but i will give your idea a try, perhaps it works.
EDIT
i see 2022.5 is not released yet? :weary:

since the update to 2022.4.7 the “resume” :1 doenst work anymore. i always get a error in the payload

Problem in query {‘show_name’: “grey’s anatomy”, ‘episode.unwatched’: [True, False], ‘resume’: 1, ‘episode.inProgress’: True, ‘sort’: ‘addedAt:asc’, ‘maxresults’: 1}: Unknown filter field “resume” for libtype “episode”. Available filter fields: [‘episode.title’, ‘episode.addedAt’, ‘episode.originallyAvailableAt’, ‘episode.year’, ‘episode.userRating’, ‘episode.viewCount’, ‘episode.lastViewedAt’, ‘episode.unwatched’, ‘episode.inProgress’, ‘episode.duplicate’, ‘episode.hdr’, ‘episode.resolution’, ‘episode.mediaSize’, ‘episode.mediaBitrate’, ‘episode.subtitleLanguage’, ‘episode.audioLanguage’, ‘episode.trash’, ‘episode.unmatched’, ‘episode.guid’, ‘episode.id’, ‘episode.index’, ‘episode.lastRatedAt’, ‘episode.updatedAt’, ‘episode.audienceRating’, ‘episode.duration’, ‘episode.rating’, ‘episode.viewOffset’, ‘episode.label’]

even with the example code from the updated wiki

service: media_player.play_media
data:
  media_content_type: EPISODE
  media_content_id: >-
    { "library_name": "Serien", "show_name": "{{
    states('input_text.firetv_kueche_tvshow_name') }}", "episode.unwatched": true, "episode.inProgress": [true, false], "resume": 1, "sort": "addedAt:asc", "maxresults": 1 }
target:
  device_id: dbfdcfee3a5f0423368baffdbd0da578

so slowly it’s hard to get my problem under control when there are always new ones that I can’t influence

I’ll take a look later today. What version did you upgrade from?

from 2022.3.0

I found a workaround for the other problem, but it’s not very practical. I have outsourced the first play command to a script, this must be started with script_on and not script_ so that the automation does not stop even if the script fails. Then I have to check whether the player has the status idle, unfortunately I have to take 15 seconds because it apparently takes a long time to run the script and change the status of the media player, I take a shorter time it doesn’t work.

so you have unfortunately 15 seconds of silence until finally something is played. unfortunately this is impractical

my code:

alias: 'Serie abspielen - Firetv KĂźche (PLEX) '
description: |-
  1. starte angefangene Ungesehene Episode 
  alternativ 
  2. starte zufaellige episode
trigger:
  - platform: state
    entity_id: input_text.firetv_kueche_tvshow_name
condition: []
action:
  - choose:
      - conditions:
          - condition: device
            device_id: dbfdcfee3a5f0423368baffdbd0da578
            domain: media_player
            entity_id: media_player.plex_firetv_kueche
            type: is_idle
        sequence:
          - service: script.turn_on
            data: {}
            target:
              entity_id: script.f
    default: []
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - condition: device
    device_id: dbfdcfee3a5f0423368baffdbd0da578
    domain: media_player
    entity_id: media_player.plex_firetv_kueche
    type: is_idle
  - service: notify.alexa_media_echo_show_kueche
    data:
      message: >-
        Es tut mir leid, ich habe keine Passende Episode gefunden, ich suche
        eine Zufällige Episode der Serie "{{
        states('input_text.firetv_kueche_tvshow_name') }}" raus.
      data:
        type: tts
  - service: media_player.play_media
    data:
      media_content_type: EPISODE
      media_content_id: >-
        { "library_name": "Serien", "show_name": "{{
        states('input_text.firetv_kueche_tvshow_name') }}", "shuffle": "1" }
    target:
      device_id: dbfdcfee3a5f0423368baffdbd0da578
mode: single

Sorry, tracked down the “resume” issue to a regression after a feature was added to 2022.4. PR to fix it is here: Fix resume and offset params for Plex play_media by jjlawren · Pull Request #70809 · home-assistant/core · GitHub.

You’ll probably need to wait for the continue_on_error feature in 2022.5 to avoid introducing a timeout. That should be released on May 4th. Without knowing if the first call failed you’d have to infer what’s happening based on state, and that introduces a bit of guesswork.

1 Like

Okay thank you for your time and help.
I thought I was too stupid after hours of try and error.

okay i wait for it, in the meantime i found a working workaround.
i check if the script is off, and than make the other conditions and commands, this takes only 3 seconds until before 15 seconds.

alias: 'Serie abspielen - Firetv KĂźche (PLEX) '
description: |-
  1. starte angefangene Ungesehene Episode 
  alternativ 
  2. starte zufaellige episode
trigger:
  - platform: state
    entity_id: input_text.firetv_kueche_tvshow_name
condition: []
action:
  - service: script.turn_on
    data: {}
    target:
      entity_id: script.f
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - condition: state
    entity_id: script.f
    state: 'off'
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - condition: and
    conditions:
      - condition: device
        device_id: dbfdcfee3a5f0423368baffdbd0da578
        domain: media_player
        entity_id: media_player.plex_firetv_kueche
        type: is_idle
      - condition: not
        conditions:
          - condition: state
            entity_id: input_text.firetv_kueche_tvshow_name
            state: reset_input
  - service: notify.alexa_media_echo_show_kueche
    data:
      message: >-
        Ich habe keine begonnene Episode gefunden, ich suche eine Zufällige
        Episode der Serie "{{ states('input_text.firetv_kueche_tvshow_name') }}"
        raus.
      data:
        type: tts
  - service: media_player.play_media
    data:
      media_content_type: EPISODE
      media_content_id: >-
        { "library_name": "Serien", "show_name": "{{
        states('input_text.firetv_kueche_tvshow_name') }}", "shuffle": "1" }
    target:
      device_id: dbfdcfee3a5f0423368baffdbd0da578
mode: single

EDIT

no, no luck, i dont know why he must have 14 seconds before check if the player is in idle, the script takes 0.7 seconds to run.
i added a 3 seconds delay, more than enough. but than he plays the new episode and stops and plays a random episode.

i dont understand why he needs 14 seconds for this…

alias: 'Serie abspielen - Firetv KĂźche (PLEX) '
description: |-
  1. starte angefangene Ungesehene Episode 
  alternativ 
  2. starte zufaellige episode
trigger:
  - platform: state
    entity_id: input_text.firetv_kueche_tvshow_name
condition: []
action:
  - condition: not
    conditions:
      - condition: state
        entity_id: input_text.firetv_kueche_tvshow_name
        state: reset_input
  - service: script.turn_on
    data: {}
    target:
      entity_id: script.f
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - condition: state
    entity_id: script.f
    state: 'off'
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - delay:
      hours: 0
      minutes: 0
      seconds: 14
      milliseconds: 0
  - condition: and
    conditions:
      - condition: device
        device_id: dbfdcfee3a5f0423368baffdbd0da578
        domain: media_player
        entity_id: media_player.plex_firetv_kueche
        type: is_idle
      - condition: not
        conditions:
          - condition: state
            entity_id: input_text.firetv_kueche_tvshow_name
            state: reset_input
  - service: notify.alexa_media_echo_show_kueche
    data:
      message: >-
        Ich habe keine begonnene Episode gefunden, ich suche eine Zufällige
        Episode der Serie "{{ states('input_text.firetv_kueche_tvshow_name') }}"
        raus.
      data:
        type: tts
  - service: media_player.play_media
    data:
      media_content_type: EPISODE
      media_content_id: >-
        { "library_name": "Serien", "show_name": "{{
        states('input_text.firetv_kueche_tvshow_name') }}", "shuffle": "1" }
    target:
      device_id: dbfdcfee3a5f0423368baffdbd0da578
mode: single

Instead of using hardcoded delays, you may want to try using something like a wait_for_trigger and a wait timeout.

However you’re still going to run into the issue of not knowing if the first call succeeded. Instead of checking if it’s still idle after a timeout, maybe see how long it normally takes to change to the ‘playing’ state when it succeeds.

Either way this problem is going to be overly complex to solve without knowledge of success/failure of that first step.