-
Notifications
You must be signed in to change notification settings - Fork 61
/
SolarPotentialSection.svelte
420 lines (378 loc) · 13.9 KB
/
SolarPotentialSection.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<!--
Copyright 2023 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script lang="ts">
/* global google */
import { slide } from 'svelte/transition';
import Expandable from '../components/Expandable.svelte';
import SummaryCard from '../components/SummaryCard.svelte';
import type { SolarPanelConfig } from '../solar';
import Table from '../components/Table.svelte';
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
import { GoogleCharts } from 'google-charts';
import { findSolarConfig, showMoney, showNumber } from '../utils';
import InputNumber from '../components/InputNumber.svelte';
import InputPanelsCount from '../components/InputPanelsCount.svelte';
import InputMoney from '../components/InputMoney.svelte';
import InputPercent from '../components/InputPercent.svelte';
import InputRatio from '../components/InputRatio.svelte';
export let expandedSection: string;
export let configId: number;
export let monthlyAverageEnergyBillInput: number;
export let energyCostPerKwhInput: number;
export let panelCapacityWattsInput: number;
export let dcToAcDerateInput: number;
export let solarPanelConfigs: SolarPanelConfig[];
export let defaultPanelCapacityWatts: number;
const icon = 'payments';
const title = 'Solar Potential analysis';
let costChart: HTMLElement;
let showAdvancedSettings = false;
// [START solar_potential_calculations]
// 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`);
// [END solar_potential_calculations]
// Reactive calculations
let panelCapacityRatio: number = 1.0;
$: panelCapacityRatio = panelCapacityWattsInput / defaultPanelCapacityWatts;
$: installationCostTotal = installationCostPerWatt * installationSizeKw * 1000;
$: if (solarPanelConfigs[configId]) {
installationSizeKw = (solarPanelConfigs[configId].panelsCount * panelCapacityWattsInput) / 1000;
}
$: monthlyKwhEnergyConsumption = monthlyAverageEnergyBillInput / energyCostPerKwhInput;
$: yearlyKwhEnergyConsumption = monthlyKwhEnergyConsumption * 12;
$: if (solarPanelConfigs[configId]) {
initialAcKwhPerYear =
solarPanelConfigs[configId].yearlyEnergyDcKwh * panelCapacityRatio * dcToAcDerateInput;
}
$: yearlyProductionAcKwh = [...Array(installationLifeSpan).keys()].map(
(year) => initialAcKwhPerYear * efficiencyDepreciationFactor ** year,
);
$: yearlyUtilityBillEstimates = yearlyProductionAcKwh.map((yearlyKwhEnergyProduced, year) => {
const billEnergyKwh = yearlyKwhEnergyConsumption - yearlyKwhEnergyProduced;
const billEstimate =
(billEnergyKwh * energyCostPerKwhInput * costIncreaseFactor ** year) / discountRate ** year;
return Math.max(billEstimate, 0); // bill cannot be negative
});
$: remainingLifetimeUtilityBill = yearlyUtilityBillEstimates.reduce((x, y) => x + y, 0);
$: totalCostWithSolar = installationCostTotal + remainingLifetimeUtilityBill - solarIncentives;
$: yearlyCostWithoutSolar = [...Array(installationLifeSpan).keys()].map(
(year) =>
(monthlyAverageEnergyBillInput * 12 * costIncreaseFactor ** year) / discountRate ** year,
);
$: totalCostWithoutSolar = yearlyCostWithoutSolar.reduce((x, y) => x + y, 0);
$: savings = totalCostWithoutSolar - totalCostWithSolar;
let energyCovered: number;
$: energyCovered = yearlyProductionAcKwh[0] / yearlyKwhEnergyConsumption;
let breakEvenYear: number = -1;
$: GoogleCharts.load(
() => {
if (!costChart) {
return;
}
const year = new Date().getFullYear();
let costWithSolar = 0;
const cumulativeCostsWithSolar = yearlyUtilityBillEstimates.map(
(billEstimate, i) =>
(costWithSolar +=
i == 0 ? billEstimate + installationCostTotal - solarIncentives : billEstimate),
);
let costWithoutSolar = 0;
const cumulativeCostsWithoutSolar = yearlyCostWithoutSolar.map(
(cost) => (costWithoutSolar += cost),
);
breakEvenYear = cumulativeCostsWithSolar.findIndex(
(costWithSolar, i) => costWithSolar <= cumulativeCostsWithoutSolar[i],
);
const data = google.visualization.arrayToDataTable([
['Year', 'Solar', 'No solar'],
[year.toString(), 0, 0],
...cumulativeCostsWithSolar.map((_, i) => [
(year + i + 1).toString(),
cumulativeCostsWithSolar[i],
cumulativeCostsWithoutSolar[i],
]),
]);
/* eslint-disable @typescript-eslint/no-explicit-any */
const googleCharts = google.charts as any;
const chart = new googleCharts.Line(costChart);
const options = googleCharts.Line.convertOptions({
title: `Cost analysis for ${installationLifeSpan} years`,
width: 350,
height: 200,
});
chart.draw(data, options);
},
{ packages: ['line'] },
);
function updateConfig() {
monthlyKwhEnergyConsumption = monthlyAverageEnergyBillInput / energyCostPerKwhInput;
yearlyKwhEnergyConsumption = monthlyKwhEnergyConsumption * 12;
panelCapacityRatio = panelCapacityWattsInput / defaultPanelCapacityWatts;
configId = findSolarConfig(
solarPanelConfigs,
yearlyKwhEnergyConsumption,
panelCapacityRatio,
dcToAcDerateInput,
);
}
</script>
<Expandable
bind:section={expandedSection}
{icon}
{title}
subtitle="Values are only placeholders."
subtitle2="Update with your own values."
secondary
>
<div class="flex flex-col space-y-4 pt-1">
<div class="p-4 mb-4 surface-variant outline-text rounded-lg">
<p class="relative inline-flex items-center space-x-2">
<md-icon class="md:w-6 w-8">info</md-icon>
<span>
Projections use a
<a
class="primary-text"
href="https://developers.google.com/maps/documentation/solar/calculate-costs-us"
target="_blank"
>
USA financial model
<md-icon class="text-sm">open_in_new</md-icon>
</a>
</span>
</p>
</div>
<InputMoney
bind:value={monthlyAverageEnergyBillInput}
icon="credit_card"
label="Monthly average energy bill"
>
/>
<div class="inline-flex items-center space-x-2">
<div class="grow">
<InputPanelsCount bind:configId {solarPanelConfigs} />
</div>
<md-icon-button role={undefined} on:click={updateConfig}>
<md-icon>sync</md-icon>
</md-icon-button>
</div>
<InputMoney
bind:value={energyCostPerKwhInput}
icon="paid"
label="Energy cost per kWh"
>
/>
<InputMoney
bind:value={solarIncentives}
icon="redeem"
label="Solar incentives"
>
/>
<InputMoney
bind:value={installationCostPerWatt}
icon="request_quote"
label="Installation cost per Watt"
>
/>
<InputNumber
bind:value={panelCapacityWattsInput}
icon="bolt"
label="Panel capacity"
suffix="Watts"
>
/>
<div class="flex flex-col items-center w-full">
<md-text-button
trailing-icon
role={undefined}
on:click={() => (showAdvancedSettings = !showAdvancedSettings)}
>
{showAdvancedSettings ? 'Hide' : 'Show'} advanced settings
<md-icon slot="icon">
{showAdvancedSettings ? 'expand_less' : 'expand_more'}
</md-icon>
</md-text-button>
</div>
{#if showAdvancedSettings}
<div class="flex flex-col space-y-4" transition:slide={{ duration: 200 }}>
<InputNumber
bind:value={installationLifeSpan}
icon="date_range"
label="Installation lifespan"
suffix="years"
>
/>
<InputPercent
bind:value={dcToAcDerateInput}
icon="dynamic_form"
label="DC to AC conversion "
>
/>
<InputRatio
bind:value={efficiencyDepreciationFactor}
icon="trending_down"
label="Panel efficiency decline per year"
decrease
>
/>
<InputRatio
bind:value={costIncreaseFactor}
icon="price_change"
label="Energy cost increase per year"
>
/>
<InputRatio
bind:value={discountRate}
icon="local_offer"
label="Discount rate per year"
>
/>
</div>
{/if}
<div class="grid justify-items-end">
<md-filled-tonal-button
trailing-icon
role={undefined}
href="https://developers.google.com/maps/documentation/solar/calculate-costs-us"
target="_blank"
>
More details
<md-icon slot="icon">open_in_new</md-icon>
</md-filled-tonal-button>
</div>
</div>
</Expandable>
<div class="absolute top-0 left-0">
{#if expandedSection == title}
<div class="flex flex-col space-y-2 m-2">
<SummaryCard
{icon}
{title}
rows={[
{
icon: 'energy_savings_leaf',
name: 'Yearly energy',
value: showNumber(
(solarPanelConfigs[configId]?.yearlyEnergyDcKwh ?? 0) * panelCapacityRatio,
),
units: 'kWh',
},
{
icon: 'speed',
name: 'Installation size',
value: showNumber(installationSizeKw),
units: 'kW',
},
{
icon: 'request_quote',
name: 'Installation cost',
value: showMoney(installationCostTotal),
},
{
icon: [
'battery_0_bar',
'battery_1_bar',
'battery_2_bar',
'battery_3_bar',
'battery_4_bar',
'battery_5_bar',
'battery_full',
][Math.floor(Math.min(Math.round(energyCovered * 100) / 100, 1) * 6)],
name: 'Energy covered',
value: Math.round(energyCovered * 100).toString(),
units: '%',
},
]}
/>
</div>
<div class="mx-2 p-4 surface on-surface-text rounded-lg shadow-lg">
<div bind:this={costChart} />
<div class="w-full secondary-text">
<Table
rows={[
{
icon: 'wallet',
name: 'Cost without solar',
value: showMoney(totalCostWithoutSolar),
},
{
icon: 'wb_sunny',
name: 'Cost with solar',
value: showMoney(totalCostWithSolar),
},
{
icon: 'savings',
name: 'Savings',
value: showMoney(savings),
},
{
icon: 'balance',
name: 'Break even',
value:
breakEvenYear >= 0
? `${breakEvenYear + new Date().getFullYear() + 1} in ${breakEvenYear + 1}`
: '--',
units: 'years',
},
]}
/>
</div>
</div>
{/if}
</div>
You can’t perform that action at this time.