Skip to content

Commit

Permalink
fix: reorder dynamic colour for battery. Solar from top
Browse files Browse the repository at this point in the history
fix: off_threshold is based on watt (W) value. Fixes #467
fix: extend dynamic colour to inverter path on full card. Only active if dynamic colour is enabled for load and battery objects closes #474
  • Loading branch information
slipx06 committed Jun 4, 2024
1 parent 0f8bbc8 commit e1af0d4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 33 deletions.
2 changes: 1 addition & 1 deletion dist/sunsynk-power-flow-card.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sunsynk-power-flow-card",
"version": "4.41.1",
"version": "4.41.2",
"description": "A customizable Home Assistant card to emulate the Sunsynk System flow that's displayed on the Inverter screen.",
"main": "sunsynk-power-flow-card.js",
"scripts": {
Expand Down
16 changes: 8 additions & 8 deletions src/cards/compact-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,14 +733,14 @@ export const compactCard = (config: sunsynkPowerFlowCardConfig, inverterImg: str
<defs>
<linearGradient id="bLg" x1="0%" x2="0%" y1="100%" y2="0%">
<stop offset="0%"
stop-color="${Number(data.pvPercentageBat) > 0 ? data.solarColour : (Number(data.gridPercentageBat) > 0 ? data.gridColour : data.batteryColour)}"/>
<stop offset="${data.pvPercentageBat < 2 ? 0 : data.pvPercentageBat}%"
stop-color="${Number(data.pvPercentageBat) > 0 ? data.solarColour : (Number(data.gridPercentageBat) > 0 ? data.gridColour : data.batteryColour)}"/>
<stop offset="${data.pvPercentageBat < 2 ? 0 : data.pvPercentageBat}%"
stop-color="${Number(data.gridPercentageBat) > 0 ? data.gridColour : data.batteryColour}"/>
<stop offset="${(Number(data.pvPercentageBat < 2 ? 0 : data.pvPercentageBat) + Number(data.gridPercentageBat < 2 ? 0 : data.gridPercentageBat))}%"
stop-color="${Number(data.gridPercentageBat) > 0 ? `${data.gridColour}` : `${data.batteryColour}`}"/>
<stop offset="${(Number(data.pvPercentageBat < 2 ? 0 : data.pvPercentageBat) + Number(data.gridPercentageBat < 2 ? 0 : data.gridPercentageBat))}%"
stop-color="${data.gridPercentageBat > 0 ? data.gridColour : data.pvPercentageBat > 0 ? data.solarColour : data.batteryColour}"/>
<stop offset="${data.gridPercentageBat < 2 ? 0 : data.gridPercentageBat}%"
stop-color="${data.gridPercentageBat > 0 ? data.gridColour : data.pvPercentageBat > 0 ? data.solarColour : data.batteryColour}"/>
<stop offset="${data.gridPercentageBat < 2 ? 0 : data.gridPercentageBat}%"
stop-color="${data.pvPercentageBat > 0 ? data.solarColour : data.batteryColour}"/>
<stop offset="${(data.gridPercentageBat < 2 ? 0 : data.gridPercentageBat) + (data.pvPercentageBat < 2 ? 0 : data.pvPercentageBat)}%"
stop-color="${data.pvPercentageBat > 0 ? data.solarColour : data.batteryColour}"/>
<stop offset="${(data.gridPercentageBat < 2 ? 0 : data.gridPercentageBat) + (data.pvPercentageBat < 2 ? 0 : data.pvPercentageBat)}%"
stop-color="${data.batteryColour}"/>
<stop offset="100%"
stop-color="${data.batteryColour}"/>
Expand Down
18 changes: 9 additions & 9 deletions src/cards/full-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ export const fullCard = (config: sunsynkPowerFlowCardConfig, inverterImg: string
</svg>
<path d="${config.inverter.three_phase ? 'M 180 223 L 180 235' : 'M 180 212 L 180 235'}"
fill="none" stroke="${data.inverterColour}" stroke-width="${data.minLineWidth}" stroke-miterlimit="10"
fill="none" stroke="${config.battery.dynamic_colour && config.load.dynamic_colour ? data.flowInvColour : data.inverterColour}" stroke-width="${data.minLineWidth}" stroke-miterlimit="10"
pointer-events="stroke"/>
<svg id="load-flow">
Expand Down Expand Up @@ -835,14 +835,14 @@ export const fullCard = (config: sunsynkPowerFlowCardConfig, inverterImg: string
<defs>
<linearGradient id="bLg" x1="0%" x2="0%" y1="100%" y2="0%">
<stop offset="0%"
stop-color="${Number(data.pvPercentageBat) > 0 ? data.solarColour : (Number(data.gridPercentageBat) > 0 ? data.gridColour : data.batteryColour)}"/>
<stop offset="${data.pvPercentageBat < 2 ? 0 : data.pvPercentageBat}%"
stop-color="${Number(data.pvPercentageBat) > 0 ? data.solarColour : (Number(data.gridPercentageBat) > 0 ? data.gridColour : data.batteryColour)}"/>
<stop offset="${data.pvPercentageBat < 2 ? 0 : data.pvPercentageBat}%"
stop-color="${Number(data.gridPercentageBat) > 0 ? data.gridColour : data.batteryColour}"/>
<stop offset="${(Number(data.pvPercentageBat < 2 ? 0 : data.pvPercentageBat) + Number(data.gridPercentageBat < 2 ? 0 : data.gridPercentageBat))}%"
stop-color="${Number(data.gridPercentageBat) > 0 ? `${data.gridColour}` : `${data.batteryColour}`}"/>
<stop offset="${(Number(data.pvPercentageBat < 2 ? 0 : data.pvPercentageBat) + Number(data.gridPercentageBat < 2 ? 0 : data.gridPercentageBat))}%"
stop-color="${data.gridPercentageBat > 0 ? data.gridColour : data.pvPercentageBat > 0 ? data.solarColour : data.batteryColour}"/>
<stop offset="${data.gridPercentageBat < 2 ? 0 : data.gridPercentageBat}%"
stop-color="${data.gridPercentageBat > 0 ? data.gridColour : data.pvPercentageBat > 0 ? data.solarColour : data.batteryColour}"/>
<stop offset="${data.gridPercentageBat < 2 ? 0 : data.gridPercentageBat}%"
stop-color="${data.pvPercentageBat > 0 ? data.solarColour : data.batteryColour}"/>
<stop offset="${(data.gridPercentageBat < 2 ? 0 : data.gridPercentageBat) + (data.pvPercentageBat < 2 ? 0 : data.pvPercentageBat)}%"
stop-color="${data.pvPercentageBat > 0 ? data.solarColour : data.batteryColour}"/>
<stop offset="${(data.gridPercentageBat < 2 ? 0 : data.gridPercentageBat) + (data.pvPercentageBat < 2 ? 0 : data.pvPercentageBat)}%"
stop-color="${data.batteryColour}"/>
<stop offset="100%"
stop-color="${data.batteryColour}"/>
Expand Down
49 changes: 35 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,18 @@ export class SunsynkPowerFlowCard extends LitElement {
let batteryCurrentDirection = !stateBatteryCurrentDirection.isNaN() ? stateBatteryCurrentDirection.toNum(0) : null;
let genericInverterImage = config.inverter?.modern;

let decimalPlaces = config.decimal_places;
let decimalPlacesEnergy = config.decimal_places_energy;

let loadColour = this.colourConvert(config.load?.colour);
let auxDynamicColour = this.calculateAuxLoadColour(stateAuxPower, Utils.toNum(config.load?.off_threshold, 0)) || loadColour;
let auxDynamicColour = this.calculateAuxLoadColour(stateAuxPower.toPower(false), Utils.toNum(config.load?.off_threshold, 0)) || loadColour;
let auxOffColour = this.colourConvert(config.load?.aux_off_colour || auxDynamicColour);
let auxDynamicColourLoad1 = this.calculateAuxLoadColour(stateAuxLoad1, Utils.toNum(config.load?.off_threshold, 0)) || loadColour;
let auxDynamicColourLoad2 = this.calculateAuxLoadColour(stateAuxLoad2, Utils.toNum(config.load?.off_threshold, 0)) || loadColour;
let dynamicColourEssentialLoad1 = this.calculateEssentialLoadColour(stateEssentialLoad1, Utils.toNum(config.load?.off_threshold, 0)) || loadColour;
let dynamicColourEssentialLoad2 = this.calculateEssentialLoadColour(stateEssentialLoad2, Utils.toNum(config.load?.off_threshold, 0)) || loadColour;
let dynamicColourEssentialLoad3 = this.calculateEssentialLoadColour(stateEssentialLoad3, Utils.toNum(config.load?.off_threshold, 0)) || loadColour;
let dynamicColourEssentialLoad4 = this.calculateEssentialLoadColour(stateEssentialLoad4, Utils.toNum(config.load?.off_threshold, 0)) || loadColour;
let auxDynamicColourLoad1 = this.calculateAuxLoadColour(stateAuxLoad1.toPower(false), Utils.toNum(config.load?.off_threshold, 0)) || loadColour;
let auxDynamicColourLoad2 = this.calculateAuxLoadColour(stateAuxLoad2.toPower(false), Utils.toNum(config.load?.off_threshold, 0)) || loadColour;
let dynamicColourEssentialLoad1 = this.calculateEssentialLoadColour(stateEssentialLoad1.toPower(false), Utils.toNum(config.load?.off_threshold, 0)) || loadColour;
let dynamicColourEssentialLoad2 = this.calculateEssentialLoadColour(stateEssentialLoad2.toPower(false), Utils.toNum(config.load?.off_threshold, 0)) || loadColour;
let dynamicColourEssentialLoad3 = this.calculateEssentialLoadColour(stateEssentialLoad3.toPower(false), Utils.toNum(config.load?.off_threshold, 0)) || loadColour;
let dynamicColourEssentialLoad4 = this.calculateEssentialLoadColour(stateEssentialLoad4.toPower(false), Utils.toNum(config.load?.off_threshold, 0)) || loadColour;

config.title_colour = this.colourConvert(config.title_colour);

Expand Down Expand Up @@ -321,13 +324,13 @@ export class SunsynkPowerFlowCard extends LitElement {
break;
}

let dynamicColourNonEssentialLoad1 = Math.abs(stateNonessentialLoad1.toNum(0)) > Utils.toNum(config.grid?.off_threshold, 0)
let dynamicColourNonEssentialLoad1 = Math.abs(stateNonessentialLoad1.toPower(false)) > Utils.toNum(config.grid?.off_threshold, 0)
? gridColour
: 'grey';
let dynamicColourNonEssentialLoad2 = Math.abs(stateNonessentialLoad2.toNum(0)) > Utils.toNum(config.grid?.off_threshold, 0)
let dynamicColourNonEssentialLoad2 = Math.abs(stateNonessentialLoad2.toPower(false)) > Utils.toNum(config.grid?.off_threshold, 0)
? gridColour
: 'grey';
let dynamicColourNonEssentialLoad3 = Math.abs(stateNonessentialLoad3.toNum(0)) > Utils.toNum(config.grid?.off_threshold, 0)
let dynamicColourNonEssentialLoad3 = Math.abs(stateNonessentialLoad3.toPower(false)) > Utils.toNum(config.grid?.off_threshold, 0)
? gridColour
: 'grey';

Expand Down Expand Up @@ -382,8 +385,6 @@ export class SunsynkPowerFlowCard extends LitElement {
const iconNonessentialLoad2 = this.getEntity('grid.load2_icon', {state: config.grid?.load2_icon?.toString() ?? ''}).state;
const iconNonessentialLoad3 = this.getEntity('grid.load3_icon', {state: config.grid?.load3_icon?.toString() ?? ''}).state;

let decimalPlaces = config.decimal_places;
let decimalPlacesEnergy = config.decimal_places_energy;
let remainingSolar = config.entities.remaining_solar ? Utils.convertValueNew(stateRemainingSolar.state, stateRemainingSolar.attributes?.unit_of_measurement, decimalPlaces) : false;
let totalSolarGeneration = config.entities.total_pv_generation ? Utils.convertValueNew(stateTotalPVGeneration.state, stateTotalPVGeneration.attributes?.unit_of_measurement, 2) : false;
let largeFont = config.large_font;
Expand Down Expand Up @@ -958,6 +959,25 @@ export class SunsynkPowerFlowCard extends LitElement {
break;
}

let flowInvColour: string;
switch (true) {
case pvPercentage >= Utils.toNum(config.load?.path_threshold, 0):
flowInvColour = solarColour;
break;
case batteryPercentage >= Utils.toNum(config.load?.path_threshold, 0):
flowInvColour = batteryColour;
break;
case gridPercentage >= Utils.toNum(config.load?.path_threshold, 0):
flowInvColour = gridColour;
break;
case gridPercentageBat >= Utils.toNum(config.battery?.path_threshold, 0):
flowInvColour = gridColour;
break;
default:
flowInvColour = inverterColour;
break;
}

//console.log(`${pvPercentageBat} % PV to charge battery, ${gridPercentageBat} % Grid to charge battery`);

let essIcon: string;
Expand Down Expand Up @@ -1172,6 +1192,7 @@ export class SunsynkPowerFlowCard extends LitElement {
gridPercentage,
flowColour,
flowBatColour,
flowInvColour,
dynamicColourEssentialLoad1,
dynamicColourEssentialLoad2,
dynamicColourEssentialLoad3,
Expand Down Expand Up @@ -1261,15 +1282,15 @@ export class SunsynkPowerFlowCard extends LitElement {
calculateAuxLoadColour(state, threshold) {
return !this._config.load.aux_dynamic_colour
? this.colourConvert(this._config.load?.aux_colour)
: Math.abs(state.toNum(0)) > threshold
: Math.abs(state) > threshold
? this.colourConvert(this._config.load?.aux_colour)
: 'grey';
}

calculateEssentialLoadColour(state, threshold) {
return !this._config.load.dynamic_colour
? this.colourConvert(this._config.load?.colour)
: Math.abs(state.toNum(0)) > threshold
: Math.abs(state) > threshold
? this.colourConvert(this._config.load?.colour)
: 'grey';
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ export interface DataDto {
gridPercentage,
flowColour,
flowBatColour,
flowInvColour,
dynamicColourEssentialLoad1,
dynamicColourEssentialLoad2,
dynamicColourEssentialLoad3,
Expand Down

0 comments on commit e1af0d4

Please sign in to comment.