Skip to content

Commit ffe6775

Browse files
committed
Fix statistics handling for negating power charge
1 parent 7bc8f44 commit ffe6775

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

src/data/energy.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,21 +1374,16 @@ export const calculateSolarConsumedGauge = (
13741374
/**
13751375
* Get current power value from entity state, normalized to kW
13761376
* @param stateObj - The entity state object to get power value from
1377-
* @param negateValue - Whether a bi-directional power value should be negated
13781377
* @returns Power value in kW, or 0 if entity not found or invalid
13791378
*/
1380-
export const getPowerFromState = (
1381-
stateObj: HassEntity,
1382-
negateValue = false
1383-
): number | undefined => {
1379+
export const getPowerFromState = (stateObj: HassEntity): number | undefined => {
13841380
if (!stateObj) {
13851381
return undefined;
13861382
}
1387-
let value = parseFloat(stateObj.state);
1383+
const value = parseFloat(stateObj.state);
13881384
if (isNaN(value)) {
13891385
return undefined;
13901386
}
1391-
if (negateValue) value = -value;
13921387

13931388
// Normalize to kW based on unit of measurement (case-sensitive)
13941389
// Supported units: GW, kW, MW, mW, TW, W

src/panels/lovelace/cards/energy/hui-power-sankey-card.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,9 @@ class HuiPowerSankeyCard
736736
// Track this entity for state change detection
737737
this._entities.add(entityId);
738738

739-
return getPowerFromState(this.hass.states[entityId], negateValue) ?? 0;
739+
let value = getPowerFromState(this.hass.states[entityId]) ?? 0;
740+
if (negateValue) value = -value;
741+
return value;
740742
}
741743

742744
/**

src/panels/lovelace/cards/energy/hui-power-sources-graph-card.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,22 @@ export class HuiPowerSourcesGraphCard
225225
// The interpolation breaks the stacking, so this positive/negative is a workaround
226226
const { positive, negative } = this._processData(
227227
statIds[key].stats.map((stat: StatsArgs) => {
228-
const stats = energyData.stats[stat.id] ?? [];
229-
const currentState = getPowerFromState(
230-
this.hass.states[stat.id],
231-
!!stat.negate
232-
);
228+
let stats = energyData.stats[stat.id] ?? [];
229+
const currentState = getPowerFromState(this.hass.states[stat.id]);
233230
if (currentState !== undefined) {
234231
stats.push({ start: now, end: now, mean: currentState });
235232
}
233+
if (stat.negate) {
234+
stats = stats.map((point) => {
235+
if (point.mean)
236+
return {
237+
start: point.start,
238+
end: point.end,
239+
mean: -point.mean,
240+
};
241+
return point;
242+
});
243+
}
236244
return stats;
237245
})
238246
);

0 commit comments

Comments
 (0)