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 and there will be no duplicate stations two days in a row. You can adjust the radiostations and playlists.
Start the script in the morning once a day and a new radiostation or playlist will be played on your Sonos device. After two weeks the script will automatically refresh.
You have to install the Sonos API google for jishi/node-sonos-http-api. On a server with Docker You have to search for the chrisns-docker-node-sonos-http-api.
Add your local server’s ipadres and portnumberPrefix in this script
You can obtain the playlistid like this:
copy the next script and run. You can adjust your radiostations, playlist and radionames @ the remarks
// Script for randomly playing your favorite Sonos radiostations and Spotify playlists
// Using a Server with the Sonos Api
// Add your Server's Ipadres and PortnumberPrefix
var ipAdres = '192.168.1.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.
//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
//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 + 'Sunshine live - Chillout';
var radio6 = radioPrefix + 'Deep Radio';
var radio7 = radioPrefix + 'Radio 538 Non-stop';
var radio8 = radioPrefix + 'Deep Dance Radio';
var radio9 = radioPrefix + 'Non-stop - HD';
var radio10 = radioPrefix + 'Slam! Non Stop';
var radio11 = radioPrefix + 'Disco Classic Radio';
var radio =[radio1,radio2,radio3,radio4,radio5,radio6,radio7,radio8,radio9,radio10,radio11];
//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=9146c69bc00e4c43';//Deephouse Delight 2
var playListId2 = '00JuraVC5HSlXCLpYJ4RQx?si=9a842d831d264a36';//360 Dance 1
var playListId3 = '4YKqPMOlV5JJK6XVKKdWJ9?si=597891fa6dd9497a';//Deep House Blend 2
var playListId4 = '7jzMzltNsYhaI5Xfkxe9If?si=z9lIfMQzRVmhmTEqwzvGzQ';//Massive Pop Remixes 1
var playListId5 = '5sInx9KNxVqnONg9EsIptX?si=4ce7def2782b437f';//Dance Classics 1
var playListId6 = '0NhMKI95ujIjriBaRXrIBs?si=2d3190c34859494b';//Dance Pop Remixes 1
var playListId7 = '0nXxaEdtBNILZHvltonbpT?si=819a2c4a2d4a4568';//Dance Pop 1
var playListId8 = '60HqFyCYzr6dxr4OMxDtS7?si=36327292b2264e67';//Truly Deep House & Tech 1
var playListId9 = '1QdAD45sKZQwptkm4VeKmO?si=a0603a2fd9444a34';//360 Dance 2
var playListId10 = '0C66rsP0YemHrh3dUhSszt?si=a31983b9fa3d4348';//Deep House Relax 2
var playListId11 = '4Vaus40H2m6gkwNqhzzril?si=42a7e45bcc734c77';//Deephouse Delight 1
var playListId12 = '7kS4ICEIagMdVKUMAe2Hy1?si=a94b23cd3a614ab5';//Deep House Blend 1
var playListId13 = '7fwbNi5Be9Jh2ljr9b8Z34?si=56710669e8214fd4';//Truly Deep House & Tech 2
var playListId14 = '0Jbf3bmusK0U8a8mct158s?si=05e62078db824b39';//Dance Covers 1
var playListId15 = '4oH8GC0TMvQGlTXlj3luUq?si=08bf6f975de44117';//Deep House Relax 1
var playListId16 = '5clhB0HCTd9E8kkb35cONJ?si=8be3fd3428414bf6';//Deep House Relax 5
var playListId17 = '7GQS3BcOeogh8wKEeA3fN8?si=86605042e024457e';//12inch Classics 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 = 'Deephouse Delight 2';
var playListName2 = '360 Dance 1';
var playListName3 = 'Deep House Blend 2';
var playListName4 = 'Massive Pop Remixes 1';
var playListName5 = 'Dance Classics 1';
var playListName6 = 'Dance Pop Remixes 1';
var playListName7 = 'Dance Pop 1';
var playListName8 = 'Truly Deep House & Tech 1';
var playListName9 = '360 Dance 2';
var playListName10 = 'Deep House Relax 2';
var playListName11 = 'Deephouse Delight 1';
var playListName12 = 'Deep House Blend 1';
var playListName13 = 'Truly Deep House & Tech 2';
var playListName14 = 'Dance Covers 1';
var playListName15 = 'Deep House Relax 1';
var playListName16 = 'Deep House Relax 5';
var playListName17 = '12inch Classics 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]
//Logic variable radioUpdate
var radioUpdate = global.get('radioUpdate');
console.log('1 radioUpdate is: '+radioUpdate);
if(radioUpdate === null )
{
var radioUpdate = false;
global.set(radioUpdate,false)
}
if (radioUpdate === false)//reset to false
{
var temp = global.get('radio');
console.log('2 radio list is: ' +temp)
temp.shift();
global.set('radio', temp);
var radioToday = temp[0];
//Logic variable radioToday
global.set('radioToday', radioToday);
global.set('radioUpdate', true);
if (radioToday.substring(0, radioPrefix.length) === radioPrefix)
{
var title = radioToday.split('/').pop();
console.log('3 radioToday.substring(0,'+radioPrefix.length +') is' +radioToday.substring(0, radioPrefix.length));
console.log('4 radioPrefix is: '+radioPrefix);
console.log('5 title is :' +title);
global.set('title', title);
}
count = 0
if (radioToday.substring(0, playListPrefix.length) === playListPrefix)
{
while(count <= playListId.length -1)
{
if (radioToday.substring(radioToday.length - 20) === playListId[count].substring(0,20))
var title = playListName[count];
console.log('6 title is: '+title);
count++
}
}
if (radioToday === 'run sonos script again')
{
var temp = new Array();
while (playList.length)
{
var nummer = playList.length.toString();
console.log('7 nummer is:' +nummer);
var randomIndex = Math.floor(Math.random() * (playList.length));
console.log('8 randomIndex is:' +randomIndex);
var randomNumber = playList.splice(randomIndex, 1)[0];
console.log('9 randomNumber is:' +randomNumber);
temp.splice(randomIndex, 0, randomNumber );
console.log('10 temp is: '+temp);
global.set('play', temp);
}
var temp = new Array();
while (radio.length)
{
var nummer = radio.length.toString();
console.log('11 nummer is:' +nummer);
var randomIndex = Math.floor(Math.random() * (radio.length));
console.log('12 randomIndex is:' +randomIndex);
var randomNumber = radio.splice(randomIndex, 1)[0];
console.log('13 randomNumber is:' +randomNumber);
temp.splice(randomIndex, 0, randomNumber );
console.log('14 temp is: '+temp);
}
var play = global.get('play');
console.log('15 play is: '+play);
console.log('16 play is: '+play[0]);
console.log('17 play is: '+play[1]);
console.log('18 play is: '+play[2]);
temp.splice(3,0,play[0])
console.log('19 radio is: ' +temp);
temp.splice(6,0,play[1])
console.log('20 radio is: ' +temp);
temp.splice(9,0,play[2])
console.log('21 radio is: ' +temp);
var nums = temp
for (i=nums.length-1; i > 0; i--) {
if(i <= nums.length && nums[i] === nums[i - 1]){
var duplicate = nums[i];
console.log('i is:' +i +' op index '+nums.indexOf());
nums.splice(i,1);
nums.unshift(duplicate)
}
}
var temp = nums
console.log('22 radio is: '+radio);
temp.push('run sonos script again');
global.set('radio', temp);
console.log('23 temp is: '+temp);
}
var radioToday = temp[0];
//Logic variable radioToday
global.set('radioToday', radioToday);
//Logic variable radioUpdate
global.set('radioUpdate', true);
}
var radioToday = global.get('radioToday');
if (radioToday.substring(0, radioPrefix.length) === radioPrefix){
var title = radioToday.split('/').pop();
console.log('24 title is :' +title);
}
count = 0
if (radioToday.substring(0, playListPrefix.length) === playListPrefix)
{
while(count <= playListId.length -1)
{
if (radioToday.substring(radioToday.length - 22) === playListId[count].substring(0,22))
var title = playListName[count];
console.log('25 title is: '+title );
count++
}
global.set('title', title);
}
radio = global.get('radio');
console.log('26 radio is: ' +radio)
title = global.get('title')
console.log('27 '+title);
radioUpdate = global.get('radioUpdate')
console.log('28 radioUpdate is: '+radioUpdate)
var tmpRadio = Array(radio.length).fill(0);
var tmpPlayListId = 0
var count = 0
while(count < radio.length )
{
if (radio[count].includes(radioPrefix))
{
tmpRadio.splice(count,1,'Radio: ' +radio[count].substring(((radioPrefix.length)),radio[count].length));
console.log('29 tmpRadio is: '+tmpRadio)
}
else if(radio[count].includes(playListPrefix))
{
tmpPlayListId = playListId.indexOf(radio[count].substring(((playListPrefix.length)),radio[count].length));
tmpRadio.splice(count,1,'Playlist: '+playListName[tmpPlayListId]);
console.log('30 tmpRadio is: '+tmpRadio)
}
else if(radio[count].includes('run sonos script again'))
{
tmpRadio.splice(count,1,);
console.log('31 tmpRadio is: '+tmpRadio)
}
count++
}
console.log('tmpRadio is: '+tmpRadio)
var radioList = tmpRadio.join('\r\n')
console.log('32 radio is: '+ radio)
console.log('33 radioList is: '+radioList)
await Homey.flow.runFlowCardAction({
uri: 'homey:manager:notifications',
id: 'create_notification',
args: {
text: 'Radio List is:'+ '\r\n' +radioList},
});