/* Solar Go/No-Go: the money model. El Paso Electric (Texas) Rate 01, monthly netting, surplus at Rate 48.
Every rate below is dated and sourced on the Data tab. Runs in the browser; also loads in Node for tests. */
(function (root) {
'use strict';
const S = root.SGO;
const MONTHS = ["Dec","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov"];
const USE = S.use; // sample household, Dec..Nov (invented)
const UT = USE.reduce((a, b) => a + b, 0);
const SMON = {Jan:146.41,Feb:145.33,Mar:173.90,Apr:177.61,May:184.26,Jun:165.00,Jul:159.34,Aug:159.67,Sep:154.53,Oct:155.07,Nov:141.35,Dec:137.08}; // PVWatts v8, south, kWh/kW
const STOT = Object.values(SMON).reduce((a, b) => a + b, 0);
const RIDERS = 0.016154+0.000687+0.00349+0.01924+0.000498+0.001825+0.000361, FIXED = 13.71+2.70, MIL = 0.64;
const EXPORT = 0.016154, DEMAND = 5.65, APP = 85;
const STD_SUM = new Set(["May","Jun","Jul","Aug","Sep","Oct"]), DG_SUM = new Set(["Jun","Jul","Aug","Sep"]);
const PW_COST = 13473, PW_CUT_S = 3, PW_CUT_W = 2;
const ORDER = ["B","A","E","C","D"];
const DES = ORDER.map(k => { const d = S.designs.find(x => x.k === k); return Object.assign({}, d, {label: d.k + " · " + d.name}); });
const FIN = [
{k:"cash", name:"Cash today", short:"Cash today"},
{k:"credit", name:"Cash + 30% credit (if 25D returns)", short:"Cash + 30% credit"},
{k:"prepaid", name:"Prepaid lease (48E passed through)", short:"Prepaid lease"},
{k:"ppa", name:"PPA, $0 down", short:"PPA"}];
const COL = {E:"#2f7d4f", A:"#1f5fa8", B:"#8a8373", C:"#1f2a38", D:"#c7462f"};
const DEF = {price:2.30, pre:39, ppa:0.10, ppaesc:1, esc:2.5, ds:5, dw:3.5, yrs:10, disc:5, deg:0.5};
const interp = (x, xs, ys) => { if (x <= xs[0]) return ys[0]; for (let i = 1; i < xs.length; i++) { if (x <= xs[i]) { const t = (x-xs[i-1])/(xs[i]-xs[i-1]); return ys[i-1]+t*(ys[i]-ys[i-1]); } } return ys[ys.length-1]; };
const sizeF = kw => interp(kw, [4,8,12,15,20], [1.12,1.015,1.0,0.957,0.93]); // smaller systems cost more per watt
const stdE = (m, k) => STD_SUM.has(m) ? 0.12897*Math.min(k,600)+0.13897*Math.max(0,k-600) : 0.10897*k;
const dgE = (m, k) => DG_SUM.has(m) ? 0.09564*Math.min(k,600)+0.10564*Math.max(0,k-600) : 0.07564*k;
const sum = a => a.reduce((x, y) => x + y, 0);
const billNow = () => MONTHS.map((m, i) => FIXED + MIL + stdE(m, USE[i]) + RIDERS*USE[i]);
const prodOf = (kwh, f) => MONTHS.map(m => kwh*SMON[m]/STOT*f);
function billDG(prod, ds, dw) {
let exp = 0;
const b = MONTHS.map((m, i) => { const net = USE[i]-prod[i]; const d = DG_SUM.has(m) ? ds : dw; let x = FIXED+MIL+DEMAND*d;
if (net >= 0) x += dgE(m, net)+RIDERS*net; else { x -= EXPORT*(-net); exp += -net; } return x; });
return {b, exp};
}
const num = (v, lo, hi, d) => { v = parseFloat(v); if (!isFinite(v)) v = d; return Math.min(hi, Math.max(lo, v)); };
function run(d, f, p) {
const gross = d.kw*1000*p.price*sizeF(d.kw);
let cost;
if (f === "cash") cost = gross+APP+d.extra;
else if (f === "credit") cost = 0.7*gross+APP+d.extra;
else if (f === "prepaid") cost = (1-p.pre)*gross+APP+d.extra;
else cost = APP+d.extra;
if (p.pw) cost += PW_COST*(f === "cash" ? 1 : 0.7);
const base = sum(billNow());
const ds = Math.max(0, p.ds-(p.pw ? PW_CUT_S : 0)), dw = Math.max(0, p.dw-(p.pw ? PW_CUT_W : 0));
const cf = [-cost]; let cum = -cost, pay = null; const cumBy = [-cost]; let exp1 = 0, expRev25 = 0, yr1 = 0, after1 = 0;
for (let y = 1; y <= 25; y++) {
const fe = Math.pow(1+p.esc, y-1), fp = Math.pow(1-p.deg, y-1);
const prod = prodOf(d.kwh, fp); const r = billDG(prod, ds, dw);
let sav = (base-sum(r.b))*fe;
if (f === "ppa") sav -= p.ppa*Math.pow(1+p.ppaesc, y-1)*sum(prod);
if (y === 1) { exp1 = r.exp; yr1 = sav; after1 = sum(r.b)+(f === "ppa" ? p.ppa*sum(prod) : 0); }
expRev25 += r.exp*EXPORT;
cf.push(sav); if (pay === null && sav > 0 && cum+sav >= 0) pay = y-1+(-cum)/sav; cum += sav; cumBy.push(cum);
}
if (pay !== null && pay < 0) pay = 0;
const npv = r => cf.reduce((a, c, i) => a+c/Math.pow(1+r, i), 0);
let irr = null;
if (cost > 200) { // bisection between -90% and +100% a year
let lo = -0.9, hi = 1;
if (npv(lo) > 0 && npv(hi) < 0) { for (let i = 0; i < 200; i++) { const mid = (lo+hi)/2; npv(mid) > 0 ? lo = mid : hi = mid; } irr = lo; }
else if (npv(hi) >= 0) irr = hi;
}
return {cost, base, after1, yr1, pay, net25: sum(cf), roi: cost > 200 ? sum(cf)/cost : null, irr, npv: npv(p.disc), cumBy, exp1, expRev1: exp1*EXPORT, expRev25, selfUse: d.kwh-exp1};
}
function verdict(r, f, p) {
if (f === "ppa") { if (r.yr1 > 0 && r.npv > 0) return ["Go","g","saves from year 1"]; if (r.npv > 0) return ["Go if you stay","y","costs more at first, gains later"]; return ["No-go","r","costs more than it saves"]; }
if (r.pay === null || r.pay > 25) return ["No-go","r","never pays back in 25 years"];
if (r.pay <= p.yrs && r.irr !== null && r.irr >= p.disc) return ["Go","g","pays back in "+r.pay.toFixed(1)+" yr, inside the "+p.yrs+" yr you expect to stay"];
if (r.irr !== null && r.irr >= p.disc) return ["Go if you stay","y","pays back in "+r.pay.toFixed(1)+" yr, after the "+p.yrs+" yr you expect to stay"];
return ["No-go","r","return "+(r.irr === null ? "n/a" : (100*r.irr).toFixed(1)+"%")+" is below your "+(100*p.disc).toFixed(1)+"%"];
}
const money = v => (v < 0 ? "−$" : "$")+Math.abs(Math.round(v)).toLocaleString("en-US");
const fmtK = v => Math.round(v).toLocaleString("en-US");
const pillOf = v => `${v[0]}`;
const api = {MONTHS, USE, UT, DES, FIN, DEF, COL, run, verdict, money, sizeF, billNow, prodOf, billDG, EXPORT, DEMAND, PW_COST, PW_CUT_S, PW_CUT_W};
root.SGOE = api;
if (typeof document === "undefined") return; // Node: model only
const $ = id => document.getElementById(id);
const IDS = {price:"ev-price", pre:"ev-pre", ppa:"ev-ppa", ppaesc:"ev-ppaesc", esc:"ev-esc", ds:"ev-ds", dw:"ev-dw", yrs:"ev-yrs", disc:"ev-disc", deg:"ev-deg"};
const P = () => ({price: num($("ev-price").value, 0.5, 6, DEF.price), pre: num($("ev-pre").value, 0, 80, DEF.pre)/100,
ppa: num($("ev-ppa").value, 0, 0.5, DEF.ppa), ppaesc: num($("ev-ppaesc").value, 0, 10, DEF.ppaesc)/100,
esc: num($("ev-esc").value, -5, 15, DEF.esc)/100, ds: num($("ev-ds").value, 0, 30, DEF.ds), dw: num($("ev-dw").value, 0, 30, DEF.dw),
yrs: Math.round(num($("ev-yrs").value, 1, 25, DEF.yrs)), disc: num($("ev-disc").value, 0, 20, DEF.disc)/100,
deg: num($("ev-deg").value, 0, 3, DEF.deg)/100, pw: $("ev-pw").checked});
let FSEL = "cash", DSEL = "E", PICK = "E";
function render() {
const p = P();
const R = {}; DES.forEach(d => { R[d.k] = {}; FIN.forEach(f => R[d.k][f.k] = run(d, f.k, p)); });
// matrix
let h = "
"+FIN.map(f => `
${f.short}
`).join("")+"
";
DES.forEach(d => { h += `
${d.label} ${d.n} panels · ${d.kw} kW
`;
FIN.forEach(f => { const r = R[d.k][f.k], v = verdict(r, f.k, p);
const line = f.k === "ppa" ? `yr 1 ${money(r.yr1)} · 25 yr ${money(r.net25)}` : `${r.pay === null ? "> 25" : r.pay.toFixed(1)} yr · ROI ${r.roi === null ? "–" : Math.round(100*r.roi)+"%"}`;
h += `
${v[0]}${line}
`; });
h += "
"; });
$("ev-mx").innerHTML = h;
$("ev-mx").querySelectorAll("td[data-d]").forEach(td => { const go = () => { DSEL = td.dataset.d; FSEL = td.dataset.f; PICK = DSEL; render(); };
td.addEventListener("click", go); td.addEventListener("keydown", e => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); go(); } }); });
// hero
const cand = []; DES.forEach(d => ["cash","prepaid","ppa"].forEach(f => cand.push({d, f, r: R[d.k][f]})));
const best = cand.reduce((a, b) => b.r.npv > a.r.npv ? b : a);
const sd = DES.find(x => x.k === DSEL), sr = R[DSEL][FSEL], v = verdict(sr, FSEL, p);
const fname = FIN.find(x => x.k === FSEL).name, fshort = FIN.find(x => x.k === FSEL).short;
$("ev-badge").className = "big "+v[1]; $("ev-badge").innerHTML = `${v[0]}${sd.k} · ${fshort}`;
$("ev-title").textContent = `${sd.label}: ${sd.n} panels, ${sd.kw} kW, ${fname}`;
const ec = R.E.cash, ep = R.E.prepaid;
const pay = r => r.pay === null ? "over 25 yr" : r.pay.toFixed(1)+" yr";
$("ev-why").innerHTML = `${v[2].charAt(0).toUpperCase()+v[2].slice(1)}. Best value today: ${best.d.label}, ${FIN.find(x => x.k === best.f).short} (NPV ${money(best.r.npv)}). The bill-sized design E for cash: ${money(ec.cost)}, payback ${pay(ec)}, ROI ${ec.roi === null ? "–" : Math.round(100*ec.roi)+"%"}. The same panels as a prepaid lease (the installer keeps the 30% credit and lowers the price): ${money(ep.cost)}, payback ${pay(ep)}, if it is running by Dec 31, 2027.`;
const k = [[money(sr.cost),"up-front"],[money(sr.base)+" → "+money(sr.after1),"yearly bill, now → year 1"],[money(sr.yr1),"year-1 saving"],
[FSEL === "ppa" ? "none" : pay(sr),"payback"],[sr.roi === null ? "n/a" : Math.round(100*sr.roi)+"%","ROI, 25 yr"],
[sr.irr === null ? "n/a" : (100*sr.irr).toFixed(1)+"%","IRR"],[money(sr.net25),"25-yr net"],[money(sr.expRev1)+"/yr","sold back ("+fmtK(sr.exp1)+" kWh)"]];
$("ev-kpis").innerHTML = k.map(x => `
${x[0]}${x[1]}
`).join("");
$("ev-rule").textContent = `Rule: Go if payback is within the ${p.yrs} years you expect to stay and the return beats your ${(100*p.disc).toFixed(1)}% discount rate; "Go if you stay" if the return is good but payback comes later; No-go otherwise. A PPA is Go if it saves from year 1 and over its life.`;
// financing buttons + chart
$("ev-fin").innerHTML = FIN.map(f => ``).join("");
$("ev-fin").querySelectorAll("button").forEach(b => b.addEventListener("click", () => { FSEL = b.dataset.f; render(); }));
["ev-fin-name","ev-fin-name2","ev-fin-name3"].forEach(id => $(id).textContent = fname);
chart(R, p);
// sold back
let e = "
Design
Made kWh/yr
Used at home
Sold back kWh/yr
Sold-back revenue yr 1
Over 25 yr
Same kWh used at home would save
";
DES.forEach(d => { const x = R[d.k].cash; e += `
${d.label}
${fmtK(d.kwh)}
${fmtK(x.selfUse)} (${Math.round(100*x.selfUse/UT)}% of use)
${fmtK(x.exp1)}
${money(x.expRev1)}
${money(x.expRev25)}
≈ ${money(x.exp1*0.125)}/yr
`; });
$("ev-export").innerHTML = e+"";
// full table
let t = "
Design
Up-front
Bill now → yr 1
Yr-1 saving
Payback
ROI
IRR
NPV
25-yr net
Sold back yr 1
Leave at yr "+p.yrs+"
Verdict
";
DES.forEach(d => { const x = R[d.k][FSEL], vv = verdict(x, FSEL, p); t += `
No-go on money El Paso Electric nets solar over the whole month, so moving daytime solar into the evening does not change the kWh billed. The battery's only bill value is trimming the $5.65/kW demand charge: about ${PW_CUT_S} kW in summer and ${PW_CUT_W} kW in winter if set for peak shaving (ASSUMED), about ${money(pwSave)}/yr against ${money(PW_COST)} installed, a ${Math.round(PW_COST/pwSave)}-year payback, longer than its 10-year warranty. Through a lease the 30% business credit could bring it to about ${money(PW_COST*0.7)}: still a ${Math.round(PW_COST*0.7/pwSave)}-year payback. Buy it for outage backup, not for the bill. Tick "Add one Tesla Powerwall 3" under the assumptions to see it in every cell.
`;
cmp(R, p);
if (root.SGOUI && root.SGOUI.onEcon) root.SGOUI.onEcon();
}
function chart(R, p) {
const W = 760, H = 320, L = 66, Rr = 12, T = 14, B = 34;
const series = DES.map(d => ({d, v: R[d.k][FSEL].cumBy}));
let mn = 0, mx = 0; series.forEach(s => s.v.forEach(x => { mn = Math.min(mn, x); mx = Math.max(mx, x); }));
const span = Math.max(1, mx-mn), step = span > 150000 ? 50000 : span > 60000 ? 20000 : span > 20000 ? 10000 : 5000;
mn = Math.floor(mn/step)*step; mx = Math.ceil(mx/step)*step; if (mx === mn) mx = mn+step;
const X = y => L+(W-L-Rr)*y/25, Y = v => T+(H-T-B)*(mx-v)/(mx-mn);
let s = `";
$("ev-leg").innerHTML = DES.map(d => `${d.label}`).join("");
}
function cmp(R, p) {
const yrs = x => x.toFixed(1)+" yr", pct = x => Math.round(100*x)+"%", ir = x => (100*x).toFixed(1)+"%";
const SITE = k => { const d = DES.find(x => x.k === k);
return {faces: Object.keys(d.per).join(", "), street: `${d.street} / ${d.angle}`, pct: d.pct+"%", ridge: d.ridge > 0.5 ? "36 in (array over 33%)" : "18 in",
clr: (k === "A" || k === "B") ? "0.25 m edges; 18 in hips and valleys" : "6 in edges, hips and valleys", vents: d.vents ? d.vents+" rerouted" : "0",
shade: d.shade.toFixed(0)+"%", panel: (d.ac <= 7.7 ? "Fits" : "Needs a fix")+` (≈${d.ac} kW AC)`, xfmr: d.kw >= 15 ? "Ask the utility first ("+d.kw+" kW)" : d.kw >= 9 ? "Ask the utility" : "Low" }; };
const head = "
"+DES.map(d => `
${d.label}
`).join("")+"
";
const sec = t => `
${t}
`;
const row = (lab, f) => `
${lab}
`+DES.map(d => `
${f(d.k)}
`).join("")+"
";
const best = (lab, f, fmt, hi) => { const vals = DES.map(d => f(d.k)); const ok = vals.filter(v => v != null); const b = ok.length ? (hi ? Math.max(...ok) : Math.min(...ok)) : null;
return `
${lab}
`+vals.map(v => `
${v == null ? "–" : fmt(v)}
`).join("")+"
"; };
let h = head+""+sec("Roof and site");
h += row("Panels · kW DC", k => { const d = DES.find(x => x.k === k); return `${d.n} · ${d.kw}`; });
h += row("Roof faces used", k => SITE(k).faces);
h += row("Facing street / at an angle", k => SITE(k).street);
h += row("Array share of roof (fire code)", k => SITE(k).pct);
h += row("Ridge setback", k => SITE(k).ridge);
h += row("Other clearances", k => SITE(k).clr);
h += row("Roof vents under panels", k => SITE(k).vents);
h += row("Worst shading on a kept panel", k => SITE(k).shade);
h += row("200 A main panel (120% rule)", k => SITE(k).panel);
h += row("Transformer / utility review", k => SITE(k).xfmr);
h += sec("Energy (year 1)");
h += row("Made, kWh/yr", k => fmtK(DES.find(x => x.k === k).kwh));
h += best(`Used at home, % of ${fmtK(UT)} kWh`, k => R[k].cash.selfUse/UT, pct, true);
h += row("Sold back, kWh/yr", k => fmtK(R[k].cash.exp1));
h += row("Sold-back revenue, yr 1 / 25 yr", k => `${money(R[k].cash.expRev1)} / ${money(R[k].cash.expRev25)}`);
h += row("Bill, now → year 1", k => `${money(R[k].cash.base)} → ${money(R[k].cash.after1)}`);
[["cash","Cash today: no credit"],["credit","Cash + 30% credit: only if 25D returns"],["prepaid","Prepaid lease: 48E passed through, running by Dec 31, 2027"]].forEach(([f, t]) => {
h += sec(t);
h += best("Up-front", k => R[k][f].cost, money, false);
h += best("Payback", k => R[k][f].pay, yrs, false);
h += best("ROI, 25 yr", k => R[k][f].roi, pct, true);
h += best("IRR", k => R[k][f].irr, ir, true);
h += best("NPV at "+(100*p.disc).toFixed(1)+"%", k => R[k][f].npv, money, true);
h += row("Verdict", k => pillOf(verdict(R[k][f], f, p))); });
h += sec("PPA: $0 down, $"+p.ppa.toFixed(3)+"/kWh");
h += best("Saving, year 1", k => R[k].ppa.yr1, money, true);
h += best("Net over 25 yr", k => R[k].ppa.net25, money, true);
h += row("Verdict", k => pillOf(verdict(R[k].ppa, "ppa", p)));
$("cmp-table").innerHTML = h+"";
}
function costs() {
const C = DES.find(x => x.k === "C"), D = DES.find(x => x.k === "D"), E = DES.find(x => x.k === "E");
$("costRows").innerHTML = [
["El Paso Electric application", "$85 for systems up to 100 kW; the utility fits the two-way meter at no charge", "$85, every design"],
["Transformer / line upgrade", `Texas rule 16 TAC §25.211: no study fee under 20 kW, and the utility must quote any upgrade before work. Unlikely for E (${E.kw} kW); ask first for C and D (${D.kw} kW).`, "$0 (flagged for C and D)"],
["Main panel: NEC 705.12, 120% rule", `A 200 A panel with a 200 A main takes 40 A of solar backfeed (≈ 7.7 kW AC). E ≈ ${E.ac} kW AC fits; C (≈ ${C.ac} kW AC) and D need a supply-side tap, a smaller main or a panel upgrade.`, "$2,150 for C and D"],
["Vent rerouting", `Only design D covers roof vents (${D.vents})`, "$1,500 for D (ASSUMED)"],
["City permit", "$200 residential submittal deposit, credited to the permit fee", "Inside the $/W price"],
["Microinverters", "Needed where panels see some shade; 25-year warranties, so no inverter replacement modelled", "Inside the $/W price"]
].map(r => `