Media_player:play_media ...how to get the media_content_id: value

I found several posts asking how to get the magic media_content_id:.
jjlawren posted the answer [Media player: Play media --> How to get the content ID? - #3 by jjlawren], but I found it still a bit cryptic. Today I went back to Radio_browser integration page. It still says

When creating an automation, use the Play Media action to pick a station from the directory.

I guess this hint gives a clue to experienced HA developers - but is far too subtle for new non-developers.

More detailed instructions are …

  1. Going to Settings > Automations & Scenes > create automation > Create new automation.

  2. In the New Automation page, under the “then Do” section, click [+ Add Action] .

  3. On the Add action pop-up, select “media Player”, then the first “Play media” option - NOTE that there are 2 options both labelled “Play media” - you want the one with subtitle “Play media on media player”; and NOT the one with subtitle “Starts playing specified media”. You should now see:

  4. Select your media player, then pick the media (audio) file you wish to play (it uses the HA Media Sources pop-up, so you can select local files (in the /media/ folder in HAOS, or a NAS share configured on Settings > System > Storage as a Media usage), Radio Browser (if installed), an installed Text-to-speech provider, … or I guess most anything installed in HA. It should now look similar to

  5. Having selected the music to play, use the vertical 3-dots button to select the “Edit in YAML” option, and hey presto, there in the “data:” section is the media_content_id: that you can copy and paste into lovelace buttons, other automations, etc. There are also the title: and thumbnail: parameters provided by Radio Browaser.


Note that the same media selector is not available in the Developer Tools, where I had expected it.

5 Likes

Or play the station and the media_content_id is an attribute of the media_player entity.

Playing the same Radio station from my initial post (ABC Classic FM MP3) and then looking at HA > Developer Tools > States for my Kitchen Speaker shows a different media_content_id value (though they both work).

A bit more explanation for new users…
The radio-browser.info website maps their station ID to the current actual stream, which we see in the Developer Tools. Using the actual stream will start the music playing a little quicker - but if the station changes their streaming provider, we have to go back to radio-browser.info and look it up again to find the new stream URL.


Similarly, Developer Tools also shows local filenames translated into URLs

HAOS exposes the /media/ folder on the HA machine (mine is 192.168.1.98)

The file /media/Il Volo/04-smile.mp3 is translated to media_content_id: http://192.168.1.98:8123/media/local/Il%20Volo/04-Smile.mp3

We can use the Settings > System > Storage to map a samba or NFS folder into the /media/ folder. In my case “_Media” is the samba share which contains music. Playing on of the songs therein gives the
media_content_id: http://192.168.1.98:8123/media/local/_Media/Music/_NZ%20Music/Bic%20Runga/All%20Fall%20Down.mp3

2 Likes

Hello! I have searched and searched to find the one which says “Play media on media player” but I can’t find it in the list when you type in ‘media player’. I can only find the “Starts playing specified media” which then doesn’t work. :frowning:

The action is media_player.play_media

https://www.home-assistant.io/integrations/media_player/#action-media_playerplay_media

No, it’s not. That’s the one with “Starts playing specified media” that doesn’t allow you to pick a media file.

Sigh. I found media_player rather confusing - all the pieces of required information are there in the documentation, but just not linked in a way that is very obvious. So I wrote the original post to make it easier … but I obviously did not do a good job :frowning:

Actually the screen image you showed does require you to enter a “Content ID” which is the pointer to the media file you want to play.

However I guess that you want to select a media file from some list … which I did say in my original post

In other words … you are in the wrong part of the user interface for what I think you are wanting to do.

Going back to the start of the original post

This is the page which allows you to pick media … and from where you can copy the media_content_id into the Content ID field in the Developer Tools pages.

As i said, it is rather confusing and took me quite a while to figure it out.

1 Like

Oh, wow, thanks for this, it did indeed work.

The problem was that when you click “+ Add action” and then you search for “play media”, only the wrong one shows up. You really have to navigate there through the “Media player” item, without using the search.

Actually there is a simpler solution to switch when the UI is asking for the “Content ID” and “Content Type”, simply:

  • Put a space in each field.

That’s it, as soon as you hit the spacebar in the second field (order doesn’t matter) the two fields disappear.

They are the same action in YAML, but if the data is formatted like this:

action: media_player.play_media
metadata: {}
data: {}

Then you get the two fields, if it is formatted like this:

action: media_player.play_media
metadata: {}
data:
  media_content_id: " "
  media_content_type: " "

Then you get the “+ Pick media” interface.