Allow parsing sensors as unavailable
Is your feature request related to a problem? Please describe.
I'm trying to scrape some data where sometimes certain elements are unavailable. In my case these are temperature values, where a very large value (9999.9 °C) is used, if the sensor is not connected. But more generally it could also be that sometime there is a real value (e.g. 42) in the response and other times it's something like null.
I'd like to mark the sensors as unavailable in Home Assistant instead of having to deal with the fake values. Crucially, the structure of the HTML is always the same, only the text inside elements changes, so I cannot use the CSS selector in select.
Describe the solution you'd like
The easiest solution might be to use parse_result=True here, then None should be parsed as None and SensorEntity should be able to deal with that.
https://github.com/danieldotnl/ha-multiscrape/blob/2287fb5d97d353fe17463d9dd022a75cdc03fb17/custom_components/multiscrape/scraper.py#L126-L128
But I assume there is a reason, why you used parse_result=False (which isn't the default). For me it would be good enough, if the at least None or an empty string would be interpreted as unavailable. However, changing this now would break existing configs, which expect string values in those cases.
Describe alternatives you've considered
Another solution would be to implement availability from the standard scrape integration. That's probably more work, but covers a more general use case.
Additional context
I tried to use
value_template: >
{% set temp = value.removesuffix("°C") | trim | float %}
{{ temp if temp < 9999 else }}
which results in
2023-10-30 13:39:43.399 ERROR (MainThread) [homeassistant.components.sensor] Error adding entities for domain sensor with platform multiscrape
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 593, in state
numerical_value = float(value) # type:ignore[arg-type]
^^^^^^^^^^^^
ValueError: could not convert string to float: 'None'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 507, in async_add_entities
await asyncio.gather(*tasks)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 752, in _async_add_entity
await entity.add_to_platform_finish()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1057, in add_to_platform_finish
self.async_write_ha_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 779, in async_write_ha_state
self._async_write_ha_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 879, in _async_write_ha_state
state, attr = self._async_generate_attributes()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 820, in _async_generate_attributes
state = self._stringify_state(available)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 785, in _stringify_state
if (state := self.state) is None:
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 595, in state
raise ValueError(
ValueError: Sensor sensor.cmi_temp_solar_kollektor has device class 'temperature', state class 'None' unit '°C' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'None' (<class 'str'>)
Same problem here, for now I also set parse_result = True 👍🏻