Homey Community Forum

[TUTORIAL] Sonos random radiostations and playlists

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?

For your flows you need your local IP adres if your server/ raspberry. Something like this;

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.

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

With the following link you go to the Homey Community; 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;

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

Make a flow to change your radiostation or playlist everyday;

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.

Make a flow to start listening

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

Many Many listening pleasures!!


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.

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)

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


Select the txt and copy/paste

1 Like

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://, 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.

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.

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

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.

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

Nope, finally figured it out… created all flows with 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!

1 Like

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.

  "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": ""

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

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?

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

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?

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

1 Like

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.