How to access the attribute of an entity?

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

Trying to use a Glance card to show the value of the US stock market indices - DJ, S&P, NasDaq. See below:

I’m using the Yahoo Finance HACS integration and it does a fine job except two things.

  1. You see the USD? This is an index and not a dollar amount like a regular stock.

  2. The value is currently set to the overall volume. I would rather see the market change in the value of the sensor. For example +242.

I realize it’s probably a templating in config.yaml file but need two or three attributes to be copied and not the whole thing.

Suggestions??

Card code is:

show_name: true
show_icon: true
show_state: true
type: glance
entities:
  - entity: sensor.yahoofinance_dji
  - entity: sensor.yahoofinance_gspc
  - entity: sensor.yahoofinance_ixic

The sensor has this attributes:

Attributes

CurrencySymbol
$
Symbol
^DJI
QuoteType
INDEX
QuoteSourceName
Delayed Quote
MarketState
PRE
AverageDailyVolume10Day
337,895,000
AverageDailyVolume3Month
315,533,114
RegularMarketChange
242
RegularMarketChangePercent
0
RegularMarketDayHigh
38,057
RegularMarketDayLow
37,796
RegularMarketPreviousClose
37,806
RegularMarketPrice
38,049
RegularMarketVolume
402,976,676
RegularMarketTime
1,706,219,549
DividendDate
Unknown
FiftyDayAverage
36,720
FiftyDayAverageChange
1,328
FiftyDayAverageChangePercent
3
PreMarketChange
0
PreMarketChangePercent
0
PreMarketTime
0
PreMarketPrice
0
PostMarketChange
0
PostMarketChangePercent
0
PostMarketPrice
0
PostMarketTime
0
TwoHundredDayAverage
34,716
TwoHundredDayAverageChange
3,332
TwoHundredDayAverageChangePercent
9
FiftyTwoWeekLow
31,429
FiftyTwoWeekLowChange
6,619
FiftyTwoWeekLowChangePercent
21
FiftyTwoWeekHigh
38,109
FiftyTwoWeekHighChange
-60
FiftyTwoWeekHighChangePercent
0
Trending
up

Please paste this into Developer Tools / Template and paste the output back here, but format it correctly (surround code with three backticks ```).

{{ states['sensor.yahoofinance_dji']['attributes'] }}

Like this (but with the code above):

and I want the equivalent of this:

{'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>, 'unit_of_measurement': '%', 'device_class': 'battery', 'friendly_name': '10DTB42 Battery'}

At a guess, you should be able to set up a template sensor via the UI with a template like:

{{ state_attr('sensor.yahoofinance_dji', 'RegularMarketChange') }}
1 Like