diff --git a/README.cs.md b/README.cs.md
index d5e70b7..bc929df 100644
--- a/README.cs.md
+++ b/README.cs.md
@@ -90,10 +90,10 @@ Senzory výkonu mohou být ve W nebo kW.
| `batteries[]` | – | `soc_entity`, volitelně `name` a výkon buď jedním senzorem se znaménkem `power_entity` (kladná hodnota = vybíjení, `invert` ho otočí), nebo dvěma vždy kladnými senzory `charge_entity` + `discharge_entity`. |
| `battery_total_entity`, `battery_invert` | součet baterií | Celkový výkon baterií a přepínač jeho znaménka. |
| `battery_charge_entity`, `battery_discharge_entity` | – | Celkový výkon baterií dvěma vždy kladnými senzory. |
-| `appliances[]` | – | `power_entity`, `name`, volitelně `on_threshold_w`. |
+| `appliances[]` | – | `name`, `power_entity`, volitelně `on_threshold_w`. S `children` (seznam stejného tvaru) se z položky stane skupina; `collapsed: true` ji zobrazí zavřenou. Viz [Skupiny spotřebičů](#skupiny-spotřebičů). |
| `show_other` | `true` | Zobrazit dopočtený zbytek spotřeby domu. |
| `update_interval` | `1` | Jak často karta překresluje živé hodnoty, v sekundách (1–20). Senzory se nečtou častěji, než samy hlásí. |
-| `animation` | `auto` | `auto` se řídí systémovým omezením pohybu, `always` ho ignoruje, `off` animace vypne. |
+| `animation` | `auto` | `auto`: když systém žádá omezení pohybu, hýbou se jen malé tečky toku (ukazují směr energie) a velká tečka s pulzováním se skryjí. `always` animuje vše bez ohledu na systém, `off` animace vypne. |
| `animation_speed` | `1` | Násobitel rychlosti toku. |
| `max_power` | `10000` | Výkon (W), při kterém tok dosáhne plné rychlosti. |
| `show_w_not_kw` | `false` | Vždy zobrazovat watty. |
@@ -101,6 +101,27 @@ Senzory výkonu mohou být ve W nebo kW.
| `statistics` | panel Energie | Vlastní senzory energie: `pv`, `grid_import`, `grid_export`, `battery_charge`, `battery_discharge` (u každého jedno id nebo seznam). |
| `statistics.max_power_kw` | `100` | Nejvyšší výkon, kterého instalace reálně dosáhne, například podle hlavního jističe domu. Řádek statistik s větší energií, než by tento výkon za daný čas dodal, je chybný skok počítadla (měřič spadl na nulu a vrátil se) a vynechá se; karta napíše, kolik jich vynechala. |
+### Skupiny spotřebičů
+
+Spotřebiče jde vnořovat, například hlavní elektroměr → byt → jeho spotřebiče. Položka s `children` je skupina. Klik na skupinu její spotřebiče skryje nebo zobrazí.
+
+```yaml
+appliances:
+ - name: Byt přízemí
+ power_entity: sensor.prizemi_vykon # podružný elektroměr bytu
+ children:
+ - { name: Pračka, power_entity: sensor.prizemi_pracka }
+ - { name: Sušička, power_entity: sensor.prizemi_susicka }
+ - name: Byt patro
+ collapsed: true # zavřená, dokud se neklikne
+ children: # bez elektroměru: skupina ukáže součet
+ - { name: Pračka, power_entity: sensor.patro_pracka }
+ - { name: Sušička, power_entity: sensor.patro_susicka }
+ - { name: Tepelné čerpadlo, power_entity: sensor.tc_vykon }
+```
+
+Skupina s vlastní `power_entity` ukazuje tento senzor; bez ní ukazuje součet svých spotřebičů. „Ostatní" na konci je spotřeba domu minus položky nejvyšší úrovně, proto spotřebiče ze skupin nesmí být uvedené ještě jednou samostatně.
+
### Bilance energie
Bilance čte dlouhodobé statistiky Home Assistantu, tedy stejná data jako panel Energie. Ve výchozím stavu si karta vezme senzory FVE, sítě a baterií nastavené v panelu Energie, proto ho nastav jako první. Jiné senzory zadáš pod `statistics`; musí to být senzory energie (kWh) se `state_class: total_increasing`.
diff --git a/README.md b/README.md
index 337871b..d935b5a 100644
--- a/README.md
+++ b/README.md
@@ -90,10 +90,10 @@ Power sensors may be in W or kW.
| `batteries[]` | – | `soc_entity`, optional `name`, and the power either as one signed sensor `power_entity` (positive = discharging, `invert` flips it) or as two always-positive sensors `charge_entity` + `discharge_entity`. |
| `battery_total_entity`, `battery_invert` | sum of batteries | Total battery power and its sign switch. |
| `battery_charge_entity`, `battery_discharge_entity` | – | Total battery power as two always-positive sensors. |
-| `appliances[]` | – | `power_entity`, `name`, optional `on_threshold_w`. |
+| `appliances[]` | – | `name`, `power_entity`, optional `on_threshold_w`. With `children` (a list of the same shape) the item becomes a group; `collapsed: true` starts it closed. See [Appliance groups](#appliance-groups). |
| `show_other` | `true` | Show the computed remainder of the house load. |
| `update_interval` | `1` | How often the card redraws live values, in seconds (1–20). Sensors are not read more often than they report. |
-| `animation` | `auto` | `auto` follows the system "reduce motion" setting, `always` ignores it, `off` never animates. |
+| `animation` | `auto` | `auto`: when the system asks for reduced motion, only the small flow dots move (they show the direction of energy) and the large dot and pulsing rings are hidden. `always` animates everything regardless, `off` never animates. |
| `animation_speed` | `1` | Multiplier for the flow speed. |
| `max_power` | `10000` | Power (W) at which the flow reaches full speed. |
| `show_w_not_kw` | `false` | Always show watts. |
@@ -101,6 +101,27 @@ Power sensors may be in W or kW.
| `statistics` | Energy dashboard | Override the energy sensors: `pv`, `grid_import`, `grid_export`, `battery_charge`, `battery_discharge` (one id or a list each). |
| `statistics.max_power_kw` | `100` | Highest power your installation can really reach, for example the main breaker of the house. A statistics row with more energy than that power could deliver in the time it covers is a counter glitch (the meter dropped to zero and came back) and is left out; the card says how many it skipped. |
+### Appliance groups
+
+Appliances can be nested, for example main meter → flat → its appliances. Give an item `children` and it becomes a group. A click on the group hides or shows its children.
+
+```yaml
+appliances:
+ - name: Ground floor
+ power_entity: sensor.ground_floor_power # sub-meter of the flat
+ children:
+ - { name: Washer, power_entity: sensor.gf_washer_power }
+ - { name: Dryer, power_entity: sensor.gf_dryer_power }
+ - name: First floor
+ collapsed: true # closed until clicked
+ children: # no sub-meter: the group shows the sum
+ - { name: Washer, power_entity: sensor.ff_washer_power }
+ - { name: Dryer, power_entity: sensor.ff_dryer_power }
+ - { name: Heat pump, power_entity: sensor.heat_pump_power }
+```
+
+A group with its own `power_entity` shows that sensor; without one it shows the sum of its children. "Other" at the end is the house load minus the top-level items, so children must not be listed a second time at the top level.
+
### Energy balance
The balance reads Home Assistant long-term statistics, the same data the Energy dashboard uses. By default the card takes the solar, grid and battery sensors configured in the Energy dashboard, so set that up first. To use other sensors, list them under `statistics`; they must be energy sensors (kWh) with `state_class: total_increasing`.
diff --git a/design/nahled-v6.html b/design/nahled-v6.html
new file mode 100644
index 0000000..57cf395
--- /dev/null
+++ b/design/nahled-v6.html
@@ -0,0 +1,719 @@
+
UltimatePower Card
+
+
+
+
+
+
+
UltimatePower Card
+
Živý náhled karty verze 0.3 se skupinami spotřebičů. Běží tu skutečný kód karty, data jsou ukázková a řídí se posuvníky vpravo.
+
+
+
+
+
+
Výroba a spotřeba
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Skupiny: klikni v kartě na kolečko 2NP nebo Dílna. Skupina se sbalí nebo rozbalí. Dílna začíná sbalená.
+
Baterie se v náhledu řídí sama: přebytek z FVE nabíjí, nedostatek vybíjí, nejvýš 3 kW. Zbytek jde ze sítě nebo do sítě.
+
+
+
+
+
+
diff --git a/docs/images/groups-dark-cs.png b/docs/images/groups-dark-cs.png
new file mode 100644
index 0000000..c426287
Binary files /dev/null and b/docs/images/groups-dark-cs.png differ
diff --git a/package-lock.json b/package-lock.json
index 4121132..8ca7983 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "ultimate-power-card",
- "version": "0.2.2",
+ "version": "0.3.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "ultimate-power-card",
- "version": "0.2.2",
+ "version": "0.3.0",
"license": "Apache-2.0",
"dependencies": {
"custom-card-helpers": "^1.9.0",
diff --git a/package.json b/package.json
index 07a4ee9..2847141 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "ultimate-power-card",
- "version": "0.2.2",
+ "version": "0.3.0",
"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",
diff --git a/src/styles.ts b/src/styles.ts
index 4d8c983..9fe5649 100644
--- a/src/styles.ts
+++ b/src/styles.ts
@@ -174,10 +174,11 @@ export const cardStyles = css`
stroke: none;
}
- /* animation: auto follows the system "reduce motion" setting,
- always ignores it, off never animates */
+ /* animation: auto - when the system asks for reduced motion, the small dots
+ keep flowing (they are the only thing that shows the direction of energy),
+ the large dot and the pulsing rings are dropped. always ignores the system
+ setting, off never animates. */
@media (prefers-reduced-motion: reduce) {
- ha-card:not(.anim-always) .stream,
ha-card:not(.anim-always) .lead,
ha-card:not(.anim-always) .pulse {
animation: none;
diff --git a/src/types.ts b/src/types.ts
index 0580e5b..785739e 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -20,10 +20,18 @@ export interface BatteryConfig {
invert?: boolean;
}
+/**
+ * An appliance, or a group of them (a flat, a floor, a sub-meter) when it has
+ * `children`. A group shows its own sensor if it has one, otherwise the sum of
+ * its children. Groups may be nested.
+ */
export interface ApplianceConfig {
- power_entity: string;
+ power_entity?: string;
name: string;
on_threshold_w?: number;
+ children?: ApplianceConfig[];
+ /** Start with the children hidden; a click on the group shows them. */
+ collapsed?: boolean;
}
/** Energy (kWh) statistics used by the balance section. Each may be one id or a list. */
@@ -68,8 +76,8 @@ export interface UltimatePowerCardConfig extends LovelaceCardConfig {
// Visual options
show_w_not_kw?: boolean;
/**
- * auto (default) follows the system "reduce motion" setting, always animates
- * regardless of it, off never animates.
+ * auto (default): with the system "reduce motion" setting on, only the small
+ * flow dots move; always animates everything regardless of it; off never animates.
*/
animation?: 'auto' | 'always' | 'off';
/** Multiplier for the flow animation speed. Default 1. */
diff --git a/src/ultimate-power-card.ts b/src/ultimate-power-card.ts
index 1d70ed4..e6bc0be 100644
--- a/src/ultimate-power-card.ts
+++ b/src/ultimate-power-card.ts
@@ -19,14 +19,29 @@ import {
type StatIds,
type StatsResult,
} from './statistics';
-import type { UltimatePowerCardConfig } from './types';
+import type { ApplianceConfig, UltimatePowerCardConfig } from './types';
-const VERSION = '0.2.2';
+const VERSION = '0.3.0';
type Pt = [number, number];
type Tone = 'pos' | 'neg';
type NodeState = Tone | 'idle';
+/** One appliance or group with its power already read. */
+interface AppNode {
+ name: string;
+ w: number;
+ entity?: string;
+ threshold: number;
+ kids: AppNode[];
+ key: string;
+ open: boolean;
+}
+/** A visible line of the appliance tree. */
+interface AppRow extends AppNode {
+ depth: number;
+}
+
/** Below this power (W) a node counts as idle and its flow is not animated. */
const IDLE_W = 5;
const MAX_OFFSET = 120;
@@ -72,6 +87,15 @@ const WORDS = {
const sum = (a: number[]): number => a.reduce((s, v) => s + v, 0);
+const appEntities = (list?: ApplianceConfig[]): (string | undefined)[] =>
+ (list ?? []).flatMap((a) => [a.power_entity, ...appEntities(a.children)]);
+
+/** Shortens a label to the width it has; SVG text does not wrap or clip by itself. */
+const fit = (text: string, px: number, fontPx = 11): string => {
+ const max = Math.max(3, Math.floor(px / (fontPx * 0.56)));
+ return text.length > max ? text.slice(0, max - 1).trimEnd() + '…' : text;
+};
+
@customElement('ultimate-power-card')
export class UltimatePowerCard extends LitElement {
static styles = cardStyles;
@@ -88,6 +112,8 @@ export class UltimatePowerCard extends LitElement {
private _request = 0;
private _timer?: number;
private _swipeX: number | null = null;
+ /** Groups whose open/closed state the user flipped by clicking, by tree path. */
+ @state() private _toggled = new Set();
private _lastRender = 0;
private _pending?: number;
@@ -196,7 +222,7 @@ export class UltimatePowerCard extends LitElement {
c.battery_discharge_entity,
...(c.pv_strings ?? []).map((s) => s.power_entity),
...(c.batteries ?? []).flatMap((b) => [b.power_entity, b.charge_entity, b.discharge_entity, b.soc_entity]),
- ...(c.appliances ?? []).map((a) => a.power_entity),
+ ...appEntities(c.appliances),
].filter((e): e is string => !!e);
}
@@ -275,15 +301,47 @@ export class UltimatePowerCard extends LitElement {
`;
}
- private _smallNode(p: Pt, st: NodeState, label: string, entity?: string): SVGTemplateResult {
+ private _smallNode(
+ p: Pt,
+ st: NodeState,
+ label: string,
+ entity?: string,
+ r = 22,
+ onClick?: () => void,
+ ): SVGTemplateResult {
+ const click = onClick ?? (entity ? () => this._moreInfo(entity) : undefined);
return svg`
- this._moreInfo(entity)}>
-
- ${this._text(p[0], p[1] + 4, 't-v', 10.5, label)}
+
+
+ ${this._text(p[0], p[1] + (r < 20 ? 3 : 4), 't-v', r < 20 ? 8.5 : 10.5, label)}
`;
}
+ /** Reads the appliance tree; a group without its own sensor is the sum of its children. */
+ private _appTree(list: ApplianceConfig[] | undefined, prefix: string): AppNode[] {
+ return (list ?? []).map((a, i) => {
+ const key = `${prefix}${i}`;
+ const kids = this._appTree(a.children, `${key}.`);
+ const flipped = this._toggled.has(key);
+ return {
+ name: a.name,
+ w: a.power_entity ? Math.max(0, readPower(this.hass!, a.power_entity)) : sum(kids.map((k) => k.w)),
+ entity: a.power_entity,
+ threshold: a.on_threshold_w ?? IDLE_W,
+ kids,
+ key,
+ open: kids.length > 0 && (a.collapsed ? flipped : !flipped),
+ };
+ });
+ }
+
+ private _toggle(key: string): void {
+ const next = new Set(this._toggled);
+ if (!next.delete(key)) next.add(key);
+ this._toggled = next;
+ }
+
private _renderFlow(): TemplateResult {
const cfg = this._config!;
const hass = this.hass!;
@@ -298,20 +356,32 @@ export class UltimatePowerCard extends LitElement {
const pvW = strings.map((x) => Math.max(0, readPower(hass, x.power_entity)));
const batW = bats.map((b) => readBattery(hass, b));
- const apps = (cfg.appliances ?? []).map((a) => ({
- name: a.name,
- w: Math.max(0, readPower(hass, a.power_entity)),
- entity: a.power_entity as string | undefined,
- threshold: a.on_threshold_w ?? IDLE_W,
- }));
- if (apps.length && cfg.show_other !== false) {
- apps.push({
+ const tree = this._appTree(cfg.appliances, '');
+ if (tree.length && cfg.show_other !== false) {
+ tree.push({
name: t.other,
- w: Math.max(0, s.house_w - sum(apps.map((a) => a.w))),
+ w: Math.max(0, s.house_w - sum(tree.map((a) => a.w))),
entity: undefined,
threshold: IDLE_W,
+ kids: [],
+ key: 'other',
+ open: false,
});
}
+ // The tree as visible lines: children follow their group when it is open.
+ const apps: AppRow[] = [];
+ const walk = (nodes: AppNode[], depth: number): void =>
+ nodes.forEach((n) => {
+ apps.push({ ...n, depth });
+ if (n.open) walk(n.kids, depth + 1);
+ });
+ walk(tree, 0);
+ // With groups the tree moves to the left edge so that indented children
+ // and their names fit beside the grid node.
+ const nested = tree.some((n) => n.kids.length > 0);
+ const appX0 = nested ? 44 : 100;
+ const APP_INDENT = 34;
+ const appR = (depth: number): number => (depth === 0 ? (nested ? 20 : 22) : 15);
// ---- layout: 480 units wide, height follows the longest column
const prodY = strings.length ? 150 : 56;
@@ -323,7 +393,12 @@ export class UltimatePowerCard extends LitElement {
const GRID: Pt = [240, hubY + 120];
const pvStep = Math.min(110, 400 / Math.max(1, strings.length));
const pvPos: Pt[] = strings.map((_, i) => [240 + (i - (strings.length - 1) / 2) * pvStep, 50]);
- const appPos: Pt[] = apps.map((_, i) => [100, hubY + 90 + i * 50]);
+ const appPos: Pt[] = [];
+ apps.forEach((a, i) => {
+ const prev = apps[i - 1];
+ const step = !prev ? 0 : a.depth === 0 ? (prev.depth === 0 ? 50 : 46) : prev.depth < a.depth ? 42 : 36;
+ appPos.push([appX0 + a.depth * APP_INDENT, i ? appPos[i - 1][1] + step : hubY + 90]);
+ });
const batPos: Pt[] = bats.map((_, i) => [380, hubY + 92 + i * 56]);
const height = Math.max(
hubY + 216,
@@ -343,11 +418,39 @@ export class UltimatePowerCard extends LitElement {
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.
- apps.forEach((_, i) =>
- wires.push(
- this._wire(i ? appPos[i - 1] : HOUSE, appPos[i], sum(apps.slice(i).map((a) => a.w)), 'neg', false),
- ),
- );
+ // Power still to be delivered along the vertical line of level `level` from
+ // line `from` on: the remaining children of the group that owns the line,
+ // and on the main line also every later top-level item.
+ const below = (level: number, from: number): number => {
+ let total = 0;
+ let q = from;
+ for (; q < apps.length && apps[q].depth > level; q++) if (apps[q].depth === level + 1) total += apps[q].w;
+ if (level === 0) for (; q < apps.length; q++) if (apps[q].depth === 0) total += apps[q].w;
+ return total;
+ };
+ // Last point reached on the vertical line of each level.
+ const tip: Pt[] = [HOUSE];
+ apps.forEach((a, i) => {
+ const p = appPos[i];
+ if (a.depth === 0) {
+ wires.push(this._wire(tip[0], p, below(0, i), 'neg', false));
+ } else {
+ const joint: Pt = [appX0 + (a.depth - 1) * APP_INDENT, p[1]];
+ wires.push(this._wire(tip[a.depth - 1], joint, below(a.depth - 1, i), 'neg', false));
+ wires.push(this._wire(joint, p, a.w, 'neg', false));
+ tip[a.depth - 1] = joint;
+ }
+ tip[a.depth] = p;
+ });
+ // Names are cut to the room they have: the grid node and the battery
+ // column take part of the width at their height.
+ const lastBatY = bats.length ? batPos[bats.length - 1][1] : hubY;
+ const appLabel = (a: AppRow, p: Pt): SVGTemplateResult => {
+ const x = p[0] + appR(a.depth) + 8;
+ const right = p[1] > hubY + 76 && p[1] < hubY + 212 ? 196 : hasBat && p[1] < lastBatY + 34 ? 290 : 472;
+ const mark = a.kids.length ? (a.open ? '▾ ' : '▸ ') : '';
+ return this._text(x, p[1] + 4, 't-n', a.depth ? 10 : 11, fit(mark + a.name, right - x, a.depth ? 10 : 11), 'start');
+ };
bats.forEach((_, i) => {
const rest = sum(batW.slice(i));
wires.push(this._wire(i ? batPos[i - 1] : BAT, batPos[i], Math.abs(rest), 'pos', false, rest >= 0));
@@ -380,8 +483,15 @@ export class UltimatePowerCard extends LitElement {
${this._text(100, hubY - 48, 't-n', 13, t.consumption)}
${apps.map(
(a, i) => svg`
- ${this._smallNode(appPos[i], a.w > a.threshold ? 'neg' : 'idle', this._power(a.w), a.entity)}
- ${this._text(130, appPos[i][1] + 4, 't-n', 11, a.name, 'start')}
+ ${this._smallNode(
+ appPos[i],
+ a.w > a.threshold ? 'neg' : 'idle',
+ this._power(a.w),
+ a.entity,
+ appR(a.depth),
+ a.kids.length ? () => this._toggle(a.key) : undefined,
+ )}
+ ${appLabel(a, appPos[i])}
`,
)}