Media Player return to playing state

Hi @techtim,

I made a modified version of my previously mentions “TTS and resume” script and tested it. I expect this will work for you:

alias: Auto Trigger
variables:
  media_entity: media_player.office
  temp_media_ContentId: media-source://media_source/local/Music/sfx01.mp3
  temp_volume: 0.35
  temp_duration_seconds: 15
  mediaplayer_State: '{{ states(media_entity) }}'
  mediaplayer_volume_level: '{{ state_attr(media_entity,''volume_level'') }}'
  mediaplayer_media_content_id: '{{ state_attr(media_entity,''media_content_id'') }}'
sequence:
  - service: media_player.volume_set
    data:
      entity_id: '{{ media_entity }}'
      volume_level: '{{ temp_volume }}'
  - service: media_player.play_media
    data:
      entity_id: '{{ media_entity }}'
      media_content_id: '{{ temp_media_ContentId }}'
      media_content_type: music
  - delay:
      hours: 0
      minutes: 0
      seconds: '{{ temp_duration_seconds }}'
      milliseconds: 0
  - service: media_player.volume_set
    data:
      entity_id: '{{ media_entity }}'
      volume_level: '{{ mediaplayer_volume_level }}'
  - service: media_player.play_media
    data:
      entity_id: '{{ media_entity }}'
      media_content_id: '{{ mediaplayer_media_content_id }}'
      media_content_type: music
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ mediaplayer_State != ''playing'' }}'
        sequence:
          - delay:
              hours: 0
              minutes: 0
              seconds: 6
              milliseconds: 0
          - service: media_player.media_pause
            data:
              entity_id: '{{ media_entity }}'
mode: single

Note:

  • At the beginning I made the media player entity, temp url, volume and duration as variables.
    If these are always the same, then keep them as is.
    Otherwise you can pass these as parameters to the script from the automation.

  • You have to make sure that the temp duration is enough to capture and play the stream.(this might take noticeable time in some situations). You can experiment changing this until you hit the desired duration.

Please let me know if this works for you

Kind regards,
Ghassan