Homey Community Forum

Generating a automatic name for a variable

I am retrieving the information for my Solaredge solarpanels with the help of their api. this is the code to retrieve the data for every month this year. what i try to achieve is that this code will work every year so i don’t have to adjust anything besides making the corresponding variables in better logic.

this is the code i have at the moment;

const res1 = await fetch('https://monitoringapi.solaredge.com/site/1590338/energyDetails.json?&timeUnit=MONTH&startTime='+Year+'-01-01%'+Year+':00:00&endTime='+Year+'-12-31%'+Year+':00:00&api_key='+SolarEdgeApiKey);                                                                                                       //Get the 

                                                                                                        //request

if (!res1.ok) {

  throw new Error(res1.statusText);

}

// 2021---Get the body JSON---2021

var body = await res1.json();

var SolarEdgeProduction2021January = (body.energyDetails.meters[0].values[0].value)/1000;

console.log('SolarEdgeProduction2021January is :'+SolarEdgeProduction2021January)

var SolarEdgeProduction2021February = (body.energyDetails.meters[0].values[1].value)/1000;

var SolarEdgeProduction2021March = (body.energyDetails.meters[0].values[2].value)/1000;

var SolarEdgeProduction2021April = (body.energyDetails.meters[0].values[3].value)/1000;

var SolarEdgeProduction2021May = (body.energyDetails.meters[0].values[4].value)/1000;

var SolarEdgeProduction2021June = (body.energyDetails.meters[0].values[5].value)/1000;

var SolarEdgeProduction2021July = (body.energyDetails.meters[0].values[6].value)/1000;

var SolarEdgeProduction2021August = (body.energyDetails.meters[0].values[7].value)/1000;

var SolarEdgeProduction2021September = (body.energyDetails.meters[0].values[8].value)/1000;

var SolarEdgeProduction2021October = (body.energyDetails.meters[0].values[9].value)/1000;

var SolarEdgeProduction2021November = (body.energyDetails.meters[0].values[10].value)/1000;

var SolarEdgeProduction2021December = (body.energyDetails.meters[0].values[11].value)/1000;

//2021---Updating the better logic variables---2021

BLApp.apiPut('SolarEdgeProduction'+Year+'January/'+SolarEdgeProduction2021January.toFixed(2));

BLApp.apiPut('SolarEdgeProduction2021February/'+SolarEdgeProduction2021February.toFixed(2));

BLApp.apiPut('SolarEdgeProduction2021March/'+SolarEdgeProduction2021March.toFixed(2));

BLApp.apiPut('SolarEdgeProduction2021April/'+SolarEdgeProduction2021April.toFixed(2));

BLApp.apiPut('SolarEdgeProduction2021May/'+SolarEdgeProduction2021May.toFixed(2));

BLApp.apiPut('SolarEdgeProduction2021June/'+SolarEdgeProduction2021June.toFixed(2));

BLApp.apiPut('SolarEdgeProduction2021July/'+SolarEdgeProduction2021July.toFixed(2));

BLApp.apiPut('SolarEdgeProduction2021August/'+SolarEdgeProduction2021August.toFixed(2));

BLApp.apiPut('SolarEdgeProduction2021September/'+SolarEdgeProduction2021September.toFixed(2));

BLApp.apiPut('SolarEdgeProduction2021October/'+SolarEdgeProduction2021October.toFixed(2));

BLApp.apiPut('SolarEdgeProduction2021November/'+SolarEdgeProduction2021November.toFixed(2));

BLApp.apiPut('SolarEdgeProduction2021December/'+SolarEdgeProduction2021December.toFixed(2));


Is it possible to automatically change the variables.

I do have some code for automatically get different variables like this for my Sonos system

> var nums = [Radio1,Radio2,Radio3,Radio4,Radio5,Radio6,Radio7,Radio8,Radio9,Radio10,Radio11,
> Radio12,Radio13,Radio14];
> 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( 'First radiostation is: '+randomNumber.toString() + "   " + nummer) ;
>  //       console.log('Nummer: '+nummer)
>  //       console.log('Randomnummer is :'+randomNumber)
>  //       console.log('RadioDay is :'+randomNumber)
> let result=BLApp.apiPut("RadioDay"+nummer+"/" + randomNumber.toString());
> };

Looks like you’re trying to reproduce what [APP] Power by the Hour: Insights per hour, day, month and year does from just one variable using insights. Isn’t it better to use that?

If hou create variables each month, with insights and all, you’ll probably strain storage way more than needed.

You could also consider usning only variables per month and use insigts to track years across months?