How to correctly cast youtube videos in sequence, with the state of media_player?

Hi,

I am trying to make a script to cast several youtube videos in sequence,
trying to identify the “paused” state when the video finishes, and to move on to cast subsequent video.
However “paused” state seemingly is set, more than I see, like at the start of the streaming,
it appears to pause more than once, which ends up to my eyes, skipping the video in the sequence.

My script now is,

cast_jazz_funk:
  sequence:
  - data_template:
      entity_id: media_player.googledevice
      media_content_type: cast
      media_content_id: >-
        {% set media_content_id = {"app_name": "youtube", "media_id": state_attr('sensor.jazz', 'url') | replace('https://www.youtube.com/watch?v=','')} %}
        {{ media_content_id |to_json }}
    service: media_player.play_media 
  - service: media_player.media_pause
    entity_id: media_player.googledevice
  - wait_template: "{{ is_state('media_player.googledevice', 'paused') }}"    
  - data_template:
      entity_id: media_player.googledevice
      media_content_type: cast
      media_content_id: >-
        {% set media_content_id = {"app_name": "youtube", "media_id": state_attr('sensor.acidjazz', 'url') | replace('https://www.youtube.com/watch?v=','')} %}
        {{ media_content_id |to_json }}
    service: media_player.play_media 
  - service: media_player.media_pause
    entity_id: media_player.googledevice
  - wait_template: "{{ is_state('media_player.googledevice', 'paused') }}"    
  - data_template:
      entity_id: media_player.googledevice
      media_content_type: cast
      media_content_id: >-
        {% set media_content_id = {"app_name": "youtube", "media_id": state_attr('sensor.dancefloor', 'url') | replace('https://www.youtube.com/watch?v=','')} %}
        {{ media_content_id |to_json }}
    service: media_player.play_media 
  - service: media_player.media_pause
    entity_id: media_player.googledevice

I thought this is the way I can use wait_template, but is there any other way to
get the real world media_player “paused” state?
Casting individual videos, no issues.

Thank you in advance,

rkjr

Looks like I was able to fix it by adding delay, before the wait_template.

Final script now is,

cast_jazz_funk:
  sequence:
  - data_template:
      entity_id: media_player.googledevice
      media_content_type: cast
      media_content_id: >-
        {% set media_content_id = {"app_name": "youtube", "media_id": state_attr('sensor.jazz', 'url') | replace('https://www.youtube.com/watch?v=','')} %}
        {{ media_content_id |to_json }}
    service: media_player.play_media 
  - delay: 00:00:03    
  - wait_template: "{{ is_state('media_player.googledevice', 'paused') }}"    
  - data_template:
      entity_id: media_player.googledevice
      media_content_type: cast
      media_content_id: >-
        {% set media_content_id = {"app_name": "youtube", "media_id": state_attr('sensor.acidjazz', 'url') | replace('https://www.youtube.com/watch?v=','')} %}
        {{ media_content_id |to_json }}
    service: media_player.play_media 
  - delay: 00:00:03    
  - wait_template: "{{ is_state('media_player.googledevice', 'paused') }}"    
  - data_template:
      entity_id: media_player.googledevice
      media_content_type: cast
      media_content_id: >-
        {% set media_content_id = {"app_name": "youtube", "media_id": state_attr('sensor.dancefloor', 'url') | replace('https://www.youtube.com/watch?v=','')} %}
        {{ media_content_id |to_json }}
    service: media_player.play_media