alandtse / tesla

Tesla custom integration for Home Assistant. This requires a refresh token be generated by third-party apps to login.
Apache License 2.0
601 stars 99 forks source link

Battery Remaining Unavailable #998

Open DigitalCharon opened 5 months ago

DigitalCharon commented 5 months ago

Is there an existing issue for this?

Version of the Tesla component

v3.23.0

Version of the Powerwall Gateway software

24.12.6 3cc46fb9

Model

3

Current Behavior

battery remaining is unavailable.

Expected Behavior

battery percentage shows up

Debug logs

Logger: homeassistant.components.sensor
Source: helpers/entity_platform.py:914
integration: Sensor (documentation, issues)
First occurred: June 12, 2024 at 11:06:25 PM (5 occurrences)
Last logged: 10:46:49 AM

Error adding entity sensor.powerwall_battery_remaining for domain sensor with platform tesla_custom
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 914, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1355, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1000, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1126, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1061, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1006, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 543, in state
    value = self.native_value
            ^^^^^^^^^^^^^^^^^
  File "/config/custom_components/tesla_custom/sensor.py", line 445, in native_value
    return round(self._energysite.energy_left)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/teslajsonpy/energy.py", line 152, in energy_left
    return round (self._site_config.get("nameplate_energy") * self.percentage_charged / 100 )
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

Anything else?

No response

Dr-ead commented 5 months ago

Having a similar issue here: Since yesterday I did not get any data from my Model S and I cannot send anything to it. via HA. Possible that the connection to the Tesla servers seem to be interrupted? Anyone else having this issue? For me it looks similar to the API thing at the start of this year.

portaldon commented 5 months ago

I have had the same issue just now (in addition to all powerwall sensors not updating) and performing the same fix here resolved it - perhaps this is a regression: https://github.com/alandtse/tesla/issues/919#issuecomment-2015548967

I am on v3.24.0 PWG v24.12.6 3cc46fb9 2x Powerwall 3

vvuk commented 4 months ago

Similar but subtly different issue. Lots of Task exception was never retrieved (might be nice if the integration catches internal exceptions and sets itself to an error state or similar?), but the traceback is:

...
home-assistant  |   File "/config/custom_components/tesla_custom/sensor.py", line 445, in native_value
home-assistant  |     return round(self._energysite.energy_left)
home-assistant  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
home-assistant  |   File "/usr/local/lib/python3.12/site-packages/teslajsonpy/energy.py", line 152, in energy_left
home-assistant  |     return round (self._site_config.get("nameplate_energy") * self.percentage_charged / 100 )
home-assistant  |                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
home-assistant  | TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'

which I believe means that nameplate_energy is an int, and percentage_charged is None -- whereas in the initial post about this issue, it looked like it was None and float. Could be the exact same issue just different python version/impl reporting differently, but it's odd.

DigitalCharon commented 4 months ago

I ended up getting my integration working again by replacing return round (self._site_config.get("nameplate_energy") * self.percentage_charged / 100 ) with return round (self._site_config.get("battery_count")*self._site_summary.get("percentage_charged")*13500/100)

in the energy.py of my local copy of teslajsonpy.