Skip to content

Commit

Permalink
add Battery's Show Remaining Energy to shutdown option
Browse files Browse the repository at this point in the history
  • Loading branch information
molikk committed Jan 3, 2025
1 parent 374aef1 commit 38cf7ed
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 49 deletions.
49 changes: 25 additions & 24 deletions docs/configuration.md

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion docs/examples/victron.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Integration via https://github.com/sfstar/hass-victron
:linenos:
type: custom:mlk-power-flow-card
panel_mode: false
large_font: false
title: Victron - Power Monitor
title_colour: White
Expand Down
9 changes: 7 additions & 2 deletions src/cards/compact/battery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ export class Battery {
</a>`;
}


static generateCapacity(data: DataDto, config: PowerFlowCardConfig) {
const x = Battery.showInnerBatteryBanks(config) ? 202 : (Battery.showOuterBatteryBanks(config) ? 322 : 270);
const y = Battery.showInnerBatteryBanks(config) ? 325 : (Battery.showOuterBatteryBanks(config) ? 307 : 338);
Expand All @@ -212,11 +211,17 @@ export class Battery {
</text>
</a>`;
}

let shutdown = config.battery.shutdown_soc_offgrid || config.battery.shutdown_soc || 0;
let storage = config.battery.remaining_energy_to_shutdown
? Utils.toNum((data.batteryEnergy * ((data.stateBatterySoc.toNum(2) - shutdown) / 100) / 1000), 2).toFixed(2)
: Utils.toNum((data.batteryEnergy * (data.stateBatterySoc.toNum(2) / 100) / 1000), 2).toFixed(2);

return svg`
<text x="${x}" y="${y}" class="st3 ${align}"
display="${!config.battery.show_remaining_energy ? 'none' : ''}"
fill="${data.batteryColour}">
${Utils.toNum((data.batteryEnergy * (data.stateBatterySoc.toNum(2) / 100) / 1000), 2)}
${storage}
${UnitOfEnergy.KILO_WATT_HOUR}
</text>`;
}
Expand Down
39 changes: 21 additions & 18 deletions src/cards/compact/batteryBank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class BatteryBank {
return svg``;
}
let banks = config.battery.battery_banks;
banks = banks > 4?4:banks;
banks = banks > 4 ? 4 : banks;
let columns = [281, 312, 343, 374];
let gap = 10, width = 28;
if (banks <= 2) {
Expand Down Expand Up @@ -91,15 +91,9 @@ export class BatteryBank {
) {
if (id <= banks) {
const textX = column + width / 2 + gap;
const power = config.battery.auto_scale
? config.battery.show_absolute
? Utils.convertValueNew(Math.abs(powerEntity.toNum(decimalPlaces)), powerEntity.getUOM(), decimalPlaces, false)
: Utils.convertValueNew(powerEntity.toNum(decimalPlaces), powerEntity.getUOM(), decimalPlaces, false) || '0'
: powerEntity.toStr(config.decimal_places, config.battery?.invert_power, config.battery.show_absolute);
const power = this.getPower(config, powerEntity, decimalPlaces);
const storage = this.getStorage(storageEntity, config, batteryEnergy, socEntity);

const storage = storageEntity?.isValid()
? storageEntity.toStr(2)
: Utils.toNum((batteryEnergy * (socEntity.toNum(2) / 100) / 1000), 2).toFixed(2);
return svg`
<rect x="${column + gap}" y="274" width="${width}" height="66" rx="4.5" ry="4.5" fill="none" pointer-events="all" stroke="${colour}"></rect>
<text x="${textX}" y="283" class="st15" fill="${colour}" id="battery_bank_${id}_power">${power}</text>
Expand Down Expand Up @@ -182,17 +176,10 @@ export class BatteryBank {
) {
if (id <= banks) {
const textX = column + width / 2 - 3;
const power = config.battery.auto_scale
? config.battery.show_absolute
? Utils.convertValueNew(Math.abs(powerEntity.toNum(decimalPlaces)), powerEntity.getUOM(), decimalPlaces, false)
: Utils.convertValueNew(powerEntity.toNum(decimalPlaces), powerEntity.getUOM(), decimalPlaces, false) || '0'
: powerEntity.toStr(config.decimal_places, config.battery?.invert_power, config.battery.show_absolute);

const power = this.getPower(config, powerEntity, decimalPlaces);
const x = column - 3 + width / 2;
const storage = this.getStorage(storageEntity, config, batteryEnergy, socEntity);

const storage = storageEntity?.isValid()
? storageEntity.toStr(2)
: Utils.toNum((batteryEnergy * (socEntity.toNum(2) / 100) / 1000), 2).toFixed(2);
return svg`
<svg id="battery-pack-flow-${id}" style="overflow: visible">
<path id="bat-line"
Expand All @@ -214,4 +201,20 @@ export class BatteryBank {
return svg``;
}

private static getStorage(storageEntity: CustomEntity, config: PowerFlowCardConfig, batteryEnergy: number, socEntity: CustomEntity) {
let shutdown = config.battery.shutdown_soc_offgrid || config.battery.shutdown_soc || 0;
return storageEntity?.isValid()
? storageEntity.toStr(2)
: config.battery.remaining_energy_to_shutdown
? Utils.toNum((batteryEnergy * ((socEntity.toNum(2) - shutdown) / 100) / 1000), 2).toFixed(2)
: Utils.toNum((batteryEnergy * (socEntity.toNum(2) / 100) / 1000), 2).toFixed(2);
}

private static getPower(config: PowerFlowCardConfig, powerEntity: CustomEntity, decimalPlaces: number) {
return config.battery.auto_scale
? config.battery.show_absolute
? Utils.convertValueNew(Math.abs(powerEntity.toNum(decimalPlaces)), powerEntity.getUOM(), decimalPlaces, false)
: Utils.convertValueNew(powerEntity.toNum(decimalPlaces), powerEntity.getUOM(), decimalPlaces, false) || '0'
: powerEntity.toStr(config.decimal_places, config.battery?.invert_power, config.battery.show_absolute);
}
}
1 change: 1 addition & 0 deletions src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default {
colour: 'pink',
show_daily: false,
show_remaining_energy: true,
remaining_energy_to_shutdown: false,
animation_speed: 6,
max_power: 4500,
show_absolute: false,
Expand Down
1 change: 1 addition & 0 deletions src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ export class ConfigurationCardEditor extends LitElement implements LovelaceCardE
{ name: 'animation_speed', selector: { number: {} } },
{ name: 'hide_soc', selector: { boolean: {} } },
{ name: 'show_remaining_energy', selector: { boolean: {} } },
{ name: 'remaining_energy_to_shutdown', selector: { boolean: {} } },
{ name: 'max_power', selector: { number: {} } },
{ name: 'path_threshold', selector: { number: {} } },
],
Expand Down
18 changes: 15 additions & 3 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { unitOfEnergyConversionRules, UnitOfEnergyOrPower, UnitOfPower } from '../const';
import { UnitOfEnergy, unitOfEnergyConversionRules, UnitOfEnergyOrPower, UnitOfPower } from '../const';
import { navigate } from 'custom-card-helpers';

export class Utils {
Expand Down Expand Up @@ -52,26 +52,38 @@ export class Utils {
return `${this.toNum(numberValue, decimal)}`;
}

if (unit === UnitOfPower.WATT && Math.abs(numberValue) < 1000) {
if ((unit === UnitOfPower.WATT || unit === UnitOfEnergy.WATT_HOUR ) && Math.abs(numberValue) < 1000) {
if (withUnit) {
return `${Math.round(numberValue)} ${unit}`;
}
return `${Math.round(numberValue)}`;
}

if (unit === UnitOfPower.KILO_WATT && Math.abs(numberValue) < 1) {
if (unit === UnitOfPower.KILO_WATT && Math.abs(numberValue) < 1) {
if (withUnit) {
return `${Math.round(numberValue * 1000)} W`;
}
return `${Math.round(numberValue * 1000)}`;
}
if ( unit === UnitOfEnergy.KILO_WATT_HOUR && Math.abs(numberValue) < 1) {
if (withUnit) {
return `${Math.round(numberValue * 1000)} Wh`;
}
return `${Math.round(numberValue * 1000)}`;
}

if (unit === UnitOfPower.MEGA_WATT && Math.abs(numberValue) < 1) {
if (withUnit) {
return `${(numberValue * 1000).toFixed(decimal)} kW`;
}
return `${(numberValue * 1000).toFixed(decimal)}`;
}
if ( unit === UnitOfEnergy.MEGA_WATT_HOUR && Math.abs(numberValue) < 1) {
if (withUnit) {
return `${(numberValue * 1000).toFixed(decimal)} kWh`;
}
return `${(numberValue * 1000).toFixed(decimal)}`;
}

for (const rule of rules) {
if (Math.abs(numberValue) >= rule.threshold) {
Expand Down
1 change: 1 addition & 0 deletions src/localize/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@

"energy_cost_decimals": "Energy Cost Decimals",
"show_remaining_energy": "Show Remaining Energy",
"remaining_energy_to_shutdown": "Show Remaining Energy to Shutdown",
"path_threshold": "Path Threshold",
"label_daily_grid_buy": "Daily Grid Buy Label",
"label_daily_grid_sell": "Daily Grid Sell Label",
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ export interface PowerFlowCardConfig extends LovelaceCardConfig {
schema_version: number;
dev_mode: boolean;
refresh_time?: string;
panel_mode?: boolean;
wide_view_mode: boolean,
align_grid: boolean;
align_load: boolean;
center_sol_inv_bat: boolean;
large_font?: boolean;
show_solar: boolean;
show_battery: boolean;
Expand Down Expand Up @@ -117,6 +117,7 @@ export interface PowerFlowCardConfig extends LovelaceCardConfig {
show_absolute: boolean;
auto_scale: boolean;
show_remaining_energy: boolean;
remaining_energy_to_shutdown: boolean;
dynamic_colour: boolean;
linear_gradient: boolean;
animate: boolean;
Expand Down

0 comments on commit 38cf7ed

Please sign in to comment.