# \[TUTORIAL\] Sonos random radiostations and playlists

**URL:** <https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576>\
**Category:** Tutorials\
**Created:** [August 13, 2020, 2:08pm UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576 "2020-08-13T14:08:13Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 13, 2020, 2:08pm UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/1 "2020-08-13T14:08:13Z")

</div>

Tutorial Random Radiostations and playlists

With this tutorial it is possible to play random radiostations or playlists on your Sonos device. You also need a server with a Docker possibility(I use the Synology 218+ in this tutorial). Every NAS or Raspberry Pi will also do.

For Homey you need to install the following apps; HTTP request, Homey Script and Better Logic.

First off all install the Sonos Docker api on your server or RPI. I followed these instructions, many thx to @mart

[Sonos Say - #64 by Martijn\_Hoogenbosch](https://community.homey.app/t/sonos-say/6697/64)?

For your flows you need your local IP adres if your server/ raspberry. Something like this; [http://192.167.178.2:5005](http://192.167.178.2:5005)

After you have the Sonos Docker running  
we need a Homeyscript. With mostly copy/paste I made the following script. For getting the script to work as I want I had great help from @JPe4619 and @robertklep. Thx guys.

Install Homeyscript. Access Homeyscript after installing Homeyscript on Homey with the following link on a computer.

> [@\[TUTORIAL\] Sonos random radiostations and playlists](https://community.homey.app/t/tutorial-sonos-and-random-radiostations-and-playlists/33576):
>
> Tutorial Random Radiostations and playlists With this tutorial it is possible to play random radiostations or playlists on your Sonos device. You also need a server with a Docker possibility(I use the Synology 218+ in this tutorial). Every NAS or Raspberry Pi will also do. For Homey you need to install the following apps; HTTP request, Homey Script and Better Logic. First off all install the Sonos Docker api on your server or RPI. I followed these instructions, many thx to @mart[https://comm…](https://community.homey.app/t/sonos-say/6697/64)

Make a new script, look for the arrow. Give your script the following name; SonosRandomVariables.

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/4/7/47413e8b5f48aa214a97f1fb998bec37b8e93648.jpeg)

With the following link you go to the Homey Community; [https://community.homey.app](https://community.homey.app).

```
// Script from Marcel Ubels for randomly playing radiostations and playlists with your Sonos Device
// With the use of a Synology Server and the Sonos Docker Api 
// Using Homey's apps Better Logic, HTTP request
// The Sonos Docker api has the extra ability to clear a queue and make an http request.
// This gives us the possibility to use variables in the http request

// Add your station or playlist @ var nums. You add it like this; "538 Ibiza Radio"
// The name off the station or playlist has to be added as a favorite in the Sonos app
// on your phone/tablet/pc. The name @ var nums has to be exactly the same

var nums = ["538 Ibiza Radio","Deep House Ibiza","538 Dance Radio","Dance FM","538 Global Dance Chart","Deep Radio","Dance Pop Remixes","Disco Radio Action","Our House FM","12 inch Classics Archive","Deep House Relax 3","Sky Radio 101 FM","Qmusic","Slam!"];
var usedNums = [];

// Calling Better Logic

let BLApp = await Homey.apps.getApp({id:"net.i-dev.betterlogic" });
    while (nums.length) {
        var nummer = nums.length.toString();
        var randomIndex = Math.floor(Math.random() * (nums.length));
        var randomNumber = nums.splice(randomIndex, 1)[0];
        console.log(randomNumber.toString() + " " + nummer) ; //+ " " + nums) ;
// await setTagValue("MyNumber"+nummer, {type: "string", title: "Mynumber"+nummer}, randomNumber.toString());
 
 // Adding the variables in Better Logic, +nummer+"/" adds a unique number at the end of de variables name
 // Give your variable a name f.i "RadioDay". Your variables has to be exact the same as you name it in this
 // script. Capital sensitive. If you added 14 stations or playlists you have to add 14 variables. 
 // f.i. RadioDay1, RadioDay2, Radioday3......RadioDay14
 // You can name your variable as you want. Replace "RadioDay" with your own name.

        let result=BLApp.apiPut("RadioDay"+nummer+"/" + randomNumber.toString());
// usedNums.push(randomNumber);
    };
// console.log(usedNums);

// Making it possible to call a routine at the AND part off a flow
    return true;

```

**Copy the script above** and go to Homeyscript. use your keyboard Ctrl-V and click Save. In the script you have to make some changes. I have added some comments where and how you can change it. Click Save.

**In Homey make you variables in Better Logic.** Make the variables as described in the script. All variables type; String. Make four extra variables type; String. RadioToday, RadioYesterday, RadioStation and RadioTrack

**Make a flow to activate your script;**

[https://homey.app/f/a5eaHf](https://homey.app/f/a5eaHf)

**Make a flow to dubbelcheck** you don’t have the same radiostation or playlist two days in a row;

[https://homey.app/f/khs5Ef](https://homey.app/f/khs5Ef)

**Make a flow to change your radiostation or playlist everyday;**

[https://homey.app/f/EDy5Ef](https://homey.app/f/EDy5Ef)

**Make a flow to search for your radiostation or playlist.** At the AND part you fill in your playlists. That way your playlist can start automatically. Do you have more than three playlists, make an extra flow with one, two or three extra playlists. This flow will be the first flow, at the ELSE part make an extra actioncard; Start a flow. In that case the flow below has to change a little bit. At the WHEN part make an actioncard; When this flow is started.

[https://homey.app/f/ak46Ef](https://homey.app/f/ak46Ef)

**Make a flow to start listening**

[https://homey.app/f/z6H7Ef](https://homey.app/f/z6H7Ef)

**You now only have to make a flow to start the above flow.**

Many Many listening pleasures!!

---

<div class="post-metadata">

**Author:** ![Martijn\_Hoogenbosch](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/martijn_hoogenbosch/32/10647_2.png) [@Martijn\_Hoogenbosch](https://community.homey.app/u/Martijn_Hoogenbosch)\
**Post date:** [August 16, 2020, 9:19am UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/2 "2020-08-16T09:19:18Z")

</div>

I have created everything according your manual, except for the last flow, that seems a bit of work and i’ll do that later.

But i’m wondering how this exactly works, where is the connection made to the sonos api? Cant seem to find it in the script.

And the URL’s of the stations, where are they? Or do you have them added as a favorite in sonos application?

* * *

Right, that part is well explained in the comments of the script ;-), still don’t know where you add the task to the sonos api.

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 16, 2020, 11:52am UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/3 "2020-08-16T11:52:29Z")

</div>

**The part make your flow to search for your radiostation or playlist** has a few http get requests. That is the part that makes connection with your Nas. So the ip-adres in this flow must be the internal ip adres of your NAS and your Sonos Docker(probably:5005)

---

<div class="post-metadata">

**Author:** ![Martijn\_Hoogenbosch](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/martijn_hoogenbosch/32/10647_2.png) [@Martijn\_Hoogenbosch](https://community.homey.app/u/Martijn_Hoogenbosch)\
**Post date:** [August 16, 2020, 12:27pm UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/4 "2020-08-16T12:27:37Z")

</div>

Right! There are 5 flows. Unfortunately i cannot see the entire GET statements:

![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/e/d/ed66c0df54be2d6684ef200a501908d68d06dd23.png)

---

<div class="post-metadata">

**Author:** ![Rocodamelshekima](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rocodamelshekima/32/8209_2.png) [@Rocodamelshekima](https://community.homey.app/u/Rocodamelshekima)\
**Post date:** [August 16, 2020, 12:33pm UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/5 "2020-08-16T12:33:52Z")

</div>

Select the txt and copy/paste

---

<div class="post-metadata">

**Author:** ![Martijn\_Hoogenbosch](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/martijn_hoogenbosch/32/10647_2.png) [@Martijn\_Hoogenbosch](https://community.homey.app/u/Martijn_Hoogenbosch)\
**Post date:** [August 17, 2020, 3:40pm UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/6 "2020-08-17T15:40:44Z")

</div>

I think I have it running, one question though. I don’t really get what the idea is behind the Radiozender opzoeken flow. So if this is started (and it gets started by the last flow) it first analyses if the RadioToday is equal to a playlist name, if not, it executes the else. But if it IS the same, it does nearly the same except for the shuffle, it turns shuffle on… Do I understand it correctly this doesn’t has anything to do with the radio channels, but with adding a playlist eg spotify playlist, and starts shuffle on that?

* * *

And I must be doing something wrong, i’m receiving a error 500 on the following GET action:  
[Http://192.168.0.90:5005/veranda/playlist/RADIOTODAY](Http://192.168.0.90:5005/veranda/playlist/RADIOTODAY), where the radiotoday the variable is. If I look at the var, it is correctly filled. When copying the var and manually adding it to the url it does work.

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 17, 2020, 4:01pm UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/7 "2020-08-17T16:01:37Z")

</div>

Yes the first part is especially for the playlists and the second part for the radiostations. I am working on a better way for the playlists, because the playlist is a Sonos playlist. A Sonos playlist stays always the same. Therefore i am making a new script which ads a Spotify playlist. That way if the playlists changes you always have the newest playlist running.

In my case i have a playlist in Spotify, when that playlist gets new songs it will be added towards a playlist called “playlist archive”. The “playlist archive” has all the original songs and the newly added songs. This playlist grows and grows. At the moment i have several of those playlists.

---

<div class="post-metadata">

**Author:** ![Martijn\_Hoogenbosch](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/martijn_hoogenbosch/32/10647_2.png) [@Martijn\_Hoogenbosch](https://community.homey.app/u/Martijn_Hoogenbosch)\
**Post date:** [August 18, 2020, 8:22am UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/8 "2020-08-18T08:22:21Z")

</div>

And any clue about the error i’m getting? It has to do something with formatting. There is no space or anything.

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/4/1/41cef1669689091827d67b299bfeda52f096d227.jpeg)

Right, there we’re spaces in my VAR, don’t know how they got there, will look further.

I’ve included some spaces by accident in the var setting flow. Should be solved now.

* * *

But it isn’t, it keeps adding spaces while setting the variables, don’t understand why.

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 18, 2020, 9:40am UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/9 "2020-08-18T09:40:42Z")

</div>

Maybe there are spaces in your script, maybe between the quotation marks. Spaces are quite annoying because you hardly see them, but they do influence your flows

---

<div class="post-metadata">

**Author:** ![Martijn\_Hoogenbosch](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/martijn_hoogenbosch/32/10647_2.png) [@Martijn\_Hoogenbosch](https://community.homey.app/u/Martijn_Hoogenbosch)\
**Post date:** [August 18, 2020, 9:49am UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/10 "2020-08-18T09:49:32Z")

</div>

Nope, finally figured it out… created all flows with [flows.athom.com](http://flows.athom.com), but constantly spaces. Now created the same flow with the app and now it works great. Took me a little bit longer than anticipated, but now works, thanks for your effort!

---

<div class="post-metadata">

**Author:** ![Martijn\_Hoogenbosch](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/martijn_hoogenbosch/32/10647_2.png) [@Martijn\_Hoogenbosch](https://community.homey.app/u/Martijn_Hoogenbosch)\
**Post date:** [September 3, 2021, 6:07pm UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/11 "2021-09-03T18:07:31Z")

</div>

I have been running these scripts for quite some time now and I’m (we are) really happy with it. Now since recently I’ve bought a new Sonos device, the Sonos Five. It is called Eetkamer because it is located in my dinerroom.

When the first person in the morning walks down the living, it starts playing the radio. I now Join the eetkamer to the living and it also starts playing radio.

What I can’t seem to figure out is when someone wants to play Spotify on the eetkamer, it should leave the woonkamer group and just do the spotify. But when playing ‘something’ it doens’t detect when something else is going to play.

I have checked the state and there is one thing always the same when playing radio on the above way. And that’s the albumart results to a URL with the IP of my Homey.

```auto
  "currentTrack": {
    "artist": "NPO 3FM",
    "title": "PINK - JUST LIKE A PILL",
    "albumArtUri": "/getaa?s=1&u=x-sonosapi-stream%3as6707%3fsid%3d254%26flags%3d8224%26sn%3d0",
    "duration": 0,
    "uri": "x-sonosapi-stream:s6707?sid=254&flags=8224&sn=0",
    "trackUri": "x-sonosapi-stream:s6707?sid=254&flags=8224&sn=0",
    "type": "radio",
    "stationName": "NPO 3FM",
    "absoluteAlbumArtUri": "http://10.10.20.15:1400/getaa?s=1&u=x-sonosapi-stream%3as6707%3fsid%3d254%26flags%3d8224%26sn%3d0"

```

Would it be possible to have the HTTP request card filter on this ‘absoluteAlbumArtUri’?

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [September 3, 2021, 8:06pm UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/12 "2021-09-03T20:06:20Z")

</div>

Do you mean when on the eetkamer is playing spotify and on the other Sonos device absolute albumaruri is that unique url, leave the group?

---

<div class="post-metadata">

**Author:** ![Martijn\_Hoogenbosch](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/martijn_hoogenbosch/32/10647_2.png) [@Martijn\_Hoogenbosch](https://community.homey.app/u/Martijn_Hoogenbosch)\
**Post date:** [September 6, 2021, 1:08pm UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/13 "2021-09-06T13:08:07Z")

</div>

My apologies for the late reply. Yes that’s what I mean, so a trigger on what is inside of the ‘absoluteAlbumUri’.

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [September 6, 2021, 5:41pm UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/14 "2021-09-06T17:41:58Z")

</div>

Ik that case i assume you have to do a get request and get the body response for absolutealbumuri. If it is different you start a flow to regroup your devices. The problem is in this case when you start the get command and you ask this every minute, you still have the problem that for a minute your devices are still grouped together. Is there somehow a spotify trigger?

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [September 6, 2021, 6:05pm UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/15 "2021-09-06T18:05:38Z")

</div>

I assume you start spotify with the spotify app, in that case there is also the possibility to choose your room, choose to group room and choose a presetted room. In this case i made 1 room named woonkamer spotify, which only has 1 device

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/e/6/e601f7fc1b9fa66531007ad4a05c306d9e0c1074.jpeg)  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/a/4/a489e58ecd6a41dafea51e8a3aa4b56240251a34.jpeg)  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/2/b2d6b73e75bfae2a3c2c0f7a1503e3814ea113e6.jpeg)

---

<div class="post-metadata">

**Author:** ![Martijn\_Hoogenbosch](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/martijn_hoogenbosch/32/10647_2.png) [@Martijn\_Hoogenbosch](https://community.homey.app/u/Martijn_Hoogenbosch)\
**Post date:** [September 7, 2021, 9:24am UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/16 "2021-09-07T09:24:35Z")

</div>

> [@Marcel\_Ubels](#):
>
> , choose to group room and choose a presetted room. In this case i made 1 roo

This for sure is an option. I’d love to see it working without having to instruct my users (wife + kids). I indeed am using the spotify / sonos.

I’ll look into it when I have time, thanks very much for your help and idea’s.

---

<div class="post-metadata">

**Author:** ![myazth](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/myazth/32/82776_2.png) [@myazth](https://community.homey.app/u/myazth)\
**Post date:** [November 21, 2023, 1:23am UTC](https://community.homey.app/t/tutorial-sonos-random-radiostations-and-playlists/33576/17 "2023-11-21T01:23:15Z")

</div>

Saw this tutorial and noticed that some of the steps to make the flows are a bit outdated. Is there something more recent?
