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

I know this is a year old, but it was very useful for me to read through.
After some testing my take is that it’s possible to store arrays as variables in Homeyscript and pick these up elsewhere, but it’s not possible to store arrays in the “variables” available from flows (unless using the script). Am I correct?

Reason for me exploring this is that I want to store some data, to be used later, and I’m unsure how low the global-variable is available and when it’s reset/removed. Anyone who can answer this? (I’m guessing it’s a js thing, and that it might reset when closing browser???)

As far as i know( and using them for some time) global variables don’t disappear. You can use them at any moment you want. I store them for my solar panel script for a few years( and for as long as global variables were enabled), and all data is available.

To access your data you need homeyscript, or you use homeyscript from within a flow. You can store your data global or in tags or in variables in Logic. I use quite often tags. My script puts data in a tag, and a flow uses this tag and data for something else.

1 Like

Thank you Marcel. That is great news.
Do you know if it’s possible to export data out to a file somehow (as a backup).

Currently I’m working on data on prices per hour. Next I need to figure out if I’m able to pick up data from my panels. I’ve got growatt

I suppose export is possible. Haven’t tried it yet, but is on my to do list. I have an article about homey with an export solution with google sheets. I have to take a look at it. But seems to be possible. Edit: I found the article but it is In Dutch language Integration Homey and Google sheets

PBTH has a integration with prices per hour.