maxwroc/battery-state-card

Exception: Cannot read properties of undefined (reading 'toLocaleUpperCase')

Closed this issue · 8 comments

Describe the bug
I am receiving an error on one specific entity. It is a battery sensor coming from the ambientweather2mqtt custom add-on

How to reproduce
Here is the sensor in question. It looks no different than any other battery sensor:

image

Expected behavior
It should pick up the battery like it was any other

YAML configuration

title: Sensors
tap_action: more-info
view_layout:
  column: 2
charging_state:
  attribute:
    - name: Battery State
      value: Charging
    - name: '*is_charging'
      value: true
    - name: '*battery_charging'
      value: true
  state:
    - charging
    - Charging
    - 'On'
    - 'on'
state_map:
  - from: 'off'
    to: 100
  - from: 'on'
    to: 10
filter:
  include:
    - name: entity_id
      value: '*_battery_level'
    - name: attributes.device_class
      value: battery
  exclude:
    - name: entity_id
      value: group*
    - name: entity_id
      value: binary_sensor.*low_battery_level*
    - name: entity_id
      value: sensor.*pixel_*
    - name: entity_id
      value: sensor.*lock*
    - name: entity_id
      value: binary_sensor.*_co2_battery
    - name: entity_id
      value: binary_sensor.*replace*(now|soon)*
    - name: entity_id
      value: sensor.bathroom_pothos_battery
    - name: entity_id
      value: sensor.african_violet_battery
    - name: entity_id
      value: sensor.plumosa_fern_battery
    - name: entity_id
      value: sensor.rabbits_foot_fern_battery
    - name: entity_id
      value: sensor.orchid_blue_pot_battery
    - name: entity_id
      value: sensor.orchid_striped_pot_battery
    - name: entity_id
      value: sensor.malfunctioning_eddie_battery
    - name: entity_id
      value: sensor.hedonismbot_battery
bulk_rename:
  - from: Level
    to: ''
  - from: Battery
    to: ''
  - from: battery
    to: ''
  - from: Upstairs Low
    to: Upstairs Temperature

Dev console errors

TypeError: Cannot read properties of undefined (reading 'toLocaleUpperCase')
    at http://homeassistant.local:8123/hacsfiles/battery-state-card/battery-state-card.js?hacstag=256292682316:35:9596
    at Vt.<anonymous> (http://homeassistant.local:8123/hacsfiles/battery-state-card/battery-state-card.js?hacstag=256292682316:35:9635)
    at Generator.next (<anonymous>)
    at http://homeassistant.local:8123/hacsfiles/battery-state-card/battery-state-card.js?hacstag=256292682316:1:633
    at new Promise (<anonymous>)
    at e (http://homeassistant.local:8123/hacsfiles/battery-state-card/battery-state-card.js?hacstag=256292682316:1:378)
    at Vt.internalUpdate (http://homeassistant.local:8123/hacsfiles/battery-state-card/battery-state-card.js?hacstag=256292682316:35:9031)
    at Vt.<anonymous> (http://homeassistant.local:8123/hacsfiles/battery-state-card/battery-state-card.js?hacstag=256292682316:35:1486)
    at Generator.next (<anonymous>)
    at http://homeassistant.local:8123/hacsfiles/battery-state-card/battery-state-card.js?hacstag=256292682316:1:633

Screenshots
image

Version
3.1.6

I have this error as well, three in fact. How did you determine it was your Ecowitt device causing the problem? I have an Ecowitt also. I don't see anything obvious in the error message tying the failure to a certain battery/device. Thanks.

I don't have an Ecowitt. Mine is from an Ambient weather station. The only real reason I knew it was that was that it happened right after some changes were made to it. I confirmed by adding it to the exclude list and the error went away.

Sorry, I read too fast and blurred Ambient and Ecowitt together since I have several Ambient weather sensors all reporting in via an Ecowitt GW1100. As for which entity is giving me trouble, I answered my question. If I click on the pink error box it shows me the device and then the Gear icon will reveal the entity name. In my case, it is failing on three Oregon thermostats (not my Ambient or Ecowitt stuff) that have all stopped sending data for past two weeks, therefore HA thinks they are Unknown.

I just double checked and mine is definitely not unknown. It's been getting regular updates

Weird. The rtldiscovery service on the Rpi hosting my RTL-SDR had stopped. Once I restarted it and MQTT discovery ran, I began getting temperature samples again. However, I noticed both the "Battery" and "Battery+" entity friendly names for these three devices did NOT start with the device name. Once I manually added the device to the beginning of the name, things got better with the Battery State Card. E.g. "Battery+" -> "Temperature Garage Battery+"

I have no idea why these entities were misnamed. Perhaps something to do with the recent changes where HA likes device entity names to all begin with the device name.

I am not sure whether there is a bug in Battery State Card or not. But I'd suggest you see if you have any misnamed "Battery" or "Battery+" entities and if renaming them fixes the problem for you.

I confirm, exactly the same problem.

Ok I know what is the problem.

It throws exception because in your rename rules you have one to replace "Battery" with an empty string. There is a line in the card code which tries to capitalize first letter of the name. The first letter doesn't exists as the string is an empty string - because "Battery" was the actual/full entity name (no other chars left).

While this is a bug in the card which should be fixed you should consider as well changing the rules or entity name as probably it's not your intention to display entity without the name.

Fixed in v3.2.0