yp87 / leaf2mqtt

Pull in data from the Nissan Connect platform for the LEAF and publish over MQTT
GNU General Public License v3.0
41 stars 39 forks source link

Leaf Battery Charging Status not working in HA #66

Closed ExMacro closed 1 year ago

ExMacro commented 1 year ago

Describe the bug This code in the configuration.yaml is not working anymore. Leaf Battery Charging Status is not working as a HA sensor. Only displays Unknown status in HA. Leaf2MQTT gets the values from the Nissan servers but the sensor configuration in configuration.yaml is not working anymore.

mqtt:
  sensor:
    - name: leaf_battery_charging
      # Since VIN is not specified, it will represent the state from the first vehicle in the account.
      state_topic: "leaf/battery/charging"
      device_class: battery

HA logs an error:

Logger: homeassistant.components.mqtt.models
Source: components/sensor/__init__.py:593 
Integration: MQTT (documentation, issues) 
First occurred: 18:32:12 (1 occurrences) 
Last logged: 18:32:12

Exception raised when updating state of sensor.leaf_battery_charging, topic: 'leaf/battery/charging' with payload: b'false'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 591, in state
    numerical_value = float(value)  # type:ignore[arg-type]
                      ^^^^^^^^^^^^
ValueError: could not convert string to float: 'false'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/models.py", line 305, in process_write_state_requests
    entity.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 730, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 830, in _async_write_ha_state
    state, attr = self._async_generate_attributes()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 771, in _async_generate_attributes
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 736, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 593, in state
    raise ValueError(
ValueError: Sensor sensor.leaf_battery_charging has device class 'battery', state class 'None' unit 'None' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'false' (<class 'str'>)

LEAF_TYPE Newer than 2019 (the model is MY2020)

Expected behavior The sensor in configuration.yaml was working correctly before.

ExMacro commented 1 year ago

I hope that I added this sensor to configuration.yaml correctly.

mqtt:
  sensor:
    - name: leaf_battery_charging
      # Since VIN is not specified, it will represent the state from the first vehicle in the account.
      state_topic: "leaf/battery/charging"
ExMacro commented 1 year ago

I'm running Home Assistant 2023.9.2 and Leaf2MQTT version: 46

ExMacro commented 1 year ago

I hope that I added this sensor to configuration.yaml correctly.

mqtt:
  sensor:
    - name: leaf_battery_charging
      # Since VIN is not specified, it will represent the state from the first vehicle in the account.
      state_topic: "leaf/battery/charging"

I realised that I need this to be binary_sensor but it still only show "Unknown" in HA.

krikkert commented 1 year ago

As far as I can tell, you should not set the device_class to "battery". When the device is a battery, it expects the value of the sensor to be an integer or a floating point number, and to represent the percentage of battery charge remaining.

Here is my setup (excerpt):

mqtt: binary_sensor:

  • name: leaf_battery_charging state_topic: "leaf//battery/charging" payload_off: "false" payload_on: "true" device: identifiers: "" device_class: battery_charging unique_id: "_leaf_batterycharging"
ExMacro commented 1 year ago

Yes! The solution is to put the payload_on / off into the config. This works for me now!

Thanks!

mqtt:
  binary_sensor:
    - name: leaf_battery_charging
      # Since VIN is not specified, it will represent the state from the first vehicle in the account.
      state_topic: "leaf/battery/charging"
      payload_off: "false"
      payload_on: "true"
      device_class: battery_charging
ExMacro commented 1 year ago

The solution is to add the payload into the config.

mqtt:
  binary_sensor:
    - name: leaf_battery_charging
      # Since VIN is not specified, it will represent the state from the first vehicle in the account.
      state_topic: "leaf/battery/charging"
      payload_off: "false"
      payload_on: "true"
      device_class: battery_charging