Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 29 additions & 26 deletions design/nahled-v6.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ultimate-power-card",
"version": "0.3.0",
"version": "0.3.1",
"description": "Home Assistant custom Lovelace card — Tesla-style power flow with multi-battery and multi-appliance support",
"main": "dist/ultimate-power-card.js",
"type": "module",
Expand Down
55 changes: 47 additions & 8 deletions src/ultimate-power-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from './statistics';
import type { ApplianceConfig, UltimatePowerCardConfig } from './types';

const VERSION = '0.3.0';
const VERSION = '0.3.1';

type Pt = [number, number];
type Tone = 'pos' | 'neg';
Expand Down Expand Up @@ -263,23 +263,49 @@ export class UltimatePowerCard extends LitElement {
// ------------------------------------------------------------- flow diagram

/** Line from a to b. Energy runs a → b, or b → a when `reverse` is set. */
private _wire(a: Pt, b: Pt, w: number, tone: Tone, main: boolean, reverse = false): SVGTemplateResult {
const d = `M${a[0]} ${a[1]} L${b[0]} ${b[1]}`;
if (w <= IDLE_W) return svg`<path class="wire" d=${d}></path>`;
private _wire(
a: Pt,
b: Pt,
w: number,
tone: Tone,
main: boolean,
reverse = false,
shift = 0,
lead = main,
): SVGTemplateResult {
const base = `M${a[0]} ${a[1]} L${b[0]} ${b[1]}`;
if (w <= IDLE_W) return svg`<path class="wire" d=${base}></path>`;
const speed = this._speed(w);
const len = Math.hypot(b[0] - a[0], b[1] - a[1]);
// `shift` moves the dots sideways so that two flows can share one line.
const ox = (-(b[1] - a[1]) / len) * shift;
const oy = ((b[0] - a[0]) / len) * shift;
const d = shift ? `M${a[0] + ox} ${a[1] + oy} L${b[0] + ox} ${b[1] + oy}` : base;
const rev = reverse ? 'rev' : '';
return svg`
<path class="wire on" d=${d}></path>
<path class="wire on" d=${base}></path>
<path class="stream ${tone} ${main ? 'main' : ''} ${rev}" d=${d}
data-rate=${(speed / DOT_GAP).toFixed(3)}></path>
${main
${lead
? svg`<path class="lead ${tone} ${rev}" d=${d} data-rate=${(speed / 100).toFixed(3)}
style=${`--p:${len};stroke-dasharray:0.1 ${len};animation-duration:${(len / 100).toFixed(2)}s`}></path>`
: nothing}
`;
}

/**
* A main arm that carries energy from two sources: own energy (green) and
* grid energy (orange) run side by side, each at the speed of its own power.
*/
private _mixed(a: Pt, b: Pt, own: number, fromGrid: number, reverse = false): SVGTemplateResult {
if (fromGrid <= IDLE_W) return this._wire(a, b, own, 'pos', true, reverse);
if (own <= IDLE_W) return this._wire(a, b, fromGrid, 'neg', true, reverse);
return svg`
${this._wire(a, b, own, 'pos', true, reverse, 3.5, own >= fromGrid)}
${this._wire(a, b, fromGrid, 'neg', true, reverse, -3.5, own < fromGrid)}
`;
}

private _text(x: number, y: number, cls: string, size: number, s: string, anchor = 'middle'): SVGTemplateResult {
return svg`<text x=${x} y=${y} class=${cls} font-size=${size} text-anchor=${anchor}>${s}</text>`;
}
Expand Down Expand Up @@ -412,9 +438,22 @@ export class UltimatePowerCard extends LitElement {

const wires: SVGTemplateResult[] = [];
if (hasPv) wires.push(this._wire(PROD, HUB, s.pv_total_w, 'pos', true));
wires.push(this._wire(HUB, HOUSE, s.house_w, 'neg', true));
// Where the energy on each arm comes from. Own energy (PV, battery) feeds
// the house first; the grid covers the rest and, after that, charging.
const gridIn = Math.max(0, grid);
const charge = Math.max(0, -bat);
const ownHouse = Math.min(s.house_w, Math.max(0, s.pv_total_w) + Math.max(0, bat));
const gridHouse = Math.min(gridIn, s.house_w - ownHouse);
const gridCharge = Math.min(charge, gridIn - gridHouse);
wires.push(this._mixed(HUB, HOUSE, s.house_w - gridHouse, gridHouse));
wires.push(this._wire(GRID, HUB, Math.abs(grid), grid >= 0 ? 'neg' : 'pos', true, grid < 0));
if (hasBat) wires.push(this._wire(BAT, HUB, Math.abs(bat), 'pos', true, bat < 0));
if (hasBat) {
wires.push(
bat < 0
? this._mixed(BAT, HUB, charge - gridCharge, gridCharge, true)
: this._wire(BAT, HUB, bat, 'pos', true),
);
}
strings.forEach((_, i) => wires.push(this._wire(pvPos[i], PROD, pvW[i], 'pos', false)));
// Appliances and batteries hang below their node as a chain; each segment
// carries the sum of everything further down.
Expand Down
Loading