Extend Sensor with custom attribute

Hello, i want to extend an given sensor (sensor.fritzbox) with an new attribute. the values of the new attribute should be filled based on template functions.

I have tried to create a new sensor template with the same name, that doesnt work.

any chance to realize this, witout creating a complete new template sensor?

It’s possible to add custom attributes to an existing entity. You can do it via Configuration > Customizations or by adding the new attribute to the customize.yaml file (explained here).

However, the custom attribute’s value cannot be defined by a template. The value must be a constant like a string or integer.

It is possible to change the value of an entity’s state or attribute. It can be done with a python_script like this one. However, that change is not permanent and is lost when Home Assistant is restarted.

As you can see, although it is easy to create a new attribute, assigning it a dynamic value isn’t straightforward. You might be better off creating a Template Sensor.

2 Likes

digging this one back up from the dead … after a bit of Googling, this seems to be the closest situation for what I’m looking for.

I think what I’m looking for is similar to what the thread starter was looking to do, which is to tack on extra attributes. After reviewing the customize documents, it seems it’s capable of overriding attributes, but not creating new ones. The distinction is that for example, a “climate” entity does not have by default any parameters relating to lights, so it’s not possible to add an attribute for it. But on the other hand, it has humidity parameters, so you can override those. Am I correct?

What I’m looking for is something that would allow me to create attributes that do not already exist.

Also @123 , I just wanted to understand what’s meant by “not by a template”. It means, normally whatever you put into a value_template won’t work, but can an attribute be defined by another sensor’s value? Or it must be a fixed number/string, as in, it must be hard-coded?

1 Like

A custom attribute, and the value you assign it, are defined in customize.yaml. The value you assign it cannot be template (so without the ability to use templates, the custom attribute’s value cannot be based on some other entity’s value or computed in any manner).

gotcha, thanks for quick reply!

Because staring at the problem for too long often results in tunnel vision, maybe there’s a different and more efficient way to do this …

My original plan was to use probes that measures and sends values over MQTT to HA as an attribute of a sensor. The idea was meant for an aquarium where temperatures (amongst other parameters) can be associated to a generic thermostat. The temperature would not be an issue since the target_temp & target_sensor can be set easily. However, additional water parameters (all measured and sent over MQTT to HA) were what I wanted to add to the generic thermostat as attributes.

While contemplating the python script idea, i figured it would not be an issue if the change is not permanent when HA gets restarted. With a probe, that attribute would essentially be written to almost constantly as the probe measures changes. On the other hand, this means an automation is constantly triggered every time there is a change seen in the probe in order to update the attribute – automations constantly triggered multiplied by however many attributes I decide to add to the entity. This clearly isn’t efficient …

I suppose I’d get asked the question, why are you trying to throw all that info that is already individual sensors into various attributes. All I can say is … proof of concept? The climate / generic thermostat seems almost so perfect for an aquarium where one focus is trying to maintain water temperature, not much different than maintaining air temperature in a home.

In line with this post I’m trying to locate in the map card the locations where I parked both my cars.
I have the latitude and longitude stored in an input text helper which I later use to have a url to gmaps with the exact location.
The map card only works with any entity that has latitude and longitude stored as attributes but not as state which is my case. I can fake the attributes in the developer tools and it works but I can’t figure out how to do this.
I tried a template sensor with latitude and longitude attributes but I don’t know how to update them when car is parked.
Maybe link the attributes in the template to another extra input helper state?
Any idea would be appreciated
:slight_smile:

EDIT:
I just did exactly that:

Maybe link the attributes in the template to another extra input helper state?

I had my coordenates in the state of a input text helper with “latitude,longitude” so I used the template sensor atribute to render the state split by comma.

- binary_sensor:
      name: Focus_CFG_Avisos_ubicacion_coord_aparcamiento_mapa
      state: true
      icon: mdi:car-hatchback
      attributes:
        latitude: >
            {{states('input_text.cfg_avisos_ubicacion_coord_aparcamiento_focus').split(",")[0]}}
        longitude: >
           {{states('input_text.cfg_avisos_ubicacion_coord_aparcamiento_focus').split(",")[1]}}

Like this I have an entity that I can change its attributes and display it in a map card.

Strange workaround but it helped in my case.