diff --git a/package.json b/package.json index af69f92d..ac7a5d06 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "battery-state-card", - "version": "1.3.5", + "version": "1.3.6", "description": "Battery State card for Home Assistant", "main": "dist/battery-state-card.js", "repository": { diff --git a/src/battery-vm.ts b/src/battery-vm.ts index 21e19896..1c34ce37 100644 --- a/src/battery-vm.ts +++ b/src/battery-vm.ts @@ -200,9 +200,15 @@ class BatteryViewModel { * @param entityData Entity state data */ private getLevel(entityData: HassEntity): string { + const UnknownLevel = "Unknown"; let level: string; + if (this.config.attribute) { - level = entityData.attributes[this.config.attribute] + level = entityData.attributes[this.config.attribute]; + if (level == undefined) { + log(`Attribute "${this.config.attribute}" doesn't exist on "${this.config.entity}" entity`); + level = UnknownLevel; + } } else { const candidates: string[] = [ @@ -211,7 +217,7 @@ class BatteryViewModel { entityData.state ]; - level = candidates.find(n => n !== null && n !== undefined)?.toString() || "Unknown"; + level = candidates.find(n => n !== null && n !== undefined)?.toString() || UnknownLevel; } // check if we should convert value eg. for binary sensors diff --git a/src/utils.ts b/src/utils.ts index 8629b7bc..55ab191a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ import { HomeAssistant } from "./ha-types"; console.info( - "%c BATTERY-STATE-CARD %c 1.3.5", + "%c BATTERY-STATE-CARD %c 1.3.6", "color: white; background: forestgreen; font-weight: 700;", "color: forestgreen; background: white; font-weight: 700;", );