fix: show incomplete Symcon utility sums with Err prefix

This commit is contained in:
jhartworks committed 2026-11-11 10:08:20 +01:00
1 parent a804fc8e15
commit bb153490b0
14 files changed
+361 -293

No files matched your search

+5 -3
View File
@@ -11,7 +11,7 @@ import { canAnnounce, roomHasSpeaker } from "../live/announce.ts";
import { isSymcon, nativeText } from "../entities/symcon-values.ts";
import "./room-hvac.ts";
import "./utility-report.ts";
import { roomUtilityReadings } from "../utilities.ts";
import { roomUtilityReadings, utilityText } from "../utilities.ts";
import type { Building, Floor, LightMood, LightSetting, Room } from "../model.ts";
import { roomPower, roomPowerSensors } from "../heatmap.ts";
import { energyCost, loadRoomEnergy, type RoomEnergy } from "../live/room-energy.ts";
@@ -586,8 +586,10 @@ export class NfRoomPanel extends LitElement {
const lux = roomClimateValue(this.hass, this.floor ?? null, room, "illuminance");
if (lux !== null) out.push(`☀ ${formatNumber(this.hass, lux, 0)} lx`);
// what the room's devices draw now
const power = this.floor && this.building ? roomPower(this.hass, this.building, this.floor, room) : null;
if (power !== null) out.push(`⚡ ${powerText(this.hass, power)}`);
const meter = roomUtilityReadings(this.hass, room, this.building?.floors.flatMap((f) => f.rooms)).find((r) => r.kind === "electricity" && r.metric === "power" && r.unit === "W");
const power = !meter && this.floor && this.building ? roomPower(this.hass, this.building, this.floor, room) : null;
if (meter) out.push(`⚡ ${utilityText(this.hass, meter)}`);
else if (power !== null) out.push(`⚡ ${powerText(this.hass, power)}`);
// how long its windows and doors stand open (the one open longest)
const openSince = this.floor
? this.floor.openings
+1 -1
View File
@@ -14,7 +14,7 @@ class NfUtilityReport extends LitElement {
render() {
return html`${UTILITY_KINDS.filter((kind) => this.readings.some((r) => r.kind === kind)).map((kind) => html`<section aria-label=${UTILITY_LABELS[kind]}>
<h4>${UTILITY_ICONS[kind]} ${UTILITY_LABELS[kind]}</h4>
${this.readings.filter((r) => r.kind === kind).map((r) => html`<div class="reading" title=${`${r.manual ? "Stockwerkszähler" : "Bereichszähler"}${r.incomplete ? " · Wert nicht verfügbar" : ""}\n${r.sources.map((id) => this.hass.states[id]?.attributes.symcon_path || this.hass.states[id]?.attributes.friendly_name || id).join("\n")}`}>
${this.readings.filter((r) => r.kind === kind).map((r) => html`<div class="reading" title=${`${r.manual ? "Stockwerkszähler" : "Bereichszähler"}${r.incomplete ? " · Err: Mindestens ein Wert fehlt; angezeigt wird die Summe der verfügbaren Werte." : ""}\n${r.sources.map((id) => this.hass.states[id]?.attributes.symcon_path || this.hass.states[id]?.attributes.friendly_name || id).join("\n")}`}>
<span>${utilityMetricLabel(r)}${r.manual ? html` <small>· Zähler</small>` : ""}</span><b>${utilityText(this.hass, r)}</b>
</div>`)}
</section>`)}`;
+6 -5
View File
@@ -52,7 +52,7 @@ import { fixtureGlow } from "../entities/kinds.ts";
import { coverTiltEntity } from "../entities/covers.ts";
import { isSymcon, isSymconVariable, nativeBool, nativeText } from "../entities/symcon-values.ts";
import { WEATHER_VARIABLE_LABELS } from "../symcon-weather.ts";
import { floorUtilityReadings } from "../utilities.ts";
import { floorUtilityReadings, utilityText } from "../utilities.ts";
import "./utility-report.ts";
import { deviceValuesText } from "../entities/device-values.ts";
import { furnitureShown, hiddenFurniture, withoutHidden } from "../entities/visibility.ts";
@@ -2600,10 +2600,11 @@ export class NfView3d extends LitElement {
const hass = this.hass;
const rows: { icon: string; label: string; value: string }[] = [];
const floor = this.floorId ? b.floors.find((f) => f.id === this.floorId) : undefined;
const floorPower = floor && isSymcon(hass) ? floorUtilityReadings(hass, floor, b.floors.flatMap((f) => f.rooms)).find((r) => r.kind === "electricity" && r.metric === "power" && r.unit === "W")?.value ?? null : floor?.power_entity ? readPower(hass?.states[floor.power_entity]) : null;
if (floor && floorPower !== null) {
const share = consumption && consumption > 0 ? ` · ${translate(hass, "floor_power_share", { n: Math.round((Math.max(0, floorPower) / consumption) * 100) })}` : "";
rows.push({ icon: "⚡", label: floor.name, value: `${powerText(hass, floorPower)}${share}` });
const meter = floor && isSymcon(hass) ? floorUtilityReadings(hass, floor, b.floors.flatMap((f) => f.rooms)).find((r) => r.kind === "electricity" && r.metric === "power" && r.unit === "W") : undefined;
const floorPower = isSymcon(hass) ? meter?.value ?? null : floor?.power_entity ? readPower(hass?.states[floor.power_entity]) : null;
if (floor && (meter || floorPower !== null)) {
const share = !meter?.incomplete && floorPower !== null && consumption && consumption > 0 ? ` · ${translate(hass, "floor_power_share", { n: Math.round((Math.max(0, floorPower) / consumption) * 100) })}` : "";
rows.push({ icon: "⚡", label: floor.name, value: `${meter ? utilityText(hass, meter) : powerText(hass, floorPower!)}${share}` });
}
for (const [key, icon, label] of [["gas", "🔥", "energy_gas_short"], ["water", "💧", "energy_water_short"]] as const) {
const id = b.energy[key];
+20 -13
View File
@@ -3,7 +3,7 @@
import { fromCelsius, kindOf, openingState, primaryEntities, roomClimateValue, tempUnit, type OpeningEntities, roomEntities } from "./devices.ts";
import { readPower } from "./energy.ts";
import { floorUtilityReadings } from "./utilities.ts";
import { floorUtilityReadings, utilityText } from "./utilities.ts";
import { formatNumber, translate } from "./i18n.ts";
import { centroid, type Building, type Floor, type Room } from "./model.ts";
import type { HomeAssistant } from "./types.ts";
@@ -87,6 +87,8 @@ export function personsInRooms(hass: HomeAssistant, building: Building): PersonM
}
export interface FloorCounts {
/** Native meter text includes Err: when only part of the sum is available. */
powerLabel?: string;
rooms: number;
lightsOn: number;
open: number;
@@ -129,17 +131,21 @@ function openingsOnFloor(hass: HomeAssistant, floor: Floor, links: Map<string, O
/** Room, light, open-window and person counts per floor, for the floor labels. */
export function floorCounts(hass: HomeAssistant, building: Building, links: Map<string, OpeningEntities>, persons: PersonMarker[]): Map<string, FloorCounts> {
return new Map(
building.floors.map((floor) => [
floor.id,
{
rooms: floor.rooms.length,
lightsOn: lightsOnFloor(hass, floor),
open: openingsOnFloor(hass, floor, links),
persons: persons.filter((p) => p.floorId === floor.id).length,
power: isSymcon(hass) ? floorUtilityReadings(hass, floor, building.floors.flatMap((f) => f.rooms)).find((r) => r.kind === "electricity" && r.metric === "power" && r.unit === "W")?.value ?? null : floor.power_entity ? readPower(hass.states[floor.power_entity]) : null,
temps: tempRange(hass, floor),
},
]),
building.floors.map((floor) => {
const meter = isSymcon(hass) ? floorUtilityReadings(hass, floor, building.floors.flatMap((f) => f.rooms)).find((r) => r.kind === "electricity" && r.metric === "power" && r.unit === "W") : undefined;
return [
floor.id,
{
rooms: floor.rooms.length,
lightsOn: lightsOnFloor(hass, floor),
open: openingsOnFloor(hass, floor, links),
persons: persons.filter((p) => p.floorId === floor.id).length,
power: isSymcon(hass) ? meter?.value ?? null : floor.power_entity ? readPower(hass.states[floor.power_entity]) : null,
...(meter ? { powerLabel: utilityText(hass, meter) } : {}),
temps: tempRange(hass, floor),
},
];
}),
);
}
@@ -159,7 +165,8 @@ export function floorInfoText(hass: HomeAssistant | undefined, c: FloorCounts):
const [lo, hi] = c.temps.map((t) => formatNumber(hass, fromCelsius(hass, t), 0));
parts.push(lo === hi ? `${lo} ${tempUnit(hass)}` : `${lo}–${hi} ${tempUnit(hass)}`);
}
if (c.power != null) parts.push(`⚡ ${powerText(hass, c.power)}`);
const power = c.powerLabel ?? (c.power != null ? powerText(hass, c.power) : null);
if (power) parts.push(`⚡ ${power}`);
return parts.join(" · ");
}
+26 -4
View File
@@ -5,7 +5,7 @@ import type { HomeAssistant, SymconObject } from "./types.ts";
import { emptyBuilding, UTILITY_KINDS } from "./model.ts";
import { floorMeter, floorUtilityReadings, roomUtilityReadings, utilityText, utilityWatched } from "./utilities.ts";
import { roomPower, roomPowerSensors } from "./heatmap.ts";
import { floorCounts } from "./presence.ts";
import { floorCounts, floorInfoText } from "./presence.ts";
import { watchedEntities } from "./components/view3d/watch-list.ts";
const DUMMY = "{485D0419-BE97-4548-AA9C-C083EB82E61E}";
@@ -120,16 +120,38 @@ test("legacy floor power remains selected and an explicit automatic choice overr
assert.deepEqual(floorUtilityReadings(h, floor), []);
});
test("an unavailable meter makes its sum unavailable instead of reporting a partial total; live values update", () => {
test("an unavailable meter keeps the available partial sum with Err: and clears the marker on recovery", () => {
const f = fixture(); f.node(10, 0, "A"); f.node(11, 10, "Strom"); f.variable(12, 11, "Leistung", 1, "W");
f.node(20, 0, "B"); f.node(21, 20, "Strom"); const id = f.variable(22, 21, "Leistung", 2, "W");
const floor = floorWith([f.room(), f.room(20)]), h = f.hass();
assert.equal(floorUtilityReadings(h, floor)[0].value, 3);
const next = { ...h, states: { ...h.states, [id]: { ...h.states[id], state: "unavailable" } } } as HomeAssistant;
assert.equal(floorUtilityReadings(next, floor)[0].value, null);
assert.equal(floorUtilityReadings(next, floor)[0].incomplete, true);
const partial = floorUtilityReadings(next, floor)[0];
assert.equal(partial.value, 1);
assert.equal(partial.incomplete, true);
assert.equal(utilityText(next, partial), "Err: 1 W");
const counts = floorCounts(next, { ...emptyBuilding(), floors: [floor] }, new Map(), []).get(floor.id)!;
assert.equal(counts.power, 1);
assert.match(floorInfoText(next, counts), /⚡ Err: 1 W$/);
const live = { ...h, states: { ...h.states, [id]: { ...h.states[id], attributes: { ...h.states[id].attributes, symcon_value: 5 } } } };
assert.equal(floorUtilityReadings(live, floor)[0].value, 6);
assert.equal(utilityText(live, floorUtilityReadings(live, floor)[0]), "6 W");
});
test("partial zero sums stay zero; fully unavailable room and manual meter readings show Err: dash", () => {
const f = fixture(); f.node(10, 0, "A"); f.node(11, 10, "Wasser");
f.node(20, 11, "Zähler A", 1); const good = f.variable(21, 20, "Verbrauch", 0, "m³");
f.node(30, 11, "Zähler B", 1); const bad = f.variable(31, 30, "Verbrauch", 100, "m³");
const h = f.hass(); h.states[bad] = { ...h.states[bad], state: "unavailable" };
let rows = roomUtilityReadings(h, f.room());
assert.equal(rows[0].value, 0);
assert.equal(utilityText(h, rows[0]), "Err: 0 m³");
const unavailable = { ...h, states: { ...h.states, [good]: { ...h.states[good], state: "unknown" } } };
rows = roomUtilityReadings(unavailable, f.room());
assert.equal(rows[0].value, null);
assert.equal(utilityText(unavailable, rows[0]), "Err: –");
const floor = floorWith([f.room()], { utility_meters: { water: { energy: bad } } });
assert.equal(utilityText(h, floorUtilityReadings(h, floor)[0]), "Err: –");
});
test("the view watches automatic and all five manual floor meters even without heatmap or climate cards", () => {
+6 -5
View File
@@ -109,14 +109,14 @@ function readings(hass: HomeAssistant, sources: Source[], manual = false): Utili
const st = hass.states[id], unit = measure(st);
const canonicalUnit = unit?.metric === s.metric ? unit.unit : String(st?.attributes.unit_of_measurement ?? "").trim();
const key = `${s.kind}|${s.metric}|${canonicalUnit}|${s.period}`;
const row = groups.get(key) ?? { kind: s.kind, metric: s.metric, unit: canonicalUnit, value: 0, period: s.period, sources: [], manual, incomplete: false };
const row = groups.get(key) ?? { kind: s.kind, metric: s.metric, unit: canonicalUnit, value: null, period: s.period, sources: [], manual, incomplete: false };
const value = nativeNumber(st);
if (value === null) row.incomplete = true;
else row.value! += value * (unit?.metric === s.metric ? unit.factor : 1);
else row.value = (row.value ?? 0) + value * (unit?.metric === s.metric ? unit.factor : 1);
row.sources.push(id);
groups.set(key, row);
}
return [...groups.values()].map((row) => row.incomplete ? { ...row, value: null } : row);
return [...groups.values()];
}
export function roomUtilityReadings(hass: HomeAssistant, room: Room, rooms: readonly Room[] = []): UtilityReading[] {
@@ -152,9 +152,10 @@ export function utilityMetricLabel(row: UtilityReading): string {
}
export function utilityText(hass: HomeAssistant | undefined, row: UtilityReading): string {
if (row.value === null) return "–";
const prefix = row.incomplete ? "Err: " : "";
if (row.value === null) return `${prefix}–`;
const value = row.unit === "W" && Math.abs(row.value) >= 1000 ? row.value / 1000 : row.value;
const unit = row.unit === "W" && Math.abs(row.value) >= 1000 ? "kW" : row.unit;
const digits = Number.isInteger(value) ? 0 : Math.min(6, Math.max(2, 1 - Math.floor(Math.log10(Math.abs(value)))));
return `${formatNumber(hass, value, digits)}${unit ? ` ${unit}` : ""}`;
return `${prefix}${formatNumber(hass, value, digits)}${unit ? ` ${unit}` : ""}`;
}