How to access the attribute of an entity?

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

Thanks - I have this which gives me two different variables. 1 with the change and 1 with the icon. How do I make this like the sensor and have both attributes in one sensor?

{% set dow_chg = state_attr('sensor.yahoofinance_dji' , 'regularMarketChange' ) %}
{% set dow_icon = state_attr('sensor.yahoofinance_dji' , 'icon' ) %}

Sensors have:

  • exactly one state, which is a string up to 255 characters;
  • zero or more attributes which have a lot more flexibility over what they can store.

You could do this, setting the change value as the state and the icon (whatever that is) as an attribute:

template:
  - sensor:
      - name: Dow change
        state: "{{ state_attr('sensor.yahoofinance_dji', 'regularMarketChange') }}"
        attributes:
          icon: "{{ state_attr('sensor.yahoofinance_dji', 'icon') }}"

What does your dow_icon look like? Is it a string, number, or an MDI icon reference? (I’d know if you’d provided the requested information above…)

Thanks. I’ll give that a try. Icon from the sensor is

icon=mdi:trending-up

which I assume is a string.

You could make that the actual icon of the new sensor then. See the template sensor docs.

1 Like

I’ll let you know! Thanks for the help!

I put this in my yaml file. Not sure if it can go into a helper or not

`  - platform: template
    sensors:
      dji_chg:
        friendly_name: DOW
        value_template: "{{ state_attr('sensor.yahoofinance_dji', 'regularMarketChange') | int }}"
        icon_template: "{{ state_attr('sensor.yahoofinance_dji', 'icon') }}"
      snp_chg:
        friendly_name: S&P
        value_template: "{{ state_attr('sensor.yahoofinance_gspc', 'regularMarketChange') | int }}"
        icon_template: "{{ state_attr('sensor.yahoofinance_gspc', 'icon') }}"
      nsdq_chg:
        friendly_name: NSDQ
        value_template: "{{ state_attr('sensor.yahoofinance_ixic', 'regularMarketChange') | int }}"
        icon_template: "{{ state_attr('sensor.yahoofinance_ixic', 'icon') }}"
`

And I have this in my glance card:

Exactly what I wanted! Thank you!