Homeyscript for beginners using ChatGPT

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.

4 Likes

The wide knowledge of chatGPT is insane. I’m even sure not only beginner but also professionals will benefit from it. Just because chatGPT can write things 10 times faster than you could ever do. Or use it parallel, like “Write a function that do this with that input” and already work on the implementation of the function while the function is being generated.
Or you can do things like this a developer on the Homey Slack did:

In my experience it works best if you don’t try to write a complete complex program at once. Just part it up in sub-problems and try to solve them first. If every bit of the puzzle is complete, tie it all together.

Not everything will work at once, but you can tell ChatGPT that there’s an error and past the error after that. Most of the time it will fix the problem.


Just be careful you do not give it too much information :crazy_face::

2 Likes

Probably this AI robot was designed in the Terminator days. But even for me those were the good old days(i think🙏).

And indeed i first tried it with sub-problems and adjusted them. But it was probably a busy day and i got a lot of error reports. After a while i could shorten my question, which reduced it to one question.

You have to be precise, because at first i got a python script.

1 Like

Someone here asked for help to translate a code written initially in Lua into Homey script. I did put his code into ChatGPT and asked exactly that. The result was a Homey script. Either would have worked or not, (I am not a programmer) it’s another story but I wouldn’t be surprised to have worked with some corrections, whatever those had to be.

Nice, i had also thought of trying that. But when i looked into the LUA code, i thought this will not work.

I tried also the translation from the LUA script into a Homey script with ChatGPT. Just out of interest.
Out of 4 attempts, 3 were aborted, and all scripts looked quite different. Since I have no programming knowledge, I don’t know nearly which of the scripts would work.

That’s also my experience. What does help is start a new chat. Also trying to make sub- questions(adjust this script and ……) does help a lot. But at this point it sometimes is a bit annoying.

I also get often different answers on the same question. But most of the examples worked. But you have to be very specific. “Write a homeyscript for homey” works, write a homeyscript not. Use existing array playList doesn’t work(it refers in that case to homey.manager), but make an array called playList works better. You only have to delete the lines which make this playList.

Make the variable global doesn’t work. But add global.get playList at the end of the script works.

So it is trial and error. But in the end i came up with a rather nice script.

Apparantly ChatGPT is learning and learning. This is my answer:

Write a homeyscript for homey and use the netatmo with device id a9f10bce-e737-4c0f-9c0f-d1d9c8c92d5f and retrieve with await netatmoDevice.capabilitiesObj[‘measure_rain.24h’].value. Retrieve the data once a day and store the data in a array that contains all the days in a month

// Get the current date
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1; // add 1 since getMonth() returns 0-indexed month

// Get the Netatmo device with the given ID
const netatmoDevice = await Homey.devices.getDevice({ id: 'a9f10bce-e737-4c0f-9c0f-d1d9c8c92d5f' });

// Get the 24-hour rain data
const rain24h = await netatmoDevice.capabilitiesObj['measure_rain.24h'].value;
console.log(rain24h)
// Get the array of saved data for the current month, or create a new one if it doesn't exist
const monthData = global.get('rain24h_' + year + '_' + month) || [];

// Check if data has already been saved for the current day
const day = now.getDate();
const savedRain24h = monthData[day];

if (savedRain24h === undefined || savedRain24h !== rain24h) {
  // If data has not been saved for the current day, or if it has changed, save the new data
  monthData[day] = rain24h;
  global.set('rain24h_' + year + '_' + month, monthData);
}

This is a nice script because it automatically decided tot add the global variable instead of a local variable. It does have problems with the capabilities, so that one i had to search myself for.