How to multiply two arrays (one for energy and one for price) to calculate cost energy?


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.

Anyone can help me?. Thanks.

Like this?

var arrayEnergy =;
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;
    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 and msg.price in one message

Thanks a lot Syntox.