Scrape sensor with string value

I previously had a few scrape sensors configured in my configuration.yaml that grabbed strings for my guest WiFi network. However, those sensor configs seem to have been deprecated and no longer work. I tried the new Scrape integration method, but it doesn’t seem to want to work since the values are strings and I have no idea what I’m doing wrong.

ValueError: Sensor sensor.guest_network has device class None, state class None unit n and suggested precision None thus indicating it has a numeric value; however, it has the non-numeric value: UniFi-Guest (<class 'str'>)
2023-05-17 22:47:04.573 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 581, in state
    numerical_value = float(value)  # type:ignore[arg-type]
ValueError: could not convert string to float: 'UniFi-Guest'

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 961, in _async_registry_updated
    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.guest_network has device class None, state class None unit n and suggested precision None thus indicating it has a numeric value; however, it has the non-numeric value: UniFi-Guest (<class 'str'>)
2023-05-17 22:47:04.574 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up scrape platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 581, in state
    numerical_value = float(value)  # type:ignore[arg-type]
ValueError: could not convert string to float: 'UniFi-Guest'

That screenshot doesn’t match the error message. Screenshot is what you want:

image

but the error message suggests you left the default “n” in place:

Make sure you have no device class, state class or unit of measurement for string sensors.

The default value in that form is n. I must’ve copied it from an earlier debugging log. However, the error message is pretty much identical even if I change it to None.

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 455, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 731, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 845, in add_to_platform_finish
    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.guest_network 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: UniFi-Guest (<class 'str'>)
2023-05-17 22:57:52.993 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 581, in state
    numerical_value = float(value)  # type:ignore[arg-type]
ValueError: could not convert string to float: 'UniFi-Guest'

Now you have state_class set to measurement. Set all three to none.

This is very bizzare. I did not change that field. However, now that I added scrape sensors for the username and password, all 3 of them started working.