Homey script een gedeelte van een array optellen

I probeer een gedeelte van een array op te tellen. De bedoeling is dat hij de eerste 12 velden optelt en dit in een nieuwe array zet, daarna moet hij de volgende 12 velden optellen etct etc.

Hij moet stoppen met tellen als het de variable END boven de lengte van de array uitkomt.

Inmiddels bonkt mijn hoofd van de warmte en het denken, maar ik loop vast, hij telt 2x keer op maar stopt dan, terwijl er nog een derde keer kan worden geteld. Iemand een idee voor de deze NO- Js script kenner?

global.set('SolarProduction', [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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);//Making a global array Production 2020
var SolarProduction = global.get('SolarProduction');
var SolarProductionYear = []

for (var begin = 0; begin <=11;begin++){
for (var end =11; end <=23;end++){
var end = begin+11, sum = 0

sum = SolarProduction.slice(begin, end+1).reduce((a,b)=>a+b,0);
SolarProductionYear.push(sum);

console.log('Totaal eerste jaar is: ' +sum);
console.log('Begin is: ' +begin);
console.log('End is: ' +end);
console.log('Totaal eerste jaar is: ' +sum);
console.log('SolarProduction lengte is: ' +SolarProduction.length);
begin = begin+12
if (end > SolarProduction.length){
break;

}
}}
while (SolarProduction.length) {
  SolarProductionYear.push(
    SolarProduction.splice(0, 12).reduce((sum, value) => sum + value, 0)
  );
}

Hij logt alleen niet de tussenstappen.

1 Like

Mooie oplossing, en het lijkt dan zo eenvoudig dat Ik er niet op kom. Werkt goed. Ik kan weer verder met het script. Dit verkort het script weer een groot stuk. Thx

ik zit compleet vast. De global.set en global.get resulteert hier in een lege global array dayLightMonth. De intentie is dat de array wordt gevuld met het aantal zonneminuten. voor de test 3 dagen, in realiteit een maand aan zonneminuten in de array. kan jij helpen Robert?

//Setting daylight minutes for a month
if (currentDay === 1 && MonthUpdate === true || MonthUpdate === true || manualUpdate === true)
{
const process = this.constructor.constructor('return process')();//workaround voor de onbeveiligde sunrise sunset site
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// the api call is from a unsecured website, therefore a workaround to be able to retrieve the data

await Homey.logic.updateVariable({id: '69a53466-a58b-4b54-85f3-140e99eaa08f', variable: { value: false }})//Logic SolarMonthUpdate op yes zetten
global.set('dayLightMonth', []);//empty an array
var dayLightMonth =  global.get('dayLightMonth')

var count = 1
while(count <= 3)
{
    console.log('count is: ' +('0'+count).slice(-2)) 
url = ('https://api.sunrise-sunset.org/json?lat='+latitude +'&lng='+longitude +'&date=' + currentYear + '-' + currentMonth + '-' +count +'&formatted=0');
response = await fetch(url);
apiSunriseSunset = await response.json();
global.set('dayLightMonth', dayLightMonth.splice((count -1),0,apiSunriseSunset.results.day_length));
//var dayLightMonthTotalMinutes = dayLightMonth.reduce((partialSum, a) => partialSum + a, 0);//Sum up the array
//    console.log('Total dayLightMonthTotalminutes day ' +count +' = '   +dayLightMonthTotalMinutes);

count++;

await wait(100);
}
}

var dayLightMonth =  global.get('dayLightMonth');
console.log('daylightMonth is :'+dayLightMonth)

Better use slice !

global.set('dayLightMonth', dayLightMonth.slice((count -1),apiSunriseSunset.results.day_length));

Still gives a “0”.

I want to add it to an array. i tried push, splice etc etc, but i am doing something wrong

This is the solution, had to make a local array, then retrieving the data
and afterwards setting the global array

//Setting daylight minutes for a month
if (currentDay === 1 && MonthUpdate === true || MonthUpdate === true || manualUpdate === true)
{
const process = this.constructor.constructor('return process')();//workaround voor de onbeveiligde sunrise sunset site
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// the api call is from a unsecured website, therefore a workaround to be able to retrieve the data

await Homey.logic.updateVariable({id: '69a53466-a58b-4b54-85f3-140e99eaa08f', variable: { value: false }})//Logic SolarMonthUpdate op yes zetten
global.set('dayLightMonth', []);//empty an array
var dayLightMonth = []

var count = 1
while(count <= daysInCurrentMonth)
{
    console.log('count is: ' +('0'+count).slice(-2)) 
url = ('https://api.sunrise-sunset.org/json?lat='+latitude +'&lng='+longitude +'&date=' + currentYear + '-' + currentMonth + '-' +count +'&formatted=0');
response = await fetch(url);
apiSunriseSunset = await response.json();

dayLightMonth.splice(count -1,1,apiSunriseSunset.results.day_length);

count++;

await wait(100);
}
}
dayLightMonth = global.set('dayLightMonth', dayLightMonth)
var dayLightMonth =  global.get('dayLightMonth');
console.log('daylightMonth is :'+dayLightMonth)
var dayLightMonthTotalMinutes = dayLightMonth.reduce((partialSum, a) => partialSum + a, 0);//Sum up the array
    console.log('Total dayLightMonthTotalminutes = '   +dayLightMonthTotalMinutes);