home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.11k stars 29.79k forks source link

ValueError: Sensor has device class 'None', state class 'None' unit '' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: '-----W--' (<class 'str'>) #94464

Closed mvaneijken closed 1 year ago

mvaneijken commented 1 year ago

The problem

The sensor component detect strings as integers resulting in an error. See diagnostics information below.

I would expect that if the test for integer data type would fallback to default string instead of integer.

What version of Home Assistant Core has the issue?

core-2023.6.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Supervised

Integration causing the issue

Sensor

Link to integration documentation on our website

https://www.home-assistant.io/integrations/sensor/

Diagnostics information

Example 1:

2023-06-12 12:38:17.682 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.air_quality_level_woonkamer fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 581, in state
    numerical_value = int(value)
                      ^^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'Fair'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 559, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 784, in async_device_update
    await self.async_update()
  File "/config/custom_components/iaquk/sensor.py", line 102, in async_update
    if self.state == LEVEL_EXCELLENT
       ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 585, in state
    raise ValueError(
ValueError: Sensor sensor.air_quality_level_woonkamer has device class 'None', state class 'measurement' unit 'None' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'Fair' ()

Example 2:

2023-06-11 00:37:02.716 ERROR (MainThread) [homeassistant.components.mqtt.models] Exception raised when updating state of sensor.otgw_status_master, topic: 'OTGW/value/otgw-C45BBEDE38A7/status_master' with payload: b'-----W--'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 581, in state
    numerical_value = int(value)
                      ^^^^^^^^^^
ValueError: invalid literal for int() with base 10: '-----W--'

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 270, in process_write_state_requests
    entity.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 590, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 654, in _async_write_ha_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 596, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 585, in state
    raise ValueError(
ValueError: Sensor sensor.otgw_status_master has device class 'None', state class 'None' unit '' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: '-----W--' (<class 'str'>)

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 1 year ago

Hey there @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (sensor) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `sensor` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign sensor` Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


sensor documentation sensor source (message by IssueLinks)

MartinHjelmare commented 1 year ago

We raise the exception as the sensor attributes are incorrectly set, either by the developer that created the integration of the sensor or by the user, if there's some entity customization or the user configuring the MQTT configuration.

In the first of your examples, the state class is set to measurement, which requires a numeric value. In the second example the unit of measurement is an empty string, while it should be None.