Play random album of artist X on specific spotify connect device

Hi,
does anybody know a way to play a random album of a certain artist to a spotify connect device with HA?

I already have python script that can do exactly that but it looks like it is going to be cumbersome to integrate it in HA as it uses includes/has dependencies…
Any tips?

This seems to be an option but I would have to figure out the URIs of all albums and “hardcode” them in HA:

Thanks

1 Like

Bump… No ideas anyone?

This shall be used to play a certain audio book/drama from a certain artist every night when going to bed which is available on spotify.

Bumping this as well :slight_smile:
I’m looking for the exact same thing - I played around with spotifys API for a while but the “get an artist’s album API” is limited to 50 albums. This does not help me as there are over 200 albums available.

Can somebody please help here? Thanks!!

Have a solution meanwhile, have a small script which has an array of all the albums and plays one randomly. As I needed to update my array I thought I may share this here.

To get the list of albums:

  • open the spotify web player and go to the discography
  • make sure to scroll so that everything is loaded
  • F12 for developer console
  • use javascript in console to print list of all albums (paste code below in the console and press enter)
var albums = ""; $$('a[title]').forEach((element,index) => albums += "\"spotify:album:" + element.href.replace("https://open.spotify.com/album/","") + "\",\n"); console.log(albums)

you can also filter albums with certain titles (in this case albums with an / in the name)

var albums = ""; $$('a[title*="/"]').forEach((element,index) => albums += "\"spotify:album:" + element.href.replace("https://open.spotify.com/album/","") + "\",\n"); console.log(albums)

In Homeassistant I have a script which uses that array (playing a random Die Drei Fragezeichen audio play)

play_ddf:
  alias: Play Random DDF
  sequence:
  - data:
      source: Echo Schlafzimmer
    entity_id: media_player.spotify_lukas
    service: media_player.select_source
  - data:
      shuffle: false
    entity_id: media_player.spotify_lukas
    service: media_player.shuffle_set
  - data:
      media_content_type: playlist
    data_template:
      media_content_id: '{%- set albums = [  "spotify:album:76pYNMMVZTiuNy3IoXcavU",
        "spotify:album:1cJ3fNx6K47p4eDFqhnvsA", "spotify:album:4BZfSV9maCil4l4yftT74F",
        "spotify:album:2zHMeFNQQxKOmUgmppOjIM", "spotify:album:7pZotNNAYOkeHNvJRKzYRk",
        "spotify:album:3ieWqOL150wJOcft7iZHKZ", "spotify:album:0q4FbI22wSMrzgWh5vgHi9",
        "spotify:album:6WgRUQY49NRG6JT6dJ4xEq", "spotify:album:6BxvBG51szGmlg5x3F1rHj",
        "spotify:album:7khAMIQlYBmW7mI9cphDhJ", "spotify:album:2Sw9FBHOoT6fBhYxHsRFxq",
        "spotify:album:1yMMjH3tDN9GyXiADzTkL9", "spotify:album:79Drf49yNdGLBzzHMGGmBz",
        "spotify:album:4Exxckn0GwbhnGl6N4um2N", "spotify:album:4FxNfDSXqAg8N1D8NBtvZ5",
        "spotify:album:09Ckx1JLgPsQHHGuSIt2j3", "spotify:album:7bTKP2hU0LRks1bXN1R7ko",
        "spotify:album:4piiNuehCc4VjN8NBIcnRt", "spotify:album:0GRoTtX1wI16M7GSnnN96R",
        "spotify:album:39a7bHWZyWPxwukCHPrTly", "spotify:album:3pbXIzVk7CA5VM2ltxu9TN",
        "spotify:album:2oKngJQA5TdxF5QcP3uZEf", "spotify:album:4m6VlVOWfrUVhWWrueMo7p",
        "spotify:album:1qag2AiFcxXU1UaIgrDFff", "spotify:album:2ICtKWqLqMBTzi7UeByhKd",
        "spotify:album:100ZHoBX994GlckxjoGpma", "spotify:album:02MXdFaVMz0qQJjmivjI8g",
        "spotify:album:22soWryC08ctyMNb6NrMGD", "spotify:album:6uj8JeIgluMibSu8Divkbh",
        "spotify:album:5kevid18ttcAPK1vk0kLtc", "spotify:album:7eHh5PAxQBt5KBWzd3a10G",
        "spotify:album:5B7w9vkfh979tEyzwkLk9k", "spotify:album:1qhlRmclvYWkrYkLnFxFZN",
        "spotify:album:4zCPToY7AoTqvOKf4HRqAN", "spotify:album:18xyskqkEGnVfVFhznkFIK",
        "spotify:album:6tioH9PNiZbmUT54MsiX5k", "spotify:album:4IZ7Lsm5le7HJPtaGLCrjO",
        "spotify:album:3w5glP0mzKfTanzXpAjN27", "spotify:album:0w6Khy7kpqTXrEDITf0rZ0",
        "spotify:album:4qbxSVpr3NxRjdIVC411ue", "spotify:album:4uggmrJ3Xpm1e2tcz6laze",
        "spotify:album:5RoL0jdWMoJ8KRFVOeFYtX", "spotify:album:2ALDCHCHdawewN1sduWVQQ",
        "spotify:album:6Anfub4AoVHg9bhJ7js0L0", "spotify:album:6YbaUGc6ZeR1YnDFGQJV8F",
        "spotify:album:62CKj2RCiWDDUtBWTn4KxQ", "spotify:album:5PEHeYcUhz1Aq02ZB1pUgJ",
        "spotify:album:77aawQqgLQZxPb9kwSg0JR", "spotify:album:7kskBahbrQWCOiaXKuoPjE",
        "spotify:album:427kScPIeUSfoNKCNercMC", "spotify:album:4izJKxYT4fTwIZjoXNgkX7",
        "spotify:album:3An657Q0wkVp4nIDg2zpcB", "spotify:album:4fv0pPN7ZjbUiejSTxTPZh",
        "spotify:album:7beppYTqtHAEPKoXyodssF", "spotify:album:1GEzdjI5N25iuGjvypRscs",
        "spotify:album:2F6VxgWbGCUpTU9ux6Dtbi", "spotify:album:4rCAnDrNJ96srnBEoA0ISj",
        "spotify:album:3uFt95otJjaxH8o40tPtIS", "spotify:album:0pdXEEgKjfh4H0aZg4IOTS",
        "spotify:album:78wUIvkGT2CdWW0ngeEy8V", "spotify:album:623V90QowsF4WSqV3cHGKf",
        "spotify:album:3uggg8gNlh1kpBxLeOfFTV", "spotify:album:7nxSEeJiQwI34VwnWT83C9",
        "spotify:album:5iNeANVy4supl8HowB9V2h", "spotify:album:3u0OSCwiOblOhm4dKjwLN5",
        "spotify:album:6cPgVsZ6TPyVIP9BHP5ty5", "spotify:album:7K7dfMc3VwHOKtENI1BWTD",
        "spotify:album:2uOsf31oenHRd4DVxCSjn9", "spotify:album:35aU9bfmxPGdjiRA4JMuFd",
        "spotify:album:3D9Jf4mdOBoeyrKpffYBvy", "spotify:album:6TjzBTmeDakBgh5NOGj1BC",
        "spotify:album:2RxTF6MhAVOtudVNOyHTQz", "spotify:album:4Y7Pxv7pzLmKVJowmBwOkd",
        "spotify:album:71moidSHTwJsc2N70axEce", "spotify:album:6gxj68DOHRSVlEM4xzwtRy",
        "spotify:album:3HxAtK23R2gFkW000mdaFT", "spotify:album:6RJf8I60kNhnYxlnw7eTGv",
        "spotify:album:79enDMGQWH7LQ5weNB96g7", "spotify:album:3m7fR2wOkytZHq9m4QeBkB",
        "spotify:album:5HiWGQmaCRaxdaon49Iw9S", "spotify:album:0TmWLedytkxVuP9b5Hqeqq",
        "spotify:album:1Cta130Rwp5vH1ddfAChbU", "spotify:album:1juMQy6yZVmk9Za6HeiicB",
        "spotify:album:23wcElHGWpunC23ggr4PcW", "spotify:album:4eorcDi9eRfsi7X0A91Z7u",
        "spotify:album:5l5CFSjzSdhbflqmplcTFX", "spotify:album:1lD4RdnibVS7QhtZqCal9E",
        "spotify:album:3XXYXmCEe185oHxigCRo97", "spotify:album:0mp5HVWlYinEYa7Y4Q2vau",
        "spotify:album:0AnyLaLPx339iGaxjqVfob", "spotify:album:0sIRyuExi7jBQmOpHyyY78",
        "spotify:album:0LoesJ9VnJPrlvIDz4u4Xt", "spotify:album:3HgetGSMzg6bGM6b67YZcj",
        "spotify:album:68NWcgqeCQMZ3QcPJXBhzH", "spotify:album:6d21lwV9Imn3ObWUyV1UN2",
        "spotify:album:6fVTJQKJBku1yaOmZOnfwg", "spotify:album:7rPDg8NlFJ1a2HMTQvVaB2",
        "spotify:album:1GKXpMQRKEyZI8pL62VUIP", "spotify:album:3piMnopGsrEAKd1EZxsU3i",
        "spotify:album:3rxr4SLE2EG0EAlpSeI99o", "spotify:album:5Hafee7JOUqNVqtsrCtrTZ",
        "spotify:album:5L2KCw21QT3JKg1KKuqSeF", "spotify:album:3bFvjy9IyWd7GrzN2dr0Hi",
        "spotify:album:0WBhXS0uAYuL1sYb6vjYeS", "spotify:album:7b9OiVsktCZyldiAnjkVDx",
        "spotify:album:1OBHPhBUih26oQTqxLvJTQ", "spotify:album:4RZllDAKF3DeDz6jZJFXTa",
        "spotify:album:3v4W4AFaI8uO7J88ojaSkS", "spotify:album:3kBby5GJMI4U97XljKcPFD",
        "spotify:album:4eX1wH67TDcYPZ1ZsjtPb6", "spotify:album:6Mi4Smq7Fn8qNOg6Xnf0In",
        "spotify:album:6joRNVLN69v3eSnxM86BSM", "spotify:album:4ADdmpObDUmBdvyJ9oNz5o",
        "spotify:album:3NYWEuwAPt4PIJ4OeNxmzO", "spotify:album:1DZAe0qMw8Pq9EPoX6gETA",
        "spotify:album:6zwBnyiy9Hy9TAser0DOuL", "spotify:album:0bC2ZiWlcizdzpwCt8vbkV",
        "spotify:album:3haGZZ8x7PyaGxiqcLmLf9", "spotify:album:4C4o7tHpT20QrdsvmI70uI",
        "spotify:album:2bfJa4PRKVb7as86HZv8Df", "spotify:album:5ub4r9Q0YVMrWuOEesu6dR",
        "spotify:album:3UHj03eU7OnDuMVCTLbmcj", "spotify:album:7a9rwqPkVdU4FC2cWFudVO",
        "spotify:album:7yMRFgR8FRyWwiLHzQQa2B", "spotify:album:0Jh7gzQaOscoFndb3raq2l",
        "spotify:album:2dlgtHB8z7ZhSl1fsSb0Ic", "spotify:album:3TcIn19pVz5LXE8UleuLuX",
        "spotify:album:3CZCwdMpj2jnjI43ZlnY5T", "spotify:album:1KpWkUQaj5ECJsrb8SrPl4",
        "spotify:album:4uF9MjchYgq5jjPi5x5YZy", "spotify:album:2eeodcwD9HXpcA2rvoFkIq",
        "spotify:album:3bhsXwKIDwVK5LTkDCICp0", "spotify:album:3yofHdHiOLtVZlxWCZ4XBB",
        "spotify:album:7iysTnwaaFrPPWAuUyyi0c", "spotify:album:2s5u6hCaMpTIvZtBRvQzha",
        "spotify:album:4izXuApLzBWtWHrFqgcrxL", "spotify:album:1rDYlXZy6vN4wqqQ6buqOr",
        "spotify:album:6t7Y1H4dUM0d4JycenVskN", "spotify:album:3CMN8Y4kjoG4sFwlOEpfsV",
        "spotify:album:6RDPGc2budsdTw1lj8f2O5", "spotify:album:2IqfdlQamN3vCWN3CKLEVI",
        "spotify:album:69VLqtWA1bpguYRCFRAxQl", "spotify:album:1K8kXLhNnHSdPvtUwf74DE",
        "spotify:album:2GQjkF0ceIse6jVZkUuAQc", "spotify:album:6EEN6onzbHIOxnwUkGfKhv",
        "spotify:album:4mlQEwk6I0XuksZCYTtatz", "spotify:album:2erJcBofKBO6GFMqDPppsU",
        "spotify:album:21pUsEhxssm6fvumGFmi35", "spotify:album:7Hjk98j1FSrHV5IHWmeeFh",
        "spotify:album:6qj4LD0zotutkIgEg9OvDj", "spotify:album:62z7yW4RKhhuAp0RklmNvc",
        "spotify:album:0tmMvsGyWRzVckCYgkvve3", "spotify:album:5GPTZKrD7eaCp9p6VOBuIN",
        "spotify:album:0biXUv1uGUaVsPcw8nAZCY", "spotify:album:26M4ktHp3isOX6XJazAxPn",
        "spotify:album:7cBoiWgh1bTMZmCwJE0eMu", "spotify:album:2qcA1EqAoUsqxXqIivY6Zc",
        "spotify:album:6DWykdlgf60Mq02hPAGYVD", "spotify:album:5W6dRiaeSVE7zUBYHbepak",
        "spotify:album:3fotka2Hbq0N7V9KKWIDdw", "spotify:album:4oH2GUWlSSsSgVkxyPVHX1",
        "spotify:album:1Fg15cBLFliy6Kr60QqRan", "spotify:album:0F9ebKwTd5RNZZOu49lWuF",
        "spotify:album:3pXJh2A9gYzNt22rEIE5q7", "spotify:album:71kiy6INV3LzsCddpTZoHf",
        "spotify:album:6DKfjDIB1BSYRGAi2YJYm3", "spotify:album:7t2SteAH5OnLLC1ghTAmka",
        "spotify:album:5IRxm1lo35PttzU8YLslZy", "spotify:album:3iWNsrEs9D0FFlKFfscdvL",
        "spotify:album:6YX4i7aMCYmn20zvAFM3lG", "spotify:album:39mWWzaUZp0AXyN9Vnp4od",
        "spotify:album:6JNNLdq9ELeKENK3RmxLWQ", "spotify:album:48QVzskevXtA0KpJjXzvb1",
        "spotify:album:24bba3w45LGGm7ps3qonkc", "spotify:album:5KUXX8H2LNsuik46M5XLx0",
        "spotify:album:2IIw1KESZrJQBFdZQAWcKY", "spotify:album:0AKPkUh4ElyZNipc0lZoiR",
        "spotify:album:6d3Pavm0enVICdOvBEqxYA", "spotify:album:6R4gnRHXKgw2cxneWgTiEL",
        "spotify:album:4R24oyyqxGkczNpqWGCXBZ", "spotify:album:2REEfC6tVMiYv1eKMlGiOX",
        "spotify:album:2L18qTB1EfS4FuBk2M6QwV", "spotify:album:1imKVIfJIM0vKNymevqKjP",
        "spotify:album:7K3VEzD1rm0dgUGBi1i5PW", "spotify:album:0tPetsUWuZIC0E85x6eVN5",
        "spotify:album:0zV9pnaXLuogDr6prsvb4M", "spotify:album:5JHijjtr65MjdNOnNvD3Ec",
        "spotify:album:7Dbd2rya9glmBLVxclcU4d", "spotify:album:2AisAUu4deLpNi6g6qlTLC",
        "spotify:album:2NSreywe8UbWo7BJVY3Spk", "spotify:album:6RXr8BmKNSK9Zk11k0I7Dl",
        "spotify:album:211IEWHi59nUsHx8li8G9r", "spotify:album:67qFOAxbhNRHEIeOjQ9Zkp",
        "spotify:album:7i50aK2wKvszWNnay4DtxP", "spotify:album:4KEZWleMTT8lDaQDLgozFc",
        "spotify:album:2koZEkq7vQnHjMP73gVXCo", "spotify:album:5ilbdbGu5eQRUIsbslyiLe",
        "spotify:album:3JqJ2zIt3UzKug7onmbmAo", "spotify:album:1yqICFWBL4l7fJkrFWvfWL",
        "spotify:album:0CMsNalUakIcv6znyVLALr", "spotify:album:16dOhg07DVqoSxlS7ToLM5",
        "spotify:album:2dQYcaTRh5qEYk2oH7WoBC", "spotify:album:3O5STjjugoL5cNKfeRuBiU",
        "spotify:album:2AZ7zFBophWEIuBPjAG60I", "spotify:album:0bSrvyOjOzFoFJfx3uaFgk",
        "spotify:album:07EhJ6uxWcQQWKeXJ1e8Lb", "spotify:album:4h75QQIxW1etThTuw6vWzg",
        "spotify:album:0aNqdp5ayuUNsOdUwu8x0b", "spotify:album:6MRsf5IcfqJIogaNqtESnh",
        "spotify:album:3nGyW4ETDrDpInYEAQCyYS", "spotify:album:54eP8BYMR270sTqklv83V0",
        "spotify:album:5YWM39RnabpxekZuHriTam", "spotify:album:2w902iYtkf0ipmTImyLlsL",
        "spotify:album:61OtrnMm1lqoMgMRb1aw7g", "spotify:album:0xldqK4Ocdt8dwQSxUzt6x",
        "spotify:album:4N9tvSjWfZXx3eHKblYEWQ", ] -%} {% set rindex =  (range(0, (albums
        | length - 1) )|random) -%} {{albums[rindex]}}

        '
    entity_id: media_player.spotify_lukas
    service: media_player.play_media
  mode: single
7 Likes

Works like a charm! Thanks a lot, my Kids will love it.

Thank you for this script, unfortunatly it doesnt seem to work for me. Is it still being used and does it work for you?

Yes still works for me every day. Which Error do you get?

1 Like

It seems, spotify can’t find my playing device (google nest)…

I found that sometimes it would not find my amazon echo, I then play something there with the spotify app and after a minute or so it showed up in homeassistant. However I did not have the problem for quite some time

Hi thex.
Please, please. please create a small video on how you set it up.
I and especially my daughter would be very grateful to you for that.

Let’s see what I can do. Which part of my explanation above is not clear?

Thanks a lot. Please from the beginning.

I’m asking what is unclear about my initial explanation as I don’t know what I would explain differently if I explain it once more.

I got it working. Thanks again. :slight_smile:

1 Like

When I try your top code, I get the following result from the console:

“spotify:album:https://instagram.com/spotify”,
“spotify:album:https://twitter.com/spotify”,
“spotify:album:Spotify”,

Anyone know what I’ve done wrong?

I’ve run the JS code on this page:
https://open.spotify.com/artist/0gxyHStUsqpMadRV0Di1Qt/discography/all

Also: Massive thanks for this template. This is so cool.

Hi,

you need to scroll down the complete page to load all albums. That way there is more content in the javascript console output.

Also - there is probably a “show more” button that expands all the rows.

Hope this helps :slight_smile:

1 Like

I just recently put in the time to match the spotify ID with the corresponding title and number. The file can be accessed here https://www.dropbox.com/scl/fi/3xdm31yrzts9lxsktmx6k/Liste-dreiFragezeichen-09-2023.xlsx?rlkey=cy7gj3kcylrl0fm53e9cggayp&dl=0

:pray: Thank you! What are the odds that I find this on a generic post on artist albums for exactly for “Die Drei ???” I want to implement it for, and also 17d after I finally buy the speaker.

Spotify probably updated their page.

Your can use the following JS:

var albums = ""; $$('a[href*="intl-de/album"]').forEach((element,index) => albums += "\"spotify:album:" + element.href.replace("https://open.spotify.com/intl-de/album/","") + "\",\n"); console.log(albums)

You might need to adapt the intl-de part in the snippet, maybe that is different depending on the country. Just check the link target of any album on that page.

Furthermore I think there is a small index issue in the original script by @thex, the last element is never selected. The last two lines could be simplified to this:

{{ albums[(range(albums | length) | random)] }}

The range does not need a start parameter, it’s 0 anyways. Also the end does not need to be reduced by one, e.g. range(2) returns 0 and 1.

my problem is different, much simpler, I would simply like to continue listening from where I left off the last time, but instead it always starts again from the beginning. Can’t find out what you were listening to via API? Since I added Spotify to Home Assistant, my Google Home devices, when I ask “Hey Google, play some music” always start with the same list of songs in the same order. What could have happened? I just wish I could continue listening where I left off, just like I did before the integration. Possible?