Sonos playing random playlists and random radiostations

I made a homeyscript to play random radiostations set as favorite on Sonos and play random Spotify playlists. The script contains 14 days of music. You can adjust the radiostations and playlists.

Start the script once a day and a new radiostation or playlist can be played on your Sonos device. After two weeks the script will automatically refresh. The playing order will see to it that there will be no duplicate radiostations in a row, and this will also happen for the playlists.

You need a server with Docker. You have to search with docker for the chrisns-docker-node-sonos-http-api1. Install this with docker.

Add your local server’s ipadres and portnumberPrefix in this script

var ipAdres = ‘192.168.178.2’;
var portNumber = ‘5005’;

Add 2 string variables in Logic, called radioToday and radioName(case sensitive) and 1 boolean variable called radioUpdate. Search for the id of this variable like this:
Schermopname (15)

Make one global variable. Open a new script and enter the following:

Schermopname (14)

//Radiostations=> Add the exact name of your favorite Radiostation between the quotes(scase sensitive). It is possible to delete some radiostations. But do not delete Radio10, Radio11, Radio12.

var radio1 = radioPrefix + ‘Ibiza - HD’;
var radio2 = radioPrefix + ‘Deep Radio’;
var radio3 = radioPrefix + ‘Dance Radio Amsterdam’;
var radio4 = radioPrefix + ‘Dance FM’;

//Playlist => Add the ID of your Spotify playlist between the quotes. For your own needs you can add the name of the playlist after the //. It is possible to delete some playlistid’s.

var playListId1 = ‘1C5F8dA3nWRN46u6lCehqm?si=67RdCbXtSG27gx6C7qP2Sw’;//Deep Delight Part 2
var playListId2 = ‘1QdAD45sKZQwptkm4VeKmO?si=_5P2-G9-SZG91WsNHt0f4g’;//360 Dance Part 2
var playListId3 = ‘4YKqPMOlV5JJK6XVKKdWJ9?si=SL7iH53eSNu-PnsZJIcr6A’;//Deep House Part 2

You can obtain the playlistid like this:



//PlaylistNames => Add the Name as you want to get presented of your Spotify playlist between the quotes.

var playListName1 = ‘Deep Delight Part 2’;
var playListName2 = ‘360 Dance Part 2’;
var playListName3 = ‘Deep House Part 2’;

// Script for randomly playing your favorite Sonos radiostations and Spotify playlists
// Using a Server with the Sonos Docker Api 

// Add your Server's Ipadres and PortnumberPrefix 
var ipAdres = '192.168.178.2';
var portNumber = '5005';

// Do not adjust 
var radioPrefix = 'http://' + ipAdres + ':' + portNumber + '/woonkamer/favorite/';
var playListPrefix = 'http://'+ ipAdres + ':'+ portNumber + '/woonkamer/spotify/now/spotify:user:spotify:playlist:';

//In your Sonos app, from Sonos(not Homey) you need to add your personal radiostations as favorite. 
//There are three variables for playlists. Therefore the variables radio12, radio13 and radio14 may not
//been altered. 

//To add your playlists you can add 17 playlist at //PlayList. Search for the ID of your playlist in your Spotify App(not Homey) and paste this ID at //PlayList between the quotes

// Add 2 string variables in Logic, called radioToday and radioName(case sensitive) and 1 boolean variable called radioUpdate. Make one global variable. Open a new script and enter the following: global.set('radio', [])

//Radiostations=> Add the exact name of your favorite Radiostation between the quotes(scase sensitive). It is possible to delete some radiostations. But do not delete Radio10, Radio11, Radio12.
var radio1 = radioPrefix + 'Ibiza - HD';
var radio2 = radioPrefix + 'Deep Radio';
var radio3 = radioPrefix + 'Dance Radio Amsterdam';
var radio4 = radioPrefix + 'Dance FM';
var radio5 = radioPrefix + 'Deep House Ibiza';
var radio6 = radioPrefix + 'Deep Radio';
var radio7 = radioPrefix + 'Deep Dance Radio';
var radio8 = radioPrefix + 'Deep Dance Radio';
var radio9 = radioPrefix + 'Sky Radio - HD';
var radio10 = radioPrefix + 'Ibiza - HD';
var radio11 = radioPrefix + 'Disco Classic Radio';
var radio12 = 'play1';//Do not adjust this variable
var radio13 = 'play2';//Do not adjust this variable
var radio14 = 'play3';//Do not adjust this variable

var radio =[radio1,radio2,radio3,radio4,radio5,radio6,radio7,radio8,radio9,radio10,radio11,radio12,radio13,radio14];

//Playlist => Add the ID of your Spotify playlist between the quotes. For your own needs you can add the 
//name of the playlist after the //. It is possible to delete some playlistid's.
var playListId1 = '1C5F8dA3nWRN46u6lCehqm?si=67RdCbXtSG27gx6C7qP2Sw';//Deep Delight Part 2
var playListId2 = '1QdAD45sKZQwptkm4VeKmO?si=_5P2-G9-SZG91WsNHt0f4g';//360 Dance Part 2
var playListId3 = '4YKqPMOlV5JJK6XVKKdWJ9?si=SL7iH53eSNu-PnsZJIcr6A';//Deep House Part 2
var playListId4 = '7jzMzltNsYhaI5Xfkxe9If?si=IwDTD4ylQJC9c5JWxEUXQw';//Massive Pop Remixes Part 1
var playListId5 = '5sInx9KNxVqnONg9EsIptX?si=8T0QWluxSDqI3bClrf0OOQ';//Dance Classics Part 1
var playListId6 = '0NhMKI95ujIjriBaRXrIBs?si=pG_J4OXGTz6mhN_kG1mvMg';//Dance Pop Remixes Part 1
var playListId7 = '1s4YUBb95LaWazpgvNGzAu?si=35wEzzqcQIKAgSZq7bkEaA';//Deep House Relax Part 3 
var playListId8 = '60HqFyCYzr6dxr4OMxDtS7?si=x7Oa5bNBQXy4DQlC2COLaw';//Truly Deeply House Part 1
var playListId9 = '00JuraVC5HSlXCLpYJ4RQx?si=LJFWf4UoQ-2DtayVAGb26g';//360 Dance Part 1
var playListId10 = '0C66rsP0YemHrh3dUhSszt?si=gdtcDlzURLWQVZCKi8Evkw';//Deep House Relax Part 2
var playListId11 = '4Vaus40H2m6gkwNqhzzril?si=6y9qg0HxQAqfB_B3bhfeIQ';//Deep Delight Part 1
var playListId12 = '7kS4ICEIagMdVKUMAe2Hy1?si=CazM4dkSQLq-0wJ_IaiJDw';//Deep House Part 3
var playListId13 = '7fwbNi5Be9Jh2ljr9b8Z34?si=uyJ4egMZQHW1r2YmyAhGjA';//Truly Deeply House 2
var playListId14 = '28MJ5Ws6Hw7K3sM0buLxvS?si=sR9304GXTzikRt1n3so-MQ';//Marcels Favorieten 2017
var playListId15 = '4oH8GC0TMvQGlTXlj3luUq?si=RnmzYP4YRCWSPa9ptJTuRA';//Deep House Relax Part 1
var playListId16 = '4g1z40nOzcPytHV5DvWpvB?si=GVjxQsb6RTyA5CB72mjSLQ';//Deep House Relax 4
var playListId17 = '7GQS3BcOeogh8wKEeA3fN8?si=taswh15eRZuXmFAkyn6IfQ';//12inch Classics Part 1

var playListId = [playListId1.substring(0,22),playListId2.substring(0,22),playListId3.substring(0,22),playListId4.substring(0,22),playListId5.substring(0,22),playListId6.substring(0,22),playListId7.substring(0,22),playListId8.substring(0,22),playListId9.substring(0,22),playListId10.substring(0,22),playListId11.substring(0,22),playListId12.substring(0,22),playListId13.substring(0,22),playListId14.substring(0,22),playListId15.substring(0,22),playListId16.substring(0,22),playListId17.substring(0,22)];

global.set('playListId',playListId)

//PlaylistNames => Add the Name as you want to get presented of your Spotify playlist between the quotes. 
var playListName1 = 'Deep Delight Part 2';
var playListName2 = '360 Dance Part 2';
var playListName3 = 'Deep House Part 2';
var playListName4 = 'Massive Pop Remixes Part 1';
var playListName5 = 'Dance Classics Part 1';
var playListName6 = 'Dance Pop Remixes Part 1';
var playListName7 = 'Deep House Relax 3';
var playListName8 = 'Truly Deeply House Part 1';
var playListName9 = '360 Dance Part 1';
var playListName10 = 'Deep House Relax 2';
var playListName11 = 'Deep Delight Part 1';
var playListName12 = 'Deep House Part 3';
var playListName13 = 'Truly Deeply House 2';
var playListName14 = 'Marcels Favorieten 2017';
var playListName15 = 'Deep House Relax Part 1';
var playListName16 = 'Deep House Relax 4';
var playListName17 = '12inch Classics Part 1';

var playListName = [playListName1,playListName2,playListName3,playListName4,playListName5,playListName6,playListName7,playListName8,playListName9,playListName10,playListName11,playListName12,playListName13,playListName14,playListName15,playListName16,playListName17]

global.set('playListName',playListName)

//Do not change the next variables
var playList1 = playListPrefix + playListId[0];//Deep Delight Part 2
var playList2 = playListPrefix + playListId[1];//360 Dance Part 2
var playList3 = playListPrefix + playListId[2];//Deep House Part 2
var playList4 = playListPrefix + playListId[3];//Massive Pop Remixes Part 1
var playList5 = playListPrefix + playListId[4];//Dance Classics Part 1
var playList6 = playListPrefix + playListId[5];//Dance Pop Remixes Part 1
var playList7 = playListPrefix + playListId[6];//Deep House Relax Part 3 
var playList8 = playListPrefix + playListId[7];//Truly Deeply House Part 1
var playList9 = playListPrefix + playListId[8];//360 Dance Part 1
var playList10 = playListPrefix + playListId[9];//Deep House Relax Part 2
var playList11 = playListPrefix + playListId[10];//Deep Delight Part 1
var playList12 = playListPrefix + playListId[11];//Deep House Part 3
var playList13 = playListPrefix + playListId[12];//Truly Deeply House 2
var playList14 = playListPrefix + playListId[13];//Marcels Favorieten 2017
var playList15 = playListPrefix + playListId[14];//Deep House Relax Part 1
var playList16 = playListPrefix + playListId[15];//Deep House Relax 4
var playList17 = playListPrefix + playListId[16];//12inch Classics Part 1

var playList = [playList1,playList2,playList3,playList4,playList5,playList6,playList7,playList8,playList9,playList10,playList11,playList12,playList13,playList14,playList15,playList16,playList17]

var radioUpdate = await Homey.logic.getVariable({ id: '94ad5995-982e-4c55-a45a-59dc66f9521f'}) ;
radioUpdate = radioUpdate.value
console.log('radioUpdate is: '+radioUpdate)

if (radioUpdate === true)
{
var temp = global.get('radio');
console.log('radio list is: ' +temp)
temp.shift();
global.set('radio', temp)
var radioToday = temp[0]
await Homey.logic.updateVariable({id: '14f9e2fe-4ba8-47f8-8505-218107dd5426', variable: { value: radioToday } })
await Homey.logic.updateVariable({id: '94ad5995-982e-4c55-a45a-59dc66f9521f', variable: { value: false } })
console.log('radio list is: ' +temp)

if (radioToday === 'run sonos script again')
{
var temp = new Array()
while (playList.length) 
{
var nummer = playList.length.toString();
console.log('nummer is:' +nummer)
var randomIndex = Math.floor(Math.random() * (playList.length));
console.log('randomIndex is:' +randomIndex)
var randomNumber = playList.splice(randomIndex, 1)[0];
console.log('randomNumber is:' +randomNumber)
temp.splice(randomIndex, 0, randomNumber );
console.log('temp is: '+temp)
global.set('play', temp);
}

var temp = new Array()
while (radio.length) 
{
var nummer = radio.length.toString();
console.log('nummer is:' +nummer)
var randomIndex = Math.floor(Math.random() * (radio.length));
console.log('randomIndex is:' +randomIndex)
var randomNumber = radio.splice(randomIndex, 1)[0];
console.log('randomNumber is:' +randomNumber)
temp.splice(randomIndex, 0, randomNumber );
console.log('temp is: '+temp)
}

var dupIndexes = new Set();
let i = 0;
while(i < temp.length - 1){
if ((temp[i] === temp[i + 1]) && (temp[i + 1] != temp.length - 1 ) || (temp[i].substring(0,77) === temp[i + 1].substring(0,77)) && (temp[i + 1] != temp.length - 1 ))
{
var duplicate = temp[i]
console.log('duplicate is: '+duplicate);
var replace = temp[i + 2]
console.log('replace is: '+replace);
temp.splice(i + 1, 1, replace);
temp.splice(i + 2, 1, duplicate);
}

if ((temp[i] === temp[i + 1]) && (temp[i] === 13 ))
{
var duplicate = temp[i]
console.log('duplicate is: '+duplicate);
var replace = temp[0]
console.log('replace is: '+replace);
temp.splice(i + 1, 1, replace);
temp.splice(0, 1, duplicate);
console.log('temp is de laatste in de array')
}
i++
}
console.log('temp after replacing is: '+temp)

var play = global.get('play');
count = 0
while(count <= 2){
var index = temp.indexOf('play' +(count + 1)); 

if (index !== -1) {
  temp[index] = play[count];
}
count++
}

temp.push('run sonos script again');
global.set('radio', temp);
console.log('temp is: '+temp)
}
var radioToday = temp[0]
await Homey.logic.updateVariable({id: '14f9e2fe-4ba8-47f8-8505-218107dd5426', variable: { value: radioToday } })
await Homey.logic.updateVariable({id: '94ad5995-982e-4c55-a45a-59dc66f9521f', variable: { value: false } })

}

var radioToday = await Homey.logic.getVariable({ id: '14f9e2fe-4ba8-47f8-8505-218107dd5426'}) ;
radioToday = radioToday.value
//console.log('radioToday.substring(0, 45) is: '+radioToday.substring(0, 45))

if (radioToday.substring(0, 45) === 'http://192.168.178.2:5005/woonkamer/favorite/'){
var titel = radioToday.split('/').pop();
console.log('RadioToday is :'   +titel);
await Homey.logic.updateVariable({id: '7ab3144b-9c30-4098-a866-a3fe5558622f', variable: { value: titel } })
}

count = 0
if (radioToday.substring(0, 77) === 'http://192.168.178.2:5005/woonkamer/spotify/now/spotify:user:spotify:playlist')
{
while(count <= playListId.length -1) 
{
if (radioToday.substring(radioToday.length - 22) === playListId[count].substring(0,22))
var titel = playListName[count]
count++
}
await Homey.logic.updateVariable({id: '7ab3144b-9c30-4098-a866-a3fe5558622f', variable: { value: titel } })
console.log('Titel playlist is: '+titel)
}

These are the flows i made for the script: