# Homeyscript Variables and numbers

**URL:** <https://community.homey.app/t/homeyscript-variables-and-numbers/33436>\
**Category:** Developers\
**Created:** [August 10, 2020, 9:54am UTC](https://community.homey.app/t/homeyscript-variables-and-numbers/33436 "2020-08-10T09:54:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 10, 2020, 9:54am UTC](https://community.homey.app/t/homeyscript-variables-and-numbers/33436/1 "2020-08-10T09:54:19Z")

</div>

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 🧐

```
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();
```

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [August 10, 2020, 12:28pm UTC](https://community.homey.app/t/homeyscript-variables-and-numbers/33436/2 "2020-08-10T12:28:01Z")

</div>

```
        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).

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 10, 2020, 1:08pm UTC](https://community.homey.app/t/homeyscript-variables-and-numbers/33436/3 "2020-08-10T13:08:48Z")

</div>

> [@JPe4619](#):
>
> The Tags are only available under “HomeyScript” tags, after running this routine, ones.

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

> [@JPe4619](#):
>
> “CameraLink” is the Variable’s name and res is the Value (String format).

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

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [August 10, 2020, 1:28pm UTC](https://community.homey.app/t/homeyscript-variables-and-numbers/33436/4 "2020-08-10T13:28:41Z")

</div>

> [@Marcel\_Ubels](#):
>
> So there will be a tag available, not a logic variable?

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

> **[JavaScript Tutorial](https://www.w3schools.com/js/default.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

> [@](#):
>
> `console.log(randomNumber.toString() + " " + lengte) ;`

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.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [August 10, 2020, 1:34pm UTC](https://community.homey.app/t/homeyscript-variables-and-numbers/33436/5 "2020-08-10T13:34:02Z")

</div>

> [@JPe4619](#):
>
> 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.

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

```auto
console.log(randomNumber, lengte)

```

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 11, 2020, 10:08am UTC](https://community.homey.app/t/homeyscript-variables-and-numbers/33436/6 "2020-08-11T10:08:04Z")

</div>

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);
```

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [August 11, 2020, 3:22pm UTC](https://community.homey.app/t/homeyscript-variables-and-numbers/33436/7 "2020-08-11T15:22:07Z")

</div>

```
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.

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 11, 2020, 3:36pm UTC](https://community.homey.app/t/homeyscript-variables-and-numbers/33436/8 "2020-08-11T15:36:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [August 11, 2020, 3:44pm UTC](https://community.homey.app/t/homeyscript-variables-and-numbers/33436/9 "2020-08-11T15:44:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 11, 2020, 7:20pm UTC](https://community.homey.app/t/homeyscript-variables-and-numbers/33436/10 "2020-08-11T19:20:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [August 11, 2020, 8:12pm UTC](https://community.homey.app/t/homeyscript-variables-and-numbers/33436/11 "2020-08-11T20:12:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 12, 2020, 9:44am UTC](https://community.homey.app/t/homeyscript-variables-and-numbers/33436/12 "2020-08-12T09:44:26Z")

</div>

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;
```

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [September 3, 2020, 9:14am UTC](https://community.homey.app/t/homeyscript-variables-and-numbers/33436/13 "2020-09-03T09:14:51Z")

</div>

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
```
