heatingpro-efficiency
Version:
Utility functions for MonitoringPro efficiency calculations with flexible unit support (GJ, kWh, MWh)
79 lines (61 loc) • 2.17 kB
JavaScript
const {
hasPropertiesAndNotDash,
toFloatWithDot,
convertHeatToKwh,
} = require("./utils/index.js");
const computeEfficiency = (
row,
prevRow,
monthlyEffectivityConstant,
fieldUnits = {}
) => {
// Helper function to convert MWh to GJ if needed
const convertValue = (value, fieldName) => {
if (fieldUnits[fieldName] === "mwh") {
return value * 3.6; // Convert MWh to GJ
}
return value;
};
let sumOfDiffs = 0;
for (let i = 1; i <= 8; i++) {
const voKey = `VO${i}`;
if (hasPropertiesAndNotDash(row, prevRow, voKey)) {
const currentVal = toFloatWithDot(row[voKey]);
const prevVal = toFloatWithDot(prevRow[voKey]);
// Convert values if they are in MWh
const convertedCurrentVal = convertValue(currentVal, voKey);
const convertedPrevVal = convertValue(prevVal, voKey);
const diff = convertedCurrentVal - convertedPrevVal;
if (!isNaN(diff)) {
sumOfDiffs += diff;
}
}
}
if (hasPropertiesAndNotDash(row, prevRow, "MT TUV")) {
const currentVal = toFloatWithDot(row["MT TUV"]);
const prevVal = toFloatWithDot(prevRow["MT TUV"]);
// Convert values if they are in MWh
const convertedCurrentVal = convertValue(currentVal, "MT TUV");
const convertedPrevVal = convertValue(prevVal, "MT TUV");
const diff = convertedCurrentVal - convertedPrevVal;
if (!isNaN(diff)) {
sumOfDiffs += diff;
}
}
const gasDiff = toFloatWithDot(row["Plyn"]) - toFloatWithDot(prevRow["Plyn"]);
// Convert heat to kWh based on input units (always GJ now since we converted above)
const voDiffInKwh = convertHeatToKwh(sumOfDiffs, "GJ");
const gasDiffInKwh = gasDiff * 9.855; // 1 m³ gas = 9.855 kWh (Method 2)
const efficiency = Number(
(voDiffInKwh / (gasDiffInKwh * monthlyEffectivityConstant)) * 10
).toFixed(4);
// Return heat units value (always in GJ for display)
// Make sure we don't return NaN
const heatUnitsValue = isNaN(sumOfDiffs) ? 0 : sumOfDiffs;
return {
...row,
ucinnost: efficiency === "NaN" ? "-" : efficiency,
heatUnits: heatUnitsValue,
};
};
module.exports = { computeEfficiency };