Homey Community Forum

Homeyscript and storage of variables

Many thx, works here

When I try this script i do not get the array, i am doing something wrong, do you have any idea what?

const SolarEdgeId = 'xxxxxx';  //Fill in between ' ' the Id of your SolarEdge

const SolarEdgeApiKey = 'xxxxxxxxxxx'// Fill in between ' 'your Solaredge Api key

var Today = new Date();

var Year = Today.getFullYear();

var Month = Today.getMonth();

var Day = Today.getDate();

var Hours = Today.getHours();

var GetDaysInMonth = function(month,year)

{

return new Date(year, month, 0).getDate();

};

var CountTwoNumbers = ('0' +CountTwoNumbers).slice(-2);

var Production202000,Production202001,Production202002,Production202003,Production202004,Production202005,Production202006,Production202007, Production202008, Production202009, Production202010, Production202011

//var Production2020 = [Production202007, Production202008, Production202009, Production202010, Production202011]

var Production2020 = [Production202000,Production202001,Production202002,Production202003,Production202004,Production202005,Production202006,Production202007,Production202008,Production202009,Production202010,Production202011]

//Get the request

//if(2020 === 2020){

const res = await fetch('https://monitoringapi.solaredge.com/site/'+SolarEdgeId+'/energyDetails.json?&timeUnit=MONTH&startTime=2020-01-01%2000:00:00&endTime=2020-12-31%2023:59:59&api_key='+SolarEdgeApiKey);

//Get the //request

if (!res.ok) {

  throw new Error(res.statusText);

}

var body = await res.json();

Count = 0;

CountTwoNumbers = 0

console.log('Production2020.length is: '+Production2020.length)

while(Count <  Production2020.length)

{

var CountTwoNumbers = ('0' +CountTwoNumbers).slice(-2);

Production2020[CountTwoNumbers] = (body.energyDetails.meters[0].values[Count].value/1000)

console.log('Production2020' +CountTwoNumbers +' is: ' +Production2020[CountTwoNumbers])

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

Count++

CountTwoNumbers++

}

global.set('Production2020',[Production202000,Production202001,Production202002,Production202003,Production202004,Production202005,Production202006,Production202007,Production202008,Production202009,Production202010,Production202011])

Production2020 = global.get('Production2020')

console.log('Production2020 is: ', Production2020,Array.isArray(Production2020.value))

This is the outcome of my script:

Production2020.length is: 12

Production202000 is: NaN

CountTwoNumbers is: 00

Production202001 is: NaN

CountTwoNumbers is: 01

Production202002 is: NaN

CountTwoNumbers is: 02

Production202003 is: NaN

CountTwoNumbers is: 03

Production202004 is: NaN

CountTwoNumbers is: 04

Production202005 is: NaN

CountTwoNumbers is: 05

Production202006 is: 237.37

CountTwoNumbers is: 06

Production202007 is: 814.389

CountTwoNumbers is: 07

Production202008 is: 619.243

CountTwoNumbers is: 08

Production202009 is: 265.006

CountTwoNumbers is: 09

Production202010 is: 176.813

CountTwoNumbers is: 10

Production202011 is: 76.603

CountTwoNumbers is: 11

Production2020 is: [ null, null, null, null, null, null, null, null, null, null, null, null ] false

β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” :white_check_mark: Script Success

:leftwards_arrow_with_hook: Returned: undefined

There are some fundamental issues with your script :grimacing:

For instance, you’re declaring Production2020 as an array:

var Production2020 = [ … ]

But you’re treating it as an object by using zero-padded strings:

Production2020[CountTwoNumbers] = …

You also seem to be assuming that this last line will create/update your variables (Production202000, Production202001, …), which it won’t.

And lastly, I don’t know what .value is doing here:

Array.isArray(Production2020.value)

Perhaps a quick fix would be to use this:

Production2020[Number(CountTwoNumbers)] = (…);

This:

global.set('Production2020', Production2020);

And this:

console.log('Production2020 is: ', Production2020,Array.isArray(Production2020));
1 Like

Robert many thx, this is the solution. Thx for your patience. And as you would have guessed by now. I am mr. Copy/Paste. I try to solve my scripting problems, but i am not all over into JS