Tính toán chi phí và tiết kiệm trong TypeScript

Bạn có thể tạo phép tính của riêng mình bằng cách sử dụng TypeScript. Mã ở cuối trang này giúp bạn xác định xem chi phí lắp đặt tấm pin năng lượng mặt trời về lâu dài hay tiếp tục thanh toán hoá đơn tiền điện như cũ sẽ rẻ hơn.

Sau đây là thông tin chi tiết về cách mã này xác định chi phí cho tấm pin năng lượng mặt trời.

Phần 1: Nhu cầu và cách thiết lập hệ thống

Trước tiên, hãy xác định hoá đơn và mức sử dụng điện hiện tại của bạn:

  • Bạn dùng bao nhiêu điện mỗi tháng? (monthlyKwhEnergyConsumption)
  • Điện đó tốn bao nhiêu tiền vậy? (energyCostPerKwh)

Tiếp theo, hãy nhập các gói hệ thống năng lượng mặt trời của bạn:

  • Có bao nhiêu bảng điều khiển? (panelsCount)
  • Bảng điều khiển có tác động mạnh mẽ đến mức nào? (panelCapacityWatts)
  • Chi phí lắp đặt là bao nhiêu? (installationCostPerWatt)
  • Có chiết khấu nào trên hệ thống không? (solarIncentives)

Phần 2: Tính toán

Dựa trên các giá trị đã nhập, mã sẽ tính:

  • yearlyProductionAcKwh: Tổng điện năng hằng năm mà các tấm pin mặt trời của bạn có thể tạo ra.
  • totalCostWithSolar: Chi phí điện trong nhiều năm với tấm pin năng lượng mặt trời.
  • totalCostWithoutSolar: Chi phí điện trong nhiều năm không có các tấm pin năng lượng mặt trời.

Phần 3: Kết quả

Mã này cũng cho bạn biết những thông tin sau:

  • savings: Mức chênh lệch giữa chi phí khi có và không có tấm pin năng lượng mặt trời.
  • breakEvenYear: Số năm nữa thì chi phí của tấm pin năng lượng mặt trời bằng với lượng điện tiết kiệm được.

Ví dụ về mã

// Solar configuration, from buildingInsights.solarPotential.solarPanelConfigs
let panelsCount = 20;
let yearlyEnergyDcKwh = 12000;

// Basic settings
let monthlyAverageEnergyBill: number = 300;
let energyCostPerKwh = 0.31;
let panelCapacityWatts = 400;
let solarIncentives: number = 7000;
let installationCostPerWatt: number = 4.0;
let installationLifeSpan: number = 20;

// Advanced settings
let dcToAcDerate = 0.85;
let efficiencyDepreciationFactor = 0.995;
let costIncreaseFactor = 1.022;
let discountRate = 1.04;

// Solar installation
let installationSizeKw: number = (panelsCount * panelCapacityWatts) / 1000;
let installationCostTotal: number = installationCostPerWatt * installationSizeKw * 1000;

// Energy consumption
let monthlyKwhEnergyConsumption: number = monthlyAverageEnergyBill / energyCostPerKwh;
let yearlyKwhEnergyConsumption: number = monthlyKwhEnergyConsumption * 12;

// Energy produced for installation life span
let initialAcKwhPerYear: number = yearlyEnergyDcKwh * dcToAcDerate;
let yearlyProductionAcKwh: number[] = [...Array(installationLifeSpan).keys()].map(
  (year) => initialAcKwhPerYear * efficiencyDepreciationFactor ** year,
);

// Cost with solar for installation life span
let yearlyUtilityBillEstimates: number[] = yearlyProductionAcKwh.map(
  (yearlyKwhEnergyProduced, year) => {
    const billEnergyKwh = yearlyKwhEnergyConsumption - yearlyKwhEnergyProduced;
    const billEstimate =
      (billEnergyKwh * energyCostPerKwh * costIncreaseFactor ** year) / discountRate ** year;
    return Math.max(billEstimate, 0); // bill cannot be negative
  },
);
let remainingLifetimeUtilityBill: number = yearlyUtilityBillEstimates.reduce((x, y) => x + y, 0);
let totalCostWithSolar: number =
  installationCostTotal + remainingLifetimeUtilityBill - solarIncentives;
console.log(`Cost with solar: $${totalCostWithSolar.toFixed(2)}`);

// Cost without solar for installation life span
let yearlyCostWithoutSolar: number[] = [...Array(installationLifeSpan).keys()].map(
  (year) => (monthlyAverageEnergyBill * 12 * costIncreaseFactor ** year) / discountRate ** year,
);
let totalCostWithoutSolar: number = yearlyCostWithoutSolar.reduce((x, y) => x + y, 0);
console.log(`Cost without solar: $${totalCostWithoutSolar.toFixed(2)}`);

// Savings with solar for installation life span
let savings: number = totalCostWithoutSolar - totalCostWithSolar;
console.log(`Savings: $${savings.toFixed(2)} in ${installationLifeSpan} years`);