How to access the attribute of an entity?

I have thermostat integrated to HA. I can see the entity (climate.upstairs) and also the attributes from the states page.

current_temperature: 75
temperature: null
target_temp_high: 81
target_temp_low: 68
current_humidity: 45

I want to show the history of current_temperature for the past couple days using either history graph card or custom:mini-graph-card, how can I access that attribute in the card?

Thanks.

Easiest is to create a sensor with the specific attribute you want to use. That one you then put in a graph.

You can extract the value through a value template:

value_template: '{{ states.binary_sensor.wdd2462081n170679_tire_warning.attributes.tirepressureRearRight }}'

If you don’t know how to do this, can you share the exact attribute you want to extract from your states page?

5 Likes

Base on your screenshot it should be something like

sensor:
  - platform: template
    sensors:
      climate.upstairs_temperature:
      friendly_name: Upstairs temperature
      entity_id: 
        - climate.upstairs
      value_template: '{{ states.climate.upstairs.attributes.current_temperature }}'
4 Likes

Or better yet (no errors if unavailable during startup):

value_template: "{{ state_attr('climate.upstairs', 'current_temperature' }}"
12 Likes

Worked perfectly!
Thanks all.

1 Like

Since this change is on the “sensor” section of the config, need restart HA to have it take effect, right?

Correct.

1 Like

I believe the difference between an entity attribute and a sensor (entity state) is that Home Assistant’s SQL database (History) does not keep the history of an attribute, but it keeps the state. So when you make a sensor from an attribute, it will cause the database to keeps its historic data.

Think of it this way, the developer of the integration thought it was an unnecessary waste of resources or cost to keep that data historically. If they thought it was essential to keep that data history, they would have made it a sensor instead of an attribute.

In your case, you would normally want to see a temperature history. I always add

secondary_info: last-updated

to all the sensors I view in the Lovelace so that I can see when they were updated. You might find that your thermostat current temperature is not producing a humanly sensible historic data.

1 Like

Hi,

sorry that I have to revive this old thread, but I’m trying to do the same as OP:

sensor:
  - platform: template
    sensors:
      media_player.frametv_title:
      friendly_name: FrameTV Media Title
      entity_id: 
        - media_player.frametv
      value_template: "{{ state_attr('media_player.frametv', 'media_title' }}"

Unfortunatly I get the following error in the config:

Invalid config for [sensor.template]: invalid slug media_player.frametv_title (try media_player_frametv_title) for dictionary value @ data['sensors']. Got OrderedDict([('media_player.frametv_title', None), ('friendly_name', 'FrameTV Media Title'), ('entity_id', ['media_player.frametv']), ('value_template', "{{ state_attr('media_player.frametv', 'media_title' }}")]). (See ?, line ?).

Can anyone please have a look and tell me what went wrong?

Thanks a lot!

You are trying to name an entity to sensor.media_player.frametv_title which can’t be done.
And I believe you have indentation issues also.
This is probably correct:

sensor:
  - platform: template
    sensors:
      frametv_title:      
        friendly_name: FrameTV Media Title
        entity_id: 
          - media_player.frametv
        value_template: "{{ state_attr('media_player.frametv', 'media_title' }}"
1 Like

Thanks for your quick help! I really appreciate it.

With your config I am getting the following error:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected '}', expected ')') for dictionary value @ data['sensors']['frametv_title']['value_template']. Got "{{ state_attr('media_player.frametv', 'media_title' }}". (See ?, line ?).

Mind taking another look? :slight_smile:

EDIT: I guess the “)” was missing. :slight_smile: Thanks again!

I just copied your yaml.

Yes I know. :slight_smile: And I just copied the solution above. :smiley:

Alright, I’ve made use of the latest yaml and inspecting the sensor through Developer Tools → State it tells me that state = unknown. It’s been more than 24h since I added the stanza, and I’ve also restarted HA repeatedly.

Why might this not work? How should I diagnose the issue?

  - platform: template
    sensors:
      thermostat_fan:
        friendly_name: Thermostat fan
        value_template: "{{ state_attr('climate.thermostat.attributes.fan', 'off') }}"
      outdoors_temperature:
        friendly_name: Outdoors temperature
        value_template: "{{weather.thermostat.temperature}}"

hello some one can help me?
I have entity climate as below
how I can read attribute current temperature?

{{ states.climate.termostato_stanzetta_aurora.attributes.current_temperature }}

1 Like

forgive me, but how do I create this entity?

What do you want to accomplish with the value? react on it in an automation, test it, change it, show it in a dashboard?

1 Like

Yess I want only the value…

I succeeded using the helper!
Thank you