As i am not a developer and have a low knowledge of Javascript i got a tip from @Satoer. With ChatGPT you can ask for a script. Open a browser and goto ChatGPT.
Here you can ask what to do. In this example i tried to make a script that shuffles my radiolist and playlist, combine these two with three playlists and sort them that way no radiostation plays two days in a row an no playlist plays two times in a row.
My question was:
Write a homeyscript for Homey and shuffle the array playList and shuffle the array radio. Make a new array called radioPlay and add the array radio and add index 1 from playList on index 3 and add index 2 from playList on index 7 and add index 3 from playList on index 10. Adjacent duplicates must get a different index. Add the line global.set at the end of the script.
The answer was:
// Shuffle the playList and radio arrays
let playList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let radio = ['station1', 'station2', 'station3', 'station4'];
playList = shuffleArray(playList);
radio = shuffleArray(radio);
// Create the radioPlay array
let radioPlay = [];
for (let i = 0; i < radio.length; i++) {
radioPlay.push(radio[i]);
if (i === 3) {
radioPlay.push(playList[1]);
} else if (i === 7) {
radioPlay.push(playList[2]);
} else if (i === 10) {
radioPlay.push(playList[3]);
}
}
// Function to shuffle an array
function shuffleArray(arr) {
let newArr = arr.slice();
for (let i = newArr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[newArr[i], newArr[j]] = [newArr[j], newArr[i]];
}
return newArr;
}
// Set the global variable
global.set('radioPlay', radioPlay);
in this case i only had to make some minor adjustments to make it work.
This i my final script. The most difficult part has been doen with the help of ChatGPT
// 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 radioPlay = global.get('radioPlay');
console.log('2 radio is: ' +radioPlay)
radioPlay.shift();
console.log('3 radio is: ' +radioPlay)
global.set('radioPlay', radioPlay);
var radioToday = radioPlay[0];
//Logic variable radioToday
global.set('radioToday', radioToday);
global.set('radioUpdate', true);
if (radioPlay.length === 0)
{
// Shuffle the playList and radio arrays
playList = shuffleArray(playList);
radio = shuffleArray(radio);
// Create the radioPlay array
let radioPlay = [];
for (let i = 0; i < radio.length; i++) {
radioPlay.push(radio[i]);
if (i === 3) {
radioPlay.push(playList[1]);
} else if (i === 7) {
radioPlay.push(playList[2]);
} else if (i === 10) {
radioPlay.push(playList[3]);
}
}
// Function to shuffle an array
function shuffleArray(arr) {
let newArr = arr.slice();
for (let i = newArr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[newArr[i], newArr[j]] = [newArr[j], newArr[i]];
}
return newArr;
}
// Set the global variable
global.set('radioPlay', radioPlay);
}
}
var radioPlay = global.get('radioPlay');
console.log('radioPlay is: ' +radioPlay)
var tmpRadio = Array(radioPlay.length).fill(0);
var tmpPlayListId = 0
var count = 0
while(count < radioPlay.length )
{
if (radioPlay[count].includes(radioPrefix))
{
tmpRadio.splice(count,1,'Radio: ' +radioPlay[count].substring(((radioPrefix.length)),radioPlay[count].length));
console.log('29 tmpRadio is: '+tmpRadio)
}
else if(radioPlay[count].includes(playListPrefix))
{
tmpPlayListId = playListId.indexOf(radioPlay[count].substring(((playListPrefix.length)),radioPlay[count].length));
tmpRadio.splice(count,1,'Playlist: '+playListName[tmpPlayListId]);
console.log('30 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},
});
It really depends how you ask your question and ChatGPT does give errors when a lot of people are online. But it does make homeyscript a lot easier. With minor knowledge of javascript you get great results.
I am now thinking of the next question:
Write a homey app with SDK3 and give me the measurements from my electricitymeter . Buy me a coffee if you like my app.
With the knowledge i know have i am almost certain i will get all the coffee i ever need.