Fundamentally lost: Sensors, attributes and how to access them

Hi Forum,

I’m starting to feel like I fundamentally don’t get some basic concepts in Home Assistant. Usually, when I poke new things long enough it clicks and I get a conceptual idea of how the thing is built and what to expect where and which questions to ask. Here I am just feeling very, very lost at the moment. This happened during the last 20 minutes:

Idea: Somehow, the Tibber integration provides a current price level. I’d like to display that as a graph.

(here was a screenshot but apparently “new users can only put one embedded media in the post”)

So, hm, that’s the price but not the attribute “price level”, I know that is contained in the JSON from the Tibber API, surely the author of the integration will have seen that…

(here was a screenshot but apparently “new users can only put one embedded media in the post”)

Oh, right, there it is! It’s an attribute of this sensor. Okay, I can understand that idea. Sensors can have attributes, so there must be an easy way to access them for graphs, let’s see. *click* Okay, no, that wasn’t it. Drop down after selecting the sensor for a graph? No, that just lets me select other sensors and entities with states. Why is price the default and why aren’t the other attributes? What makes one the default, how is that selected? Okay, let’s see the sensors documentation…

(here was a screenshot but apparently “new users can only put one embedded media in the post”)

… it doesn’t even mention attributes there. I have absolutely no idea how to access a sensor attribute for a graph. I don’t even know where to start if not on the page that documents sensors.

I’m just feeling kind of lost. How do you guys get a grasp of all of the concepts? Is there any fundamental abstract that describes all of the elements of Home Assistant and how they are built and interact? I hate being in this state of stringing myself along with “cookbook configuration” while not feeling like I have a somewhat competent idea of the entire thing.

So yeah, I need some help with that actual problem but also some answers for the overall problem would be appreciated.

Why is price the default and why aren’t the other attributes? What makes one the default, how is that selected?

Which attribute is used as the value of an entity is a design decision of the respective maintainer/developer of the integration. Actually, it is now common practice in Home Assistant for integrations not to pack additional information into the attributes, but to offer separate sensors with the respective values.

If this is not the case with the integration you are using, you will have to extract the values you are interested in either via an additional template sensor or an additional integration for attribute values.

Thank you. I sense that this is what I want to do:

Data type “map” sounds like a dictionary/hash, so how would I get the actual index of the desired attribute from this rendered list of “fancy” names?

Bildschirmfoto vom 2023-10-23 01-27-28

A good way to understand attributes is to use Developer Tools. In there you can see the correct names of the attributes and their actual values for sensors etc in your environment, using the STATES tab.

You can then use the TEMPLATE tab to test your template to expose those attributes:

1 Like

In Developer Tools | Template, paste this in:

{{ states.zone.home.state }}
{%- for attr in states.zone.home.attributes %}
  {{ attr }}:{{ state_attr('zone.home', attr) }}
{%- endfor %}

Very basic and everyone should have a home zone. If not, go add it!

It will give you an idea of what to see with attributes.

You will see the following attributes:

latitude:
longitude:
radius:
passive:
persons:[]
editable:
icon:mdi:
friendly_name:

The first line is the state of the home zone. That is the number of people (trackable devices) that are in that zone.
The rest is a loop through the attributes of the home zone.
Persons is a list of people (trackable devices) that are home. I think the rest are self-explanatory.

1 Like

You should get the value you want with a template like this:

{{ state_attr('sensor.NAME_OF_SENSOR_WITH_ATTRIBUTES', 'ATTRIBUTE_NAME') }}
1 Like

Thank you guys, I think all of that set me on the right track. Also, I found this: Core Architecture | Home Assistant Developer Docs

I got the string in ‘price_level’ printed in a simple jinja template and now I’ll find out where to go from there to make it a usable entity.