Random media playback: how to?

Hi, what I’m largely unsuccesful while working with HA is random media playback. I have several automations such as playback of cartoons for kids or lunchtime playlists but I cannot get them to play randomly. I don’t almost ever want them to play in direct order as that results in me hearing the first track over and over again. I have made several threads and tried many approaches over two years, to no real avail.

I cannot get random playback to work on Kodi, Sonos nor Spotify. For example, Spotify has the “random_song: true” attribute that doesn’t do anything on my setup (also, toying with the Spotify integration often boots me out with "[spotipy.client] HTTP Error for PUT to https://api.spotify.com/v1/me/player/play with Params: {} returned 403 due to Player command failed: Restriction violated" which requires HA restart, which is not really HA’s fault but no fun nevertheless.

A different example, official documentation says this, emphasis mine:

script:
  play_kodi_smp:
    alias: "Turn on the silly box with ***random*** Firefighter Sam episode"
    sequence:
      - alias: "TV on"
        service: media_player.turn_on
        target:
          entity_id: media_player.kodi
      - service: media_player.play_media
        target:
          entity_id: media_player.kodi
        data:
          media_content_type: DIRECTORY
          media_content_id: special://profile/playlists/video/feuerwehrmann_sam.xsp

Nope, not random.

I would love some sort of built-in support for this, or any help at all really. I don’t think this is too much to ask from an advanced automation system such as HA, is it? :slight_smile: Thanks!

I haven’t used Kodi in a long time, but don’t you need to make the smart playlist in kodi random?

Like example 3.3 here: Smart playlists/Examples - Official Kodi Wiki

I assume you are using Spotcast for your Spotify playback. There is an offset parameter that you can set to choose which song in the playlist to start at. I’ll bet you can use the random sensor to dynamically assign that offset. Using this technique along with the shuffle parameter set to true would probably do what you want.

1 Like

I have a slight issue that I don’t have these kids’ cartoons outside the Kodi library (meaning it has no metadata and is accessed through File/Folder hierarchy) - I don’t want it to clutter my library. That’s a an issue a bit of my own making, but anyway, this shouldn’t be so hard.

Thank you!

Hey,
I just stumbled over your topic here and dont know, if you already solved it.

I build a similar solution, for my kids spotify accounts, using nfc tag reader.
First of all, I created some input_selects that look for example like this:

playlists_paw_patrol:
  name: Paw Patrol
  options:
  - https://open.spotify.com/album/2n7i1gQPKO7NB6RNf8fErI?si=6AM6jE0JTx6x0HQPHo9a_Q&dl_branch=1 # 31
  - https://open.spotify.com/album/3RiQQxENfL7eHf3gqRbgO2?si=9lAajbenSxCC1KBrId-csw&dl_branch=1 # 32
  - https://open.spotify.com/album/3aPHwqo36UHqfdW3wiirY9?si=_oE7pLDZSFK0RrDYKyl7SA&dl_branch=1 # 33
  - https://open.spotify.com/album/3GHQ48ryvWTcIS9k4cKhRM?si=6odXxACyRsO7nBMPYxmTNw&dl_branch=1 # 34

and in my automation, I pick a random entry of this list and play it at the specific kids device:

media_content_uri: "{{state_attr('input_select.playlists_paw_patrol', 'options') | random }}"

Have a nice one

3 Likes