Skip to content

Commit

Permalink
fix: autoscale when pv sensor data is in kW. Changes to Solis battery…
Browse files Browse the repository at this point in the history
… direction
  • Loading branch information
slipx06 committed Mar 23, 2024
1 parent cccbc7d commit 00b6e16
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 17 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.24.4",
"version": "4.24.5",
"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: 12 additions & 4 deletions src/cards/compact-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,28 +1135,36 @@ export const compactCard = (config: sunsynkPowerFlowCardConfig, inverterImg: str
class="${data.largeFont !== true ? 'st14' : 'st4'} st8"
display="${!config.show_solar || !data.statePV1Power.isValid() ? 'none' : ''}"
fill="${data.solarColour}">
${data.statePV1Power?.toPowerString(config.solar.auto_scale, data.decimalPlaces)}
${config.solar.auto_scale
? Utils.convertValue(data.pv1PowerWatts, data.decimalPlaces) || 0
: `${Utils.toNum(data.pv1PowerWatts || 0, 0)} ${UnitOfPower.WATT}`}
</text>
</a>
<a href="#" @click=${(e) => Utils.handlePopup(e, config.entities.pv2_power_187)}>
<text id="pv2_power_187" x="289.5" y="71" class="${data.largeFont !== true ? 'st14' : 'st4'} st8"
display="${!config.show_solar || config.solar.mppts === 1 || !data.statePV2Power.isValid() ? 'none' : ''}"
fill="${data.solarColour}">
${data.statePV2Power?.toPowerString(config.solar.auto_scale, data.decimalPlaces)}
${config.solar.auto_scale
? Utils.convertValue(data.pv2PowerWatts, data.decimalPlaces) || 0
: `${Utils.toNum(data.pv2PowerWatts || 0, 0)} ${UnitOfPower.WATT}`}
</text>
</a>
<a href="#" @click=${(e) => Utils.handlePopup(e, config.entities.pv3_power_188)}>
<text id="pv3_power_188" x="113" y="71" class="${data.largeFont !== true ? 'st14' : 'st4'} st8"
display="${!config.show_solar || [1, 2].includes(config.solar.mppts) || !data.statePV3Power.isValid() ? 'none' : ''}"
fill="${data.solarColour}">
${data.statePV3Power?.toPowerString(config.solar.auto_scale, data.decimalPlaces)}
${config.solar.auto_scale
? Utils.convertValue(data.pv3PowerWatts, data.decimalPlaces) || 0
: `${Utils.toNum(data.pv3PowerWatts || 0, 0)} ${UnitOfPower.WATT}`}
</text>
</a>
<a href="#" @click=${(e) => Utils.handlePopup(e, config.entities.pv4_power_189)}>
<text id="pv4_power_189" x="366" y="71" class="${data.largeFont !== true ? 'st14' : 'st4'} st8"
display="${!config.show_solar || [1, 2, 3].includes(config.solar.mppts) || !data.statePV4Power.isValid() ? 'none' : ''}"
fill="${data.solarColour}">
${data.statePV4Power?.toPowerString(config.solar.auto_scale, data.decimalPlaces)}
${config.solar.auto_scale
? Utils.convertValue(data.pv4PowerWatts, data.decimalPlaces) || 0
: `${Utils.toNum(data.pv4PowerWatts || 0, 0)} ${UnitOfPower.WATT}`}
</text>
</a>
${config.entities?.essential_power && config.entities.essential_power !== 'none'
Expand Down
16 changes: 12 additions & 4 deletions src/cards/full-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1605,28 +1605,36 @@ export const fullCard = (config: sunsynkPowerFlowCardConfig, inverterImg: string
<text id="pv1_power_186" x="36.5" y="56.5" class="${data.largeFont !== true ? 'st14' : 'st4'} st8"
display="${!config.show_solar || !data.statePV1Power.isValid() ? 'none' : ''}"
fill="${data.solarColour}" >
${data.statePV1Power.toPowerString(config.solar.auto_scale, data.decimalPlaces)}
${config.solar.auto_scale
? Utils.convertValue(data.pv1PowerWatts, data.decimalPlaces) || 0
: `${Utils.toNum(data.pv1PowerWatts || 0, 0)} ${UnitOfPower.WATT}`}
</text>
</a>
<a href="#" @click=${(e) => Utils.handlePopup(e, config.entities.pv2_power_187)}>
<text id="pv2_power_187" x="137" y="56.5" class="${data.largeFont !== true ? 'st14' : 'st4'} st8"
display="${!config.show_solar || config.solar.mppts === 1 || !data.statePV2Power.isValid() ? 'none' : ''}"
fill="${data.solarColour}">
${data.statePV2Power.toPowerString(config.solar.auto_scale, data.decimalPlaces)}
${config.solar.auto_scale
? Utils.convertValue(data.pv2PowerWatts, data.decimalPlaces) || 0
: `${Utils.toNum(data.pv2PowerWatts || 0, 0)} ${UnitOfPower.WATT}`}
</text>
</a>
<a href="#" @click=${(e) => Utils.handlePopup(e, config.entities.pv3_power_188)}>
<text id="pv3_power_188" x="36.5" y="117" class="${data.largeFont !== true ? 'st14' : 'st4'} st8"
display="${!config.show_solar || [1, 2].includes(config.solar.mppts) || !data.statePV3Power.isValid() ? 'none' : ''}"
fill="${data.solarColour}">
${data.statePV3Power.toPowerString(config.solar.auto_scale, data.decimalPlaces)}
${config.solar.auto_scale
? Utils.convertValue(data.pv3PowerWatts, data.decimalPlaces) || 0
: `${Utils.toNum(data.pv3PowerWatts || 0, 0)} ${UnitOfPower.WATT}`}
</text>
</a>
<a href="#" @click=${(e) => Utils.handlePopup(e, config.entities.pv4_power_189)}>
<text id="pv4_power_189" x="137" y="117" class="${data.largeFont !== true ? 'st14' : 'st4'} st8"
display="${!config.show_solar || [1, 2, 3].includes(config.solar.mppts) || !data.statePV3Power.isValid() ? 'none' : ''}"
fill="${data.solarColour}">
${data.statePV4Power.toPowerString(config.solar.auto_scale, data.decimalPlaces)}
${config.solar.auto_scale
? Utils.convertValue(data.pv4PowerWatts, data.decimalPlaces) || 0
: `${Utils.toNum(data.pv4PowerWatts || 0, 0)} ${UnitOfPower.WATT}`}
</text>
</a>
<a href="#" @click=${(e) => Utils.handlePopup(e, config.entities.aux_load1)}>
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export class Utils {

static convertValue(value, decimal = 2) {
decimal = Number.isNaN(decimal) ? 2 : decimal;
if (Math.abs(value) > 999) {
if (Math.abs(value) >= 1000000) {
return `${(value / 1000000).toFixed(decimal)} MW`;
} else if (Math.abs(value) >= 1000) {
return `${(value / 1000).toFixed(decimal)} kW`;
} else {
return `${Math.round(value)} W`;
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ export class SunsynkPowerFlowCard extends LitElement {

let totalGridPower = config.inverter.three_phase ? gridPowerTotal : gridPower;

let gridVoltage = !stateGridVoltage.isNaN() ? Utils.toNum(stateGridVoltage.state) : null;
let batteryCurrentDirection = !stateBatteryCurrentDirection.isNaN() ? parseInt(stateBatteryCurrentDirection.state) : null;
let gridVoltage = !stateGridVoltage.isNaN() ? stateGridVoltage.toNum(0) : null;
let batteryCurrentDirection = !stateBatteryCurrentDirection.isNaN() ? stateBatteryCurrentDirection.toNum(0) : null;
let genericInverterImage = config.inverter?.modern;

let loadColour = this.colourConvert(config.load?.colour);
Expand Down Expand Up @@ -535,7 +535,7 @@ export class SunsynkPowerFlowCard extends LitElement {
gridStatus = gridVoltage > 50 ? 'on' : 'off';
}

if (batteryCurrentDirection != null && !Number.isNaN(batteryCurrentDirection)) {
if (batteryCurrentDirection != null) {
if (inverterModel == InverterModel.Solis && batteryCurrentDirection === 0) {
batteryPower = -batteryPower;
}
Expand Down
13 changes: 10 additions & 3 deletions src/inverters/dto/custom-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,16 @@ export function convertToCustomEntity(entity: any): CustomEntity {
isValid: () => entity?.state !== null && entity.state !== undefined && entity.state !== 'unknown' || false,
notEmpty: () => entity?.state !== '' || false,
isNaN: () => Number.isNaN(entity?.state) || true,
toPower: (invert?: boolean) => (entity.attributes?.unit_of_measurement || '').toLowerCase() === 'kw'
? Utils.toNum(((entity?.state || '0') * 1000), 0, invert)
: Utils.toNum((entity?.state || '0'), 0, invert) || 0,
toPower: (invert?: boolean) => {
const unit = (entity.attributes?.unit_of_measurement || '').toLowerCase();
if (unit === 'kw') {
return Utils.toNum(((entity?.state || '0') * 1000), 0, invert);
} else if (unit === 'mw') {
return Utils.toNum(((entity?.state || '0') * 1000000), 0, invert);
} else {
return Utils.toNum((entity?.state || '0'), 0, invert) || 0;
}
},
toPowerString: (scale?: boolean, decimals?: number, invert?: boolean) =>
scale ?
Utils.convertValueNew(entity?.state, entity?.attributes?.unit_of_measurement, decimals || 0) :
Expand Down

0 comments on commit 00b6e16

Please sign in to comment.