I store in InfluxdB one array with the energy of my system by kwh at each hour) and one array with the price in € each kwh.
To calculate the cost I need to read the data, and I have two arrays, but I don’t know how I can multiply for each index of arrays the energy and price and finally calculate the total cost.
var arrayEnergy = msg.energy;
var arrayPrice = msg.price;
var pricePerHour = [];
var minLength = 0;
var totalCost = 0;
// only use length of shortest array so we don't end up multiplying x*none
if (arrayEnergy.length <= arrayPrice.length){
minLength = arrayEnergy.length;
}
else{
minLength = arrayPrice.length;
}
// multiply corresponding indexes of each array
for(var i=0; i<minLength; i++) {
pricePerHour[i] = arrayEnergy[i] * arrayPrice[i];
}
// add all entrys in pricePerHour together
pricePerHour.forEach(entry => totalCost += entry)
// set output message
msg = {"pricePerHour":pricePerHour, "totalCost":totalCost}
return msg;
You have to pass in the two arrays of msg.energy and msg.price in one message