It is quite common to want to turn an attribute of an entity into it’s own entity: E.g Sun elevation for automations. Temperature from the weather entity. Etc etc. This makes automations easier and also gives other advantages such as state history.
I think this is a great idea and will build it.
Probably plenty of templates which are no longer needed then for a lot of people as still there is a lot of data in attributes still.
Awesome, link to the PR when you make it. And would be happy to contribute.
We might even be able to use regex to detect YAML entries in the style of "{{ state_attr('x', 'y') }}" and {{states.x.attributes.y}} and suggest migration using the new repairs UI.
As this was not approved to have within core I have released it finally as a custom component if someone finds it here and is interested.
At time of writing not yet available in HACS default.
giving this a shot… seems like it would be useful rather than having to whip out yaml files for this…
accuweather doesn’t have a humidity entity, so hoping this will solve that problem from the weather.accuweather device
UPDATE: This is awesome!.. thanks for making this @gjohansson
2024-06-24 11:30:30.540 ERROR (MainThread) [homeassistant.components.sensor] Error adding entity sensor.accuweather_humidity for domain sensor with platform attribute_as_sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 658, in state
numerical_value = int(value)
^^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'unknown'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 661, in state
numerical_value = float(value)
^^^^^^^^^^^^
ValueError: could not convert string to float: 'unknown'
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 600, in _async_add_entities
await coro
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 914, in _async_add_entity
await entity.add_to_platform_finish()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1355, in add_to_platform_finish
self.async_write_ha_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1000, in async_write_ha_state
self._async_write_ha_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1126, in _async_write_ha_state
state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1061, in __async_calculate_state
state = self._stringify_state(available)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1006, in _stringify_state
if (state := self.state) is None:
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 665, in state
raise ValueError(
ValueError: Sensor sensor.accuweather_humidity has device class 'humidity', state class 'measurement' unit '%' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'unknown' (<class 'str'>)