core: Exception since 2023.5.0: state class None: numeric value, however, it has the non-numeric value.

The problem

The issue is introduced in 2023.5, without any config changes from my site related to my iPhone

Exception is visible both with charging and not-charging. (makes sense off course)

What version of Home Assistant Core has the issue?

2023.5.0

What was the last working version of Home Assistant Core?

2023.4.6

What type of installation are you running?

Home Assistant OS

Integration causing the issue

NA

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

2023-05-04 11:30:55.105 ERROR (MainThread) [homeassistant.util.logging] Exception in _handle_update when dispatching 'mobile_app_sensor_update': ('aa296a1333b3ea46c2cfa721b87a9816df8e9935cdba08f5c8f23c57d57a0dda_battery_state', {'attributes': {'Low Power Mode': False}, 'icon': 'mdi:battery-charging-20', 'state': 'Charging', 'type': 'sensor', 'unique_id': 'battery_state', 'webhook_id': 'aa296a1333b3ea46c2cfa721b87a9816df8e9935cdba08f5c8f23c57d57a0dda'})
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 579, in state
    numerical_value = int(value)
ValueError: invalid literal for int() with base 10: 'Charging'

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

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mobile_app/entity.py", line 105, in _handle_update
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 585, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 649, in _async_write_ha_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 591, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 583, in state
    raise ValueError(
ValueError: Sensor sensor.iphone_van_marck_battery_state 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: Charging (<class 'str'>)

Additional information

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 15
  • Comments: 39 (14 by maintainers)

Most upvoted comments

I don’t know why suddenly home assistant is now strictly enforcing typing of states. States in the past were always strings unless explicitly used in a manner that forces their conversion, ie. platform: numeric_state or manually converted via a template. I think this was the best way of handling it. Now it’s assumed that if there is a unit of measurement the state must be convertible to a float or be None. Why? Of what benefit is this of the user?

Everyone who writes uses a template sensor, MQTT sensor must write in their own data sanitization to keep home assistant from throwing ugly exceptions. I thought the goal of this project was to make things more user friendly over time, but this move is completely backwards when we have to make the states compatible with raw python string to numeric conversion at all times.

Worse yet this isn’t even compatible with how home assistant normally handles the state of sensors. If a sensor is malfunctioning or hasn’t been completely initialized yet it is supposed to report unknown or unavailable. But even this standard behavior isn’t being trapped. I have tons of template sensors that were coded to report unknown or unavailable to handle bad data. Now would have to change them to ‘None’. Changing these well known error states to appease python’s string to numeric conversion will look terrible on the frontend. Why is this burden being shifted onto the user instead of adding checks for these states?

For templates the solution seems to be {{None}}. Home assisant will generously convert all sorts of strings to python booleans, but for a None type only {{None}} via Jinja will do. This will be converted back into a string as the state ‘unknown’.

For your trigger templates, I think I have an idea of what could be wrong as I encountered this issue. If a trigger based template throws the exception due to a failed string to numeric conversion the trigger gets disabled until hass is rebooted. You can have a whole block of template sensors under one trigger and if any one of those throws the exception the whole block no longer triggers. Later in the week I’ll see if I can come up with a reproducer configuration and open an issue for that.

I have the same issue with modus sensors… no change on my end for years.

You should open a separate issue for modbus. It should be adjusted to strip nan values.

ValueError: Sensor sensor.inverter_dc_current has device class None, state class None unit A and suggested precision None thus indicating it has a numeric value; however, it has the non-numeric value: nan (<class 'str'>)

If it has a unit of measurement => the value MUST be a number or None

Where is this documented? If the unit of measurement is TIMESTAMP my understanding is it is (and was) expected to be a string.