Homeyscript Variables and numbers

I have found a little script which randomize given numbers. Each number I want in a variable. Logic or Better Logic. So I have 14 numbers and want these in 14 variables. How can I achieve this. Yes I have searched the forum but got lost :face_with_monocle:

var nums = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14"];
    var usedNums = [];
    while (nums.length) {
      var randomIndex = Math.floor(Math.random() * (nums.length - 1));
      var randomNumber = nums.splice(randomIndex, 1)[0];
      console.log(randomNumber.toString());
      usedNums.push(randomNumber);
    };
    console.log();
        var nums = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14"];
   var usedNums = [];
    while (nums.length) {

        var lengte = nums.length.toString();

        var randomIndex = Math.floor(Math.random() * (nums.length - 1));
        var randomNumber = nums.splice(randomIndex, 1)[0];
        console.log(randomNumber.toString() + "   " + lengte) ;

        await setTagValue("MyNumber"+lengte, {type: "string", title: "Mynumber"+lengte}, randomNumber.toString());

      usedNums.push(randomNumber);
    };

The Tags are only available under “HomeyScript” tags, after running this routine, ones.
For BetterLogic, you must define the variables first in BL and make a list of these variables in this routine, use the next lines for BetterLogic:

let BLApp = await Homey.apps.getApp({id:"net.i-dev.betterlogic" });
let result=BLApp.apiPut("CameraLink/"+res);

“CameraLink” is the Variable’s name and res is the Value (String format).

1 Like

So there will be a tag available, not an logic variable?

Res is string, what is number and boolean? Or where can I find these answers/description

No, not with this example, not sure if that is possible.
You can use the BetterLogic example for that.

example:
to do a console.log, the data must be in String format,
randomNumber is a Number, so randomNumber.toString() is converting the number to a String format.

A boolean is a binary variable, it can only be Yes or No resp. True/False.

console.log() accepts any JS object (including Number instances) as arguments. This also works:

console.log(randomNumber, lengte)
2 Likes

Trying to adjust the script as suggested, but does not work at the moment. Got it working with Tags, but not with Better Logic. Can someone take a look?

var nums = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14"];
   var usedNums = [];
    while (nums.length) {

        var lengte = nums.length.toString();

        var randomIndex = Math.floor(Math.random() * (nums.length - 1));
        var randomNumber = nums.splice(randomIndex, 1)[0];
        console.log(randomNumber, lengte)

      usedNums.push(randomNumber);
    };
let BLApp = await Homey.apps.getApp({id:"net.i-dev.betterlogic" });
let result=BLApp.apiPut('RadioDag1/'+nums, 'RadioDag2/'+nums, 'RadioDag3/'+nums, 'RadioDag4/'+nums, 'RadioDag5/'+nums, 'RadioDag6/'+nums, 'RadioDag7/'+nums, 'RadioDag8/'+nums, 'RadioDag9/'+nums, 'RadioDag10/'+nums, 'RadioDag11/'+nums, 'RadioDag12/'+nums, 'RadioDag13/'+nums, 'RadioDag14/'+nums);
var nums = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14"];
var usedNums = [];
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 - 1));
        var randomNumber = nums.splice(randomIndex, 1)[0];
        console.log(randomNumber.toString() + "   " + nummer) ; //+ "  " + nums) ;
//        await setTagValue("MyNumber"+nummer, {type: "string", title: "Mynumber"+nummer}, randomNumber.toString());
        let result=BLApp.apiPut("RadioDag"+nummer+"/" +  randomNumber.toString());
//        usedNums.push(randomNumber);
    };
//    console.log(usedNums);
    return true;

You have to create the 14 variables in BetterLogic yourselves ( they must be called RadioDag1 up to RadioDag14 ). ( I replaced the variable lengte by nummer, looks more clear for me.)

Rem: “return true;” makes it possible to place the routine in the AND column.

1 Like

Thx help is much appreciated. Just started with JS. Has at this moment many many secrets for me. But i am learning. Thx👍

Same for me, maybe I’m only half a step before you, but also have a lot to learn, that’s why I like these cases, and find solutions. Also very nice is, that @robertklep is always helpful and correcting when I make mistakes.

Works almost great. Just one item is not working. The numbers are all random but one. Number 14 is always assigned tot variable RadioDag1. The other numbers are random assigned. Any clue why this is happening?

Replace (nums.length - 1) by (nums.length) and try again. ( this was the code you delivered :wink:)

1 Like

Works perfect. Many many thx. Hearby the complete script, with some remarks for me to remember how this works as a reminder(in Dutch).

// Radiozenders in Better Logic Variabelen zetten
// Zet bij var nums de invoer voor de variabelen, als je cijfers gebruikt
// maak je in Better Logic een nummer variabele aan, als je woorden gebruikt maak 
// je in better logic een string variabele aan

// Invoer van de variabelen. Gebruik cijfers of letters
var nums = ["538 Ibiza","Deep House Ibiza","538 Dance Radio","Dance FM","538 Global Dance Chart","Deep FM","Dance Pop Remixes","Disco Radio Action","Our House","Our House","Deep House Relax 3","Sky Radio","Qmusic",'Slam!'];
var usedNums = [];

// Roep Better Logic aan
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());
 
 // Benoemen van de variabele in Better Logic, de toevoeging +nummer+"/" zet een nummer 
 // achter de variabele "Radiodag". De varaiabele moet exact overeenkomen. Dus hoofdletter
 // gevoelig
        let result=BLApp.apiPut("RadioDag"+nummer+"/" +  randomNumber.toString());
//        usedNums.push(randomNumber);
    };
//    console.log(usedNums);

// Mogelijk maken om de routine aan te roepen in een AND flow
    return true;
1 Like

I am trying to adjust the script a little bit. What I want is randomly put 17 Spotify Playlists to 3 better logic variables, called PlayListDay1 PlayListDay2 and PlayListDay3. After that I want to add the content off the 3 playlist variables PlayListDay1, PlayListDay2, PlaylistDay3 towards the variables Radio12, Radio13 and Radio14
Then put 11 radiostations towards 11 better variables. Called Radio1, Radio 2 …Radio11. After this i want the content off Radio1 - Radio14 randomly set to the better Logic variables RadioDay1 - RadioDay14.

Hope it makes some sense.

This is what I made untill now

   // Script for randomly playing radiostations and playlists from Spotify with your Sonos Device
// With the use of a Synology Server and the Sonos Docker Api 
// Using Homey's apps Better Logic and HTTP request


//Prefix voor radiostations en playlists
//Add your local ip adres at IPadres en your Portnumber at Portnumber between the quotes
const IpAdres = '192.168.178.2'; 
const Portnumber = '5005';
const RadioPrefix = 'http:%2F%2F' + IpAdres + ':' + Portnumber + '%2Fwoonkamer%2Ffavorite%2F';
const PlayListPrefix = 'http:%2F%2F'+ IpAdres + ':'+ Portnumber + '%2Fwoonkamer%2Fspotify%2Fnow%2Fspotify:user:spotify:playlist:';

//Add your station at //Radiostations. In your Sonos app, from Sonos(not Homey) you
//need to add your radiostation as favorite. Use this exact name. In this script
// I have added three playlists. Therefore the variable names Radio12, Radio13 en Radio14 must be
//stay the same and not been altered. 

//To add some random playlist I have made 17 playlist which you can adjust. Just search the ID 
//off your playlist in your Spotify App(not Homey) and paste this ID at //PlayList between 
//the quotes

// Add 14 string variables in Better Logic, called RadioDay1, RadioDay2.....Radioday3 and three extra
//string variables called PlayListDay1, PlayListDay2 and PlayListDay3. The names are capital sensitive.

//Radiostations  Add the name off your Sonos favorite between the quotes
const Radio1 = RadioPrefix + '538 Ibiza Radio';
const Radio2 = RadioPrefix + 'Deep House Ibiza';
const Radio3 = RadioPrefix + '538 Dance Radio';
const Radio4 = RadioPrefix + 'Dance FM';
const Radio5 = RadioPrefix + '538 Global Dance Chart';
const Radio6 = RadioPrefix + 'Deep Radio';
const Radio7 = RadioPrefix + 'Disco Radio Action';
const Radio8 = RadioPrefix + 'Our House';
const Radio9 = RadioPrefix + 'Sky Radio 101 FM';
const Radio10 = RadioPrefix + 'Qmusic';
const Radio11 = RadioPrefix + 'Slam!';
//const Radio12 = 'Playlist1';//Do not adjust this variable
//const Radio13 = 'Playlist2';//Do not adjust this variable
//const Radio14 = 'Playlist3';//Do not adjust this variable

//Playlist. Ad the ID of your Spotify playlist between the quotes. For own needs you can add the 
//name of the playlist after the //

const PlayList1 = PlayListPrefix + ‘7GQS3BcOeogh8wKEeA3fN8?si=taswh15eRZuXmFAkyn6IfQ’//12inch Classics Part 1

const PlayList2 = PlayListPrefix + ‘1C5F8dA3nWRN46u6lCehqm?si=67RdCbXtSG27gx6C7qP2Sw’//Deep Delight Part 2

const PlayList3 = PlayListPrefix + ‘1QdAD45sKZQwptkm4VeKmO?si=_5P2-G9-SZG91WsNHt0f4g’//360 Dance Part 2

const PlayList4 = PlayListPrefix + ‘4YKqPMOlV5JJK6XVKKdWJ9?si=SL7iH53eSNu-PnsZJIcr6A’//Deep House Part 2

const PlayList5 = PlayListPrefix + ‘7jzMzltNsYhaI5Xfkxe9If?si=IwDTD4ylQJC9c5JWxEUXQw’//Massive Pop Remixes Part 1

const PlayList6 = PlayListPrefix + ‘5sInx9KNxVqnONg9EsIptX?si=8T0QWluxSDqI3bClrf0OOQ’//Dance Classics Part 1

const PlayList7 = PlayListPrefix + ‘0NhMKI95ujIjriBaRXrIBs?si=pG_J4OXGTz6mhN_kG1mvMg’//Dance Pop Remixes Part 1

const PlayList8 = PlayListPrefix + ‘1s4YUBb95LaWazpgvNGzAu?si=35wEzzqcQIKAgSZq7bkEaA’//Deep House Relax Part 3

const PlayList9 = PlayListPrefix + ‘60HqFyCYzr6dxr4OMxDtS7?si=x7Oa5bNBQXy4DQlC2COLaw’//Truly Deeply House Part 1

const PlayList10 = PlayListPrefix + ‘00JuraVC5HSlXCLpYJ4RQx?si=LJFWf4UoQ-2DtayVAGb26g’//360 Dance Part 1

const PlayList11 = PlayListPrefix + ‘0C66rsP0YemHrh3dUhSszt?si=gdtcDlzURLWQVZCKi8Evkw’//Deep House Relax Part 2

const PlayList12 = PlayListPrefix + ‘4Vaus40H2m6gkwNqhzzril?si=6y9qg0HxQAqfB_B3bhfeIQ’//Deep Delight Part 1

const PlayList13 = PlayListPrefix + ‘0nXxaEdtBNILZHvltonbpT?si=4IxGc-mzQ7OGoFKCPGD2vA’//Dance Pop Part 1

const PlayList14 = PlayListPrefix + ‘3xDZn9OQLBxJBtWYSfNO3p?si=9C5nZsdZT7Kd3knE2KcngA’//Marcels Favorieten 2018 en 2019

const PlayList15 = PlayListPrefix + ‘28MJ5Ws6Hw7K3sM0buLxvS?si=sR9304GXTzikRt1n3so-MQ’//Marcels Favorieten 2017

const PlayList16 = PlayListPrefix + ‘4oH8GC0TMvQGlTXlj3luUq?si=RnmzYP4YRCWSPa9ptJTuRA’//Deep House Relax Part 1

const PlayList17 = PlayListPrefix + ‘6tN0M7JUSAUnPbqe4cPz8I?si=juDi1DjuQjms0iBzMowMag’//Marcels Favorieten 2016

var PlayListMake = [PlayList1,PlayList2,PlayList3,PlayList4,PlayList5,PlayList6,PlayList7,
PlayList8,PlayList9,PlayList10,PlayList11,PlayList12,PlayList13,PlayList14,PlayList15,
PlayList16,PlayList17];
var usedNums = [];

let BLApp = await Homey.apps.getApp({id:"net.i-dev.betterlogic" });
    while (PlayListMake.length) {
        var number = PlayListMake.length.toString();
        var randomIndex = Math.floor(Math.random() * (PlayListMake.length));
        var randomNumber = PlayListMake.splice(randomIndex, 1)[0];
        console.log(randomNumber.toString() + "   " + number) ;
        
let result=BLApp.apiPut("PlayListDay"+number+"/" + randomNumber.toString());
};



let PlayListDay1=await BLApp.apiGet('PlayListDay1');
let Radio12 = PlayListDay1;
let PlayListDay2=await BLApp.apiGet('PlayListDay2');
let Radio13 = PlayListDay2;
let PlayListDay3=await BLApp.apiGet('PlayListDay3');
let Radio14 = PlayListDay3;

var nums = [Radio1,Radio2,Radio3,Radio4,Radio5,Radio6,Radio7,Radio8,Radio9,Radio10,Radio11,
PlayListDay1,PlayListDay2,PlayListDay3];
var usedNums = [];

//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) ;
        
let result=BLApp