Accessing entity attribute in the dashboard and automations

I can’t figure out something seemingly very basic - how to use a distance attribute on an ESPresense sensor. But it’s a more generic question - when I see something in an entity card, how do I 1) show that in the dashboard as a value or chart; 2) use it in automations.

Documentation is pretty cryptic and examples don’t seem to pass validation. Numerous posts do not seem to answer the question in a reusable way.

Here is my example -

sensor.black_beacon points to the location name (like basement or not_home). But how do I access the distance? I tried sensor.black_beacon.distance, sensor.black_beacon.attributres.distance, and few other variations - no luck.

Alas, but nothing ever seems to be too basic in Home Assistant to require a more detailed explanation.

Without further information, I’m guessing that you are looking for help on templating, which is extremely useful but also rather tricky. Here’s the documentation page for it: Templating - Home Assistant. It’s a bit overwhelming, but this is a good page to have as a reference. Also, check all of your templates in the Template tab of Developer Tools, if you aren’t already, before trying to use them elsewhere. If they don’t return the answer you are looking for, you’ll have to try again (and again and again until you get the hang of it :woman_facepalming:).

As for the attribute you’re trying to pull out, you would want to template something like this {{ state_attr("sensor.black_beacon","distance") }}. The {{ }} are important to let Home Assistant know that it’s a template. Then in order to look at an attribute, you use state_attr(entity, attribute). You can also use the form {{ states.sensor.black_beacon.attributes.distance }}, but it is not as recommended.

In actuality, there are many answers to using the attribute as part of an automation or a card on the dashboard. In automations, you can use the State or Numeric State triggers and fill in the attribute to be used instead of the general state. You could also use a Template trigger with the above templating for something a little more complicated. For cards on the dashboard, that depends on the card. Some can use attributes, some can use templates. Those will require more specifications if you want recommendations.

2 Likes

Thank you. I scanned through the template documentation and it explains the syntax - but where do I put it to make it work?

I tried the card yaml, but I get this:

So this is a situation where the card probably won’t accept a template. That card requires an entity at that value, and the entity you are using/it is looking for is sensor.black_beacon. I looked through the documentation and it doesn’t appear to have many options for entities, so you probably can’t use that card as is for this purpose, but I have two ideas.

First idea, and the easiest: make a Template Sensor helper to track the attribute, then use that sensor as the entity in the card. I’d recommend using the UI to do it as described in the documentation here: Template - Home Assistant

Second idea, look for a custom card element available in HACS that would show whatever kind of card you are wanting and is capable of using attributes as well as/instead of entity states. Some custom cards only work with yaml, so this isn’t necessarily preferable for ease of use, but it may also be the only way to get exactly what you actually want when Home Assistant isn’t already cooperative.

Creating template via UI worked great, and now I have a card showing the distance chart! Thank you!

Now I need to do this for 20+ remaining beacons. My approach for repetitive configurations so far has been finding the YAML representation of what I did in UI, copy-paste, find-replace. I did that for automations, scripts, and even when I was adding actual beacons.

But I can’t find the YAML file defining the template-sensor I created via UI as per your First Idea. Will I have to add all of them manually? Or maybe there is a way to use wildcards for this so that I do that once and it creates distance sensors for each beacon?

Lol, yeah, copy & paste is definitely the better way to go for that many (I don’t know enough about wildcards to comment or suggest on them). At least automations and cards now have a copy & paste function, but I don’t know of one for helpers outside of yaml.

I have also yet to find that file, either, and it’s beginning to bother me. I spotted the one for automations ages ago, but nothing for sensors created in the UI. You’ll have to create your own sensor.yaml file and link it in the configuration.yaml file if you haven’t already. Then, you can easily copy and paste to make multiple sensors. I also like to add comments at the top so that I can fold the code for easy scrolling and identification as well as unique_ids so I can edit the names and icons from the UI rather than going back to yaml all the time. Something like:

# ESPresense Attribute Sensors 
  - platform: template
    sensors:
        insert_sensor_name_here:
            unique_id: "xxxxxxxxxxxxxxx" #File Editor has a lovely "Generate UUID" function in the Settings menu in the upper right-hand corner
            friendly_name: "Insert Friendly Name Here"
            unit_of_measurement: "in" #Whatever the unit is
            device_class: distance #If you want to
            icon_template: mdi:currency-usd #Might be easiest to use a placeholder and replace individually in the UI because you have to know the exact name of the icon you want in File Editor
            value_template: '{{ state_attr("sensor.black_beacon","distance") }}'
        insert_second_sensor_here:
            unique_id: "xxxxxxxxxxxxxxx"
            friendly_name: "Insert Friendly Name Here"
            unit_of_measurement: "in"
            device_class: distance
            icon_template: mdi:currency-usd
            value_template: '{{ state_attr("sensor.black_beacon","distance") }}'

And so on.

1 Like

Thank you! This does work.

1 Like

this thread has been so helpful… been trying to setup a helper for the below but when i test in tempate tester just returns null… for all the attributes in device_tracker…

am i missing something?

{{ state_attr(‘device_tracker.xxx’,‘Ap MAC’) }}

Look at the attributes under Developer Tools / States — might be a upper- vs lower-case problem.

Alternatively put this in the template tester:

{{ states['device_tracker.XXX']['attributes'] }}

and you’ll see what they really are. On my system it’s 'ap_mac', so:

{{ state_attr('device_tracker.XXX', 'ap_mac') }}
1 Like

I know this is an older thread. But I found it very helpful so far.

It got me this far.

How do I now get only the value?

Thanks,
-dk

{{ state_attr('sensor.weewx', 'Temperature')['value'] }}
1 Like

Awesome. I tried a bunch of things but didn’t think of that. Thanks so much. I’m trying to place it as a trigger for an automation.

platform: numeric_state
entity_id: >-
  sensor.apt_attic_4_1_sensor_air_temperature
  value_template: '{{ states('sensor.apt_attic_4_1_sensor_air_temperature')|float(0) - 30 }}'
above: {{ state_attr("sensor.weewx","Temperature")['value'] }}

In the value template I’ve tried the above and state.state and the above. But both give me a message like this, “Message malformed: Entity sensor.apt_attic_4_1_sensor_air_temperature value_template: ‘{{ state.state|float(0) - 30 }}’ is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘entity_id’]”

Yet it seems to work in the template tool.

You can’t use templates in the above. Read the docs:

The way to do this is to set up a template sensor (UI, under Helpers) with a state template as my response above, then use that new entity ID in the above configuration.

However, the error message is due to you indenting the value_template too much. Should be at the same level as entity_id; and you’ve got the quotes wrong. Use double quotes around the outside of the statement.

platform: numeric_state
entity_id: sensor.apt_attic_4_1_sensor_air_temperature
value_template: "{{ states('sensor.apt_attic_4_1_sensor_air_temperature')|float(0) - 30 }}"
above: sensor.your_new_sensor

The template tool only processes Jinja templates. It’s just outputting your YAML as text, not checking its validity.

Thanks. I’m new to this. I’m sure that’s obvious. Finally got it to save. I’ll find out tomorrow if it works.

Don’t think I would have figured it out without your help. The first one is always the hardest.

Alternatively to @Troon’s method, you could use a Template trigger and template the whole thing. Something like:

trigger:
  - platform: template
    value_template: "{{ states('sensor.apt_attic_4_1_sensor_air_temperature') | float(0) - 30 > state_attr('sensor.weewx','Temperature')['value'] | float(0) }}"

You’ll probably have to have a | float or | int filter after the state_attr("sensor.weewx","Temperature")['value'] as well because it will probably spit out a string like the states('sensor.apt_attic_4_1_sensor_air_temperature') would. Since you are doing math, everything has to be read as a number (i.e. a float or an int) in order for the template to work. Otherwise, you could possibly leave the state/attribute strings to be compared against each other, though it is still recommended to convert them.

1 Like

Ah, I can see how that would work as well. Thank you.