Homeyscript globale variabelen ophalen en combineren

Ik heb zes global arrays gemaakt; productionWithMonthAndYear0, productionWithMonthAndYear1, productionWithMonthAndYear2, Year0, Year1, Year2. Ieder jaar komt daar weer een global array bij, vb productionWithMonthAndYear3 en Year3 en daarna productionWithMonthAndYear4 en Year4 etc.

Nu wil ik in een variabele array ongeveer de volgende tekst krijgen op index 0;
Index 0 van global array productionWithMonthAndYear0[0] +‘Kwh’ +‘\t’ +‘\t’ + Index 0 van global array productionWithMonthAndYear1[0] +(Year1[0] - Year0[0] )

op index 1 wil daarna de volgende tekst krijgen:
Index 1 van global array productionWithMonthAndYear0[1] +‘Kwh’ +‘\t’ +‘\t’ +Index 1 van global array productionWithMonthAndYear1[1] +(Year1[1] - Year0[1]

Ik kwam in eerste instantie tot het volgende 01’22(131) KWh 02’22(297) KWh)

Maar het lukt mij niet om bijv Year0 en Year1 in 1 array te krijgen.

Met dit script ben ik aan het spelen en stoeien. Maar als je er te lang na kijkt duizelt het je voor je ogen. Wellicht dat iemand een helder lichtje kan opsteken?

Hellup dus…

function getDaysInMonth(year, month) 
{
return new Date(year, month, 0).getDate();
}

function toMonthName(monthNumber) 
{
const date = new Date();
date.setMonth(monthNumber);

return date.toLocaleString('en-US', 
{
month: 'long',
});
}

const date = new Date();
const currentYear = date.getFullYear();
  console.log('This year is: '+currentYear);
const currentMonth = date.getMonth() + 1;
  console.log('This month is: '+currentMonth);
const currentMonthTwoDigits = ("0" +currentMonth).slice(-2);
  console.log('This Month with two digits: ' +currentMonthTwoDigits);
const daysInCurrentMonth = getDaysInMonth(currentYear, currentMonth);
  console.log('Days in current month: ' +daysInCurrentMonth);
const currentDay = new Date().getDate();
  console.log('What daynumer is is today: ' +currentDay);
const currentDayTwoDigits = ("0" +currentDay).slice(-2);
  console.log('This day with two digits: ' +currentDayTwoDigits);


//fill in yourself these variables
const id = '1590338';//Fill in between ' ' the Id of your SolarEdge
const apiKey = xx;// Fill in between ' 'your Solaredge Api key
const installYear = 2020;//Fill in installation year of your Solar Panels
const installMonth = 7;//Fill in installation month of your Solar Panels
const installMonthTwoDigits = ("0" +installMonth).slice(-2);
  console.log('Instal month with two digits: ' +installMonthTwoDigits);
const installDay = 23;//Fill in installation day of your Solar Panels
const installDayTwoDigits = ("0" +installDay).slice(-2);
  console.log('Instal day with two digits: ' +installDayTwoDigits)
const manualUpdate = false;//Fill in true if you want to retrieve the data for the daylight prognosis
const degeneration = 0.004;// Fill in the SolarDegeneration per year
const latitude = 53.033548;//Fill in latitude in decimal degrees South add a '-' in front of the degrees
const longitude = -5.661103;//Fill in longitude in decimal degrees, For East add a '-' in front of the degrees
monthUpdate = global.get('monthUpdate')//before running this script for the first time make a new script wirh the following sentence: global.set('monthUpdate', false);

var production = [0,0,0,0,0,0,237,814,619,265,177,76,163,297,474,785,803,962,870,702,555,369,155,96,131,297,739,787,915,980,902];


var count = 0

while(count <= (currentYear - installYear))
{
global.set('productionWithMonthAndYear'+count , []);

count++
}



var count = 0
var number = 0

while (((count * 12)) < (Math.ceil(production.length / 12)) * 12)
{
var productionYears = production.slice(count * 12,(count * 12) + 12)
global.set('productionYears' +number , productionYears);
console.log('productionYears' +number +' = : ' +productionYears)


var productionWithMonthAndYear = global.get('productionYears' +number);
global.set('productionWithMonthAndYear' +number , productionWithMonthAndYear);
//console.log('productionWithMonthAndYear' +number +' = : ' +productionWithMonthAndYear)
count++
number++
}

var count = 0
var number = 0
while(count <= (currentYear - installYear))
{

productionWithMonthAndYear = global.get('productionWithMonthAndYear' +number)
productionWithMonthAndYear = productionWithMonthAndYear.map(a => "'" +('' +(installYear + count)).slice(-2) +'(' +a +')');

monthLetters = ['01','02','03','04','05','06','07','08','09','10','11','12']

var count1 = 0
var arrayLength = (productionWithMonthAndYear.length -1)
console.log('array length is: ' +arrayLength)

while(count1 <= arrayLength)
{

productionWithMonthAndYear.splice(count1,1,'' +monthLetters[count1]  +productionWithMonthAndYear[count1]);
global.set('productionWithMonthAndYear' +number , productionWithMonthAndYear);

count1++
console.log(productionWithMonthAndYear)
}

count++
number++

}

var count = 0
while(count <= (currentYear - installYear))
{
var productionWithMonthAndYear = global.get('productionWithMonthAndYear' +count);
console.log('productionWithMonthAndYear'+count +' is : '+productionWithMonthAndYear +' Lengte van deze array is: ' +productionWithMonthAndYear.length )

count++
}



//productionWithMonthAndYear = []
var count = 0

while(count <= (currentYear - installYear))
{
//var productionWithMonthAndYear = global.get('productionWithMonthAndYear' +count);
//var arrayLength = 0
//var arrayLength = (((productionWithMonthAndYear).length)-1)
//  console.log('arrayLength = ' + arrayLength)
var arrayIndex = 0
var temporarily = new Array();


while(arrayIndex <= 11)
{

temporarily.push(productionWithMonthAndYear[arrayIndex] +' KWh' +'\t' +'\t' +productionWithMonthAndYear[arrayIndex + 1] +' KWh)' )//+(productionYears +count[arrayIndex] - productionYears +(count + 1)[arrayIndex]) +')' + '\r\n')

console.log('temporarily is: ' +temporarily)
arrayIndex++
}
count++
}
 

Ik heb al veel vragen gesteld ivm dit script. Dit is war ik nu iedere dag in mijn mail krijg. Het onderste gedeelte met alle cijfers probeer ik nu dus verder te automatiseren zodat ik dat niet meer handmatig steeds moet aanpassen.

Ik heb er geen verstand van maar het heeft niks te maken neem ik aan met de gebruikte tekst in de index regel met KWh of Kwh? Wat eigenlijk kWh moet zijn?
Denk het niet want dat staat nergens in het script.

  • de regel “met degneratie is: 707” ontbreekt een e.

Nee klopt, zal wel de door jou aangedragen wijzigingen doorvoeren.

1 Like

Het vullen van de array lukt nu. Ook het combineren van twee jaren lukt nu, wat nog niet lukt is het opvolgend combineren. Dus index 0 tot 11 heeft de tekst waarbij de uitkomst van het eerste jaar wordt gecombineerd met de uitkomst van het tweede jaar. Ik wil nu ook graag op index 12 tot en met 23 de uitkomst hebben van de vergelijking tussen het tweede en het derde jaar. Daarna als er ook een vierde jaar, vijfde jaar etc in de toekomst komt, moeten die vergelijkingen weer in de volgende indexen komen. Script tot nu is:

function getDaysInMonth(year, month) 
{
return new Date(year, month, 0).getDate();
}

function toMonthName(monthNumber) 
{
const date = new Date();
date.setMonth(monthNumber);

return date.toLocaleString('en-US', 
{
month: 'long',
});
}

const date = new Date();
const currentYear = date.getFullYear();
  console.log('This year is: '+currentYear);
const currentMonth = date.getMonth() + 1;
  console.log('This month is: '+currentMonth);
const currentMonthTwoDigits = ("0" +currentMonth).slice(-2);
  console.log('This Month with two digits: ' +currentMonthTwoDigits);
const daysInCurrentMonth = getDaysInMonth(currentYear, currentMonth);
  console.log('Days in current month: ' +daysInCurrentMonth);
const currentDay = new Date().getDate();
  console.log('What daynumer is is today: ' +currentDay);
const currentDayTwoDigits = ("0" +currentDay).slice(-2);
  console.log('This day with two digits: ' +currentDayTwoDigits);


//fill in yourself these variables
const id = '1590338';//Fill in between ' ' the Id of your SolarEdge
const apiKey = 'xx';// Fill in between ' 'your Solaredge Api key
const installYear = 2020;//Fill in installation year of your Solar Panels
const installMonth = 7;//Fill in installation month of your Solar Panels
const installMonthTwoDigits = ("0" +installMonth).slice(-2);
  console.log('Instal month with two digits: ' +installMonthTwoDigits);
const installDay = 23;//Fill in installation day of your Solar Panels
const installDayTwoDigits = ("0" +installDay).slice(-2);
  console.log('Instal day with two digits: ' +installDayTwoDigits)
const manualUpdate = false;//Fill in true if you want to retrieve the data for the daylight prognosis
const degeneration = 0.004;// Fill in the SolarDegeneration per year
const latitude = 53.033548;//Fill in latitude in decimal degrees South add a '-' in front of the degrees
const longitude = -5.661103;//Fill in longitude in decimal degrees, For East add a '-' in front of the degrees
monthUpdate = global.get('monthUpdate')//before running this script for the first time make a new script wirh the following sentence: global.set('monthUpdate', false);

var production = [0,0,0,0,0,0,237,814,619,265,177,76,163,297,474,785,803,962,870,702,555,369,155,96,131,297,739,787,915,980,902];


var count = 0

while(count <= (currentYear - installYear))
{
global.set('productionWithMonthAndYear'+count , []);

count++
}



var count = 0
var number = 0

while (((count * 12)) < (Math.ceil(production.length / 12)) * 12)
{
var productionYear = production.slice(count * 12,(count * 12) + 12)
global.set('productionYear' +number , productionYear);
console.log('productionYear' +number +' = : ' +productionYear)


var productionWithMonthAndYear = global.get('productionYear' +number);
global.set('productionWithMonthAndYear' +number , productionWithMonthAndYear);
//console.log('productionWithMonthAndYear' +number +' = : ' +productionWithMonthAndYear)
count++
number++
}

var count = 0
var number = 0
while(count <= (currentYear - installYear))
{

productionWithMonthAndYear = global.get('productionWithMonthAndYear' +number)
productionWithMonthAndYear = productionWithMonthAndYear.map(a => "'" +('' +(installYear + count)).slice(-2) +'(' +a +')');

monthLetters = ['01','02','03','04','05','06','07','08','09','10','11','12']

var count1 = 0
var arrayLength = (productionWithMonthAndYear.length -1)
console.log('array length is: ' +arrayLength)

while(count1 <= arrayLength)
{

productionWithMonthAndYear.splice(count1,1,'' +monthLetters[count1]  +productionWithMonthAndYear[count1]);
global.set('productionWithMonthAndYear' +number , productionWithMonthAndYear);

count1++
console.log(productionWithMonthAndYear)
}

count++
number++

}

//var count = 0
//while(count <= 0)
//{
//var productionYearFirst = global.get('productionYear' +count);
//var productionYearSecond = global.get('productionYear' +(count + 1));
//console.log('productionYearFirst is : '+productionYearFirst +' Lengte van deze array is: ' +productionYearFirst.length )
//console.log('productionYearSecond is : '+productionYearSecond +' Lengte van deze array is: ' +productionYearSecond.length )

//var productionWithMonthAndYearFirst = global.get('productionWithMonthAndYear' +count);
//var productionWithMonthAndYearSecond = global.get('productionWithMonthAndYear' +(count + 1));
//console.log('productionWithMonthAndYearFirst is : '+productionWithMonthAndYearFirst +' Lengte van deze array is: ' +productionWithMonthAndYearFirst.length )
//console.log('productionWithMonthAndYearSecond is : '+productionWithMonthAndYearSecond +' Lengte van deze array is: ' +productionWithMonthAndYearSecond.length )

//count++
//}


var count = 0
var arrayIndex = 0
var number = 0
var temporarily = new Array();

while(count <= (currentYear - installYear))
{

var productionYearFirst = global.get('productionYear' +number);
var productionYearSecond = global.get('productionYear' +(number + 1));
//console.log('productionYearFirst is : '+productionYearFirst +' Lengte van deze array is: ' +productionYearFirst.length )
//console.log('productionYearSecond is : '+productionYearSecond +' Lengte van deze array is: ' +productionYearSecond.length )

var productionWithMonthAndYearFirst = global.get('productionWithMonthAndYear' +count);
var productionWithMonthAndYearSecond = global.get('productionWithMonthAndYear' +(count + 1));
//console.log('productionWithMonthAndYearFirst is : '+productionWithMonthAndYearFirst +' Lengte van deze array is: ' +productionWithMonthAndYearFirst.length )
//console.log('productionWithMonthAndYearSecond is : '+productionWithMonthAndYearSecond +' Lengte van deze array is: ' +productionWithMonthAndYearSecond.length )


while(arrayIndex <= 11)
{

temporarily.push(productionWithMonthAndYearFirst[arrayIndex] +' kWh' +'\t' +'\t' +productionWithMonthAndYearSecond[arrayIndex] +' kWh(' +(productionYearSecond[arrayIndex] - productionYearFirst[arrayIndex]) +')' + '\r\n')

//console.log(''+temporarily[arrayIndex])
arrayIndex++

}
global.set('result' +number , temporarily);
result = global.get('result' +number)
console.log('Result'+number +' is ' +result)

number++
count++
}