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.athom.com.

// 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