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?
For your flows you need your local IP adres if your server/ raspberry. Something like this; 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.
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!!