Anyway to save a media players current state and return to it after an event?

Hi All,

Curious to know if there is any way to save a specific media player’s current state and return to it after an event has fired that includes that same media player.

Use Case: If I am watching YouTube TV on my kitchen display (media_player) and someone rings my doorbird doorbell… the live feed from the doorbell will show on that same media player for 10 seconds. After it stops, I’d love for the media player to return to YouTube TV and what I was watching.

Anyone have a creative way to achieve this?

I haven’t tried but can you save media player settings with scenes?

Save a scene, do your thang, restore the scene.

Hmmmm, interesting thought. Let me give this a try and report back. Thanks!

Great idea, but I don’t think this will work. I don’t think you can’t save or restore specify the media_content_id and I believe the app disconnects also. Actually I don’t even think you can get Netflix to restart at all from HA unless someone has come up with something lately I missed.

People have been trying to solve this issue with TTS announcements and Spotify forever. Best I’ve been able to do myself is get Spotify to restart the same playlist (but not from where it left off).

If you’re using Google cast media players Assistant Relay addon does work using the broadcast feature, but it’s still not ideal.

If I’m wrong here PLEASE correct me because if so I’ve got some work to do!

Yeah, I took a look and the scene idea unfortunately doesn’t work. I am not a YAML expert, but anyway to set media_content_id to a variable or something… Then recall it?

As I understand it, the problem is that to start playing media in HA you use the media_player.play_media command and must provide the media_content_id as a parameter. With Netflix, that ID is not available for us to use in HA.

Look at the difference between the attributes in the media player state between Netflix and Spotify.

Interesting… I see that for NetFlix. I hadn’t looked at that yet. I am going to do some exploration this weekend (nothing else to do, thanks COVID) to see if other platforms can at least be addressed. I did some quick testing with my google nest hub max and did not have any success since YouTubeTV is essentially casting.

D’oh! I dunno why I thought you talking about Netflix. Probably because that was my issue in the past. I’m pretty sure Netflix will always be a bust but of course you can get url’s for youtube! You’ve piqued my curiosity because I haven’t looked at this issue in a while.

A quick search leads me to believe you’re probably going to need this. I’ll fool around with it too just for shits and giggles.

Well this works. Kinda. I was able to cast a youtube video, run this script, change it to spotify during the delay, and the original video did resume playing.

However - the video did not resume in the youtube app. Also if you try to run it a second time while the now non youtube video is playing it fails as the url is now something with more than 255 characters (limit of input_text).

In the end I don’t think it’s going to work for a robust solution. I suppose it was that easy there would probably be hundreds of examples available! But here it is if you want to play with it.


input_text:
  youtube_id:
    max: 255

script:
  play_youtube_video:
    sequence:
      - service: media_extractor.play_media
        entity_id: media_player.bedroom_tv
        data_template:
          media_content_id: "https://www.youtube.com/watch?v={{ media_content_id }}"
          media_content_type: video/youtube

  save_youtube_video:
    sequence:
      - service: input_text.set_value
        data_template:
          entity_id: input_text.youtube_id
          value: "{{ state_attr('media_player.bedroom_tv','media_content_id') }}"

  play_youtube:
    sequence:
      - service: script.save_youtube_video

      # delay so we can switch media player content manually for test
      - delay:
          minutes: 1

      - service: script.play_youtube_video
        data_template:
          media_content_id: "{{ states('input_text.youtube_id') }}"
1 Like

Thanks man! I will mess with this a bit today. I think this probably works (sort of, as you said) for a TV solution. I’ll need to tweak it a bit for my google nest hub max I imagine.

Would there be a way to save multiple states in one text field? I have a nest hub so I would like to save multiple fields to also show an image and text back on the display instead of only the media content.