#Trying to play a random album... this shouldn't be this hard!

1 messages · Page 1 of 1 (latest)

cold ruin
#

All I want to do is play a random album on my LMS system (integration is working fine, just automation not) based on the available albums it has. Ideally, I would be able to add a filter based on the number of tracks (because there are some singles), but let's start with "any album".

I can create an automation with variables like the attached yaml (discord won't let me paste the code because they want to extract money from everyone) but nothing in the media_player actions actually seems to take template/variable input! The result of the second search_media in that automation is always "every album", and the result of the play_media is always the first in the list.

Is this really not possible?!

hexed salmon
#

Apparenly I don't have a media player which supports media_player.search_media. But step 1 and 2 of the actions work?
Why do you need a second media_player.search_media in action 3?

And yeah, annoying enough the whole content id etc is really dependant on the integration/device.
What you can try is via Dev tools => actions => select media_player.play_media. Then browse the media (Pick media) and try to play an album. And then look at the yaml for it.

For a Sonos device I don't seem to be able to select an album though, only a track :/

cold ruin
#

I was testing whether the search_media would take a variable. It clearly isn't. I can look at the results and understand the content_id fine, if I replace the {{selected_album_content_id}} with an album id - say 37, it will play that album. I don't want to preselect the album though, I want to randomly select it.

#

My problem isn't with understanding the content_id, it's passing it into any method of media_player without hardcoding it.

hexed salmon
#

Mm, I'm not sure how integration dependent the media_player.play_media is. But would expect it to take a tempalte

#

let me check this evening with Sonos. But yeah, can't do the serach

fathom falcon
#

You aren't setting a value for selected_album or selected_album_content_id within the proper scope, you are only setting them in the logbook action instead of a script variable.

But the real issue is getting the number of albums. I use Music Assistant which has a more useful (but still kind of annoying) action music_assistant.get_library that you can use to figure out how many albums exist in the library. I update that value once a week and set it to an Input Number. Once you have that number the automation can be as simple as:

triggers:
  - trigger: state
    entity_id:
      - input_button.play_random_album
    id: Virtual Button
conditions: []
actions:
  - variables:
      player: media_player.bathroom_esp32
      album_count: "{{ states('input_number.album_count')|int(2000) + 1 }}"
  - variables:
      selectedalbumid: "{{ range(1, album_count) | random }}"
  - action: media_player.play_media
    metadata: {}
    data:
      media:
        media_content_id: "{{ selectedalbumid }}"
        media_content_type: album
      enqueue: replace
    target:
      entity_id: "{{ player }}"
mode: single

If you can't find a way to do it with LMS, one hack would be to just use a static value for album_count that is reasonable but larger than your actual album count, then use a Repeat Until to call a random numbered album until the media player starts playing something.