Add an electricity estimate to the Costs page

The typical draws in the Inventory, the price of a kWh and the currency set
there give a cost per device and in all, counted in the totals under an
Electricity category when there is a draw, a price and a three-letter currency,
and said plainly (with what is missing) when there is not.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-10 20:59:50 +01:00
1 parent d69962528e
commit f546971000
6 files changed
+351 -9

No files matched your search

+99 -2
View File
@@ -1,6 +1,6 @@
import { useEffect, useMemo, useState } from "react";
import { Link } from "react-router-dom";
import { api, type CostInput, type CostItem, type CostsReport, type CurrentUser } from "../api/client";
import { api, type CostInput, type CostItem, type CostsReport, type CurrentUser, type Electricity } from "../api/client";
import CostHistory from "../components/CostHistory";
import EmptyState from "../components/EmptyState";
import { BlockSkeleton } from "../components/Skeleton";
@@ -19,6 +19,101 @@ const money = (n: number, currency: string) => {
}
};
/** An amount in the currency as it was typed in the Inventory settings ("SEK", "kr"): a three-letter code is formatted as money, anything else is written after the number. */
const moneyIn = (n: number, label: string, code: string | null) => (code ? money(n, code) : `${n.toLocaleString(undefined, { maximumFractionDigits: 2 })}${label ? ` ${label}` : ""}`);
/**
* What the electricity costs: the typical draw of each piece of hardware in the Inventory, at the price of a kWh set there. Shows what it is made of, biggest first, and says what is missing when there
* is not enough to work it out (a draw, a price, a currency that is a three-letter code) — and whether it is counted in the totals above.
*/
function ElectricityCard({ e }: { e: Electricity }) {
const none = e.watts <= 0;
const hint =
e.missing === "draw"
? "Enter each piece of hardware's typical power draw (watts) in the Inventory to estimate it."
: e.missing === "price"
? "Set the price of a kWh under Inventory → Running cost to put a cost on it."
: e.missing === "currency"
? `The currency in the Inventory settings (${e.currencyLabel ? `“${e.currencyLabel}”` : "none"}) is not a three-letter code, so this is not counted in the totals — set it to SEK, EUR … to count it.`
: null;
return (
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">Electricity</h3>
<div className="card-actions">
<Link to="/inventory" className="btn btn-sm">
Open the Inventory
</Link>
</div>
</div>
<div className="card-body">
{none ? (
<div className="text-secondary">{hint}</div>
) : (
<div className="row g-3 mb-3">
<div className="col-6 col-md-3">
<div className="text-secondary small">Typical draw</div>
<div className="h2 mb-0">{e.watts.toLocaleString()} W</div>
<div className="text-secondary small">{e.kwhPerMonth?.toLocaleString(undefined, { maximumFractionDigits: 0 })} kWh a month</div>
</div>
<div className="col-6 col-md-3">
<div className="text-secondary small">Per month</div>
<div className="h2 mb-0">{e.monthly !== null ? moneyIn(e.monthly, e.currencyLabel, e.currency) : "—"}</div>
<div className="text-secondary small">{e.pricePerKwh !== null ? `at ${e.pricePerKwh} per kWh` : "no price set"}</div>
</div>
<div className="col-6 col-md-3">
<div className="text-secondary small">Per year</div>
<div className="h2 mb-0">{e.yearly !== null ? moneyIn(e.yearly, e.currencyLabel, e.currency) : "—"}</div>
</div>
<div className="col-6 col-md-3">
<div className="text-secondary small">In the totals</div>
<div className="h2 mb-0">{e.includedInTotals ? "Yes" : "No"}</div>
<div className="text-secondary small">{e.includedInTotals ? "under Electricity" : "see below"}</div>
</div>
</div>
)}
{!none && hint && <div className="text-yellow small mb-3">{hint}</div>}
{e.devices.length > 0 && (
<div className="table-responsive">
<table className="table table-sm table-vcenter mb-0">
<thead>
<tr>
<th>Hardware</th>
<th className="text-end">Draw</th>
<th className="text-end">Per month</th>
<th style={{ minWidth: 140 }}>Share</th>
</tr>
</thead>
<tbody>
{e.devices.map((d) => (
<tr key={d.id}>
<td>
<Link to={`/inventory?q=${encodeURIComponent(d.name)}`}>{d.name}</Link> <span className="text-secondary small">{d.kind}</span>
</td>
<td className="text-end">{d.watts} W</td>
<td className="text-end">{d.monthly !== null ? moneyIn(d.monthly, e.currencyLabel, e.currency) : "—"}</td>
<td>
<div className="progress progress-sm" title={`${d.share}% of the draw`}>
<div className="progress-bar" style={{ width: `${Math.max(2, d.share)}%` }} />
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
{e.withoutDraw > 0 && (
<div className="text-secondary small mt-2">
{e.withoutDraw} piece{e.withoutDraw === 1 ? "" : "s"} of hardware in the Inventory {e.withoutDraw === 1 ? "has" : "have"} no power draw entered and {e.withoutDraw === 1 ? "is" : "are"} not in this.
</div>
)}
<div className="text-secondary small mt-2">An estimate: the typical draws added up and run all day, every day ({Math.round((24 * 365) / 12)} hours a month), at one price. What the meter says is what counts.</div>
</div>
</div>
);
}
const blank = (currency: string): CostInput => ({ name: "", amount: 0, currency, period: "month", category: "Subscriptions", renewsOn: null, note: null });
function ItemForm({ initial, categories, currencies, saving, onSave, onCancel }: { initial: CostInput; categories: string[]; currencies: string[]; saving: boolean; onSave: (v: CostInput) => void; onCancel: () => void }) {
@@ -183,6 +278,8 @@ export default function Costs({ user }: { user: CurrentUser }) {
</div>
)}
{report && <ElectricityCard e={report.electricity} />}
{form && (
<div className="card mb-3">
<div className="card-header">
@@ -224,7 +321,7 @@ export default function Costs({ user }: { user: CurrentUser }) {
<strong>{a.name}</strong> <span className="badge bg-blue-lt text-blue">read from {a.source}</span>
<div className="text-secondary small text-break">{a.detail}{a.stale ? " — not read lately, so this may be out of date" : ""}</div>
</td>
<td className="text-secondary">Hosting</td>
<td className="text-secondary">{a.category ?? "Hosting"}</td>
<td className="text-secondary">estimate</td>
<td>{money(a.monthly, a.currency)}</td>
<td className="text-secondary">—</td>