New Helper: Entity from attribute

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.

This is often done with templates

template:
  - sensor:
    - name: sun_elevation
      state: "{{ state_attr('sun.sun', 'elevation') }}"
      unique_id: c0b35ff264f5d00dfbcf135fd560e375
      unit_of_measurement: '°'
#      state_class: measurement
    - name: boiler_target_temp
      state: "{{ state_attr('climate.hive_boiler_thermostat', 'temperature') }}"
      unique_id: f3ba759359840932bac93ba1928c63c4
      unit_of_measurement: '°C'

It would be great if these could be set in the helper UI.

+1 and also remembering that the attribute can be ‘deep’, such as x.y.z.

Commonly - weather forecast related:

weather.home.attributes.forecast[0].templow

For example to check if the forecast low temperature might warrant a frost warning, or:

weather.home.attributes.forecast[0].precipitation
weather.home.attributes.forecast[1].precipitation

To generate 2 sensors that can be added together to issue a warning if the 48 hour precipitation forecast might cause local flooding.

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.

2 Likes

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.

1 Like
5 Likes

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.

4 Likes

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

well… I noticed this in my logs…

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'>)