Play music after stream ends on SONOS

Hello,

I try to create a skript for SONOS media_player which startet a media_extractor stream and if the stream ends it should start a playlist.

But I don’t know how to start the playlist after the stream ends.
I try it with a “wait for trigger” but then always the playlist starts, also if I stop any music…
Anybody an idea how to solve the problem?

Here is my script:

alias: sonos_morgens
sequence:
  - service: media_player.volume_set
    data:
      volume_level: 0.07
    entity_id: media_player.bad
  - service: media_extractor.play_media
    data:
      media_content_id: 'http://www.tagesschau.de/100sekunden/index.html'
      media_content_type: music
    entity_id: media_player.bad
#  - wait_for_trigger:
#      - platform: state
#        entity_id: media_player.bad
#        from: playing
#        to: paused
  - service: media_player.shuffle_set
    data:
      shuffle: true
    entity_id: media_player.bad
  - delay: '2'
  - service: media_player.select_source
    data:
      source: Lieder
    entity_id: media_player.bad
mode: single

Did you check the state of the media player, does it really change to paused, or maybe to idle?

I checked the state- it is paused. So everytime I paused the player, it starts automaticaly :frowning:

Sorry, I don’t get it.
You start the script, it plays the “tagesschau” and then it immediately starts the playlist instead of playing “tagesschau” until the end? Is this your problem?

Or is the problem that the playlist starts to play when you stop the “tagesschau” manually?

You start the script, it plays the “tagesschau” and then it immediately starts the playlist instead of playing “tagesschau” until the end? Is this your problem?

That is the problem

And when the tagesschau starts, the media player changes to playing and it only changes to paused when it is finished?

Yes, it changes to paused after tageschau is finished

I solved the problem with a delay of media duration:

alias: sonos_morgens
sequence:
  - service: media_player.volume_set
    data:
      volume_level: 0.04
    entity_id: media_player.bad
  - service: media_player.clear_playlist
    data: {}
    entity_id: media_player.bad
  - service: media_extractor.play_media
    data:
      media_content_id: 'http://www.tagesschau.de/100sekunden/index.html'
      media_content_type: music
    entity_id: media_player.bad
  - delay: >-
      {% set duration = state_attr("media_player.bad","media_duration") %} {% if
      duration > 0 %} {% set duration = duration %} {% endif %} {{ duration |
      timestamp_custom("%H:%M:%S", 0) }}
  - service: media_player.shuffle_set
    data:
      shuffle: true
    entity_id: media_player.bad
  - delay: '2'
  - service: media_player.select_source
    data:
      source: Lieder
    entity_id: media_player.bad
  - service: media_player.shuffle_set
    data:
      shuffle: true
    entity_id: media_player.bad
mode: single
1 Like