How to Track Stock Prices in Home Assistant

Full guide here: https://smarthomepursuits.com/how-to-track-stock-in-home-assistant/

In this guide, I’m going to show you how to quickly and easily track your favorite stocks from within Home Assistant. Whether you are wanting to track GME, AMC, BB, or any of the other meme stocks that are exploding in popularity lately, this is a great way to do right from within Home Assistant. If you are more of a cryptocurrency fan, I have a How To Track Crypto in Home Assistant guide you can follow.

By the end of this guide, you will be able to track any stock you want, create a beautiful Lovelace card for each one, and then create an automation once a stock hits a certain $ threshold.

This guide assumes you already have HACS installed.

Step 1: Install Avanza_Stock from the HACS Store

Click HACS on your sidebar. Then search for Avanza Stock.

Then, restart Home Assistant from Configuration > Server Controls > restart Home Assistant.

Step 2: Find Stocks

Next, you need to find the stock market stocks that you want to track. In my case, I want to track Gamestop and AMC (GME, AMC).

Go to this URL and search for them: https://www.avanza.se/ This is a Swedish site, but if using Google Chrome or Edge it’ll prompt you to translate, which I recommend you do.

Click one of the stocks on the ticker at the top or click “The Stock Market Today”. This will bring you to a page where you can search for stocks.

Then click Search in the top right corner. Search for a stock and then click it.

Located in the URL you will the stock ID number. Grab as many stock ID’s during this step as you want to track and paste them into Notepad or something similar temporarily.

  • Gamestop = 194698
  • AMC = 450548

Step 3: Create Sensors in Configuration.yaml

Next, click File Editor in Home Assistant > configuration.yaml. If you only want to track one stock with all defaults, create a sensor like this:

sensor:
  - platform: avanza_stock
    stock:
      - id: 194698
        name: Gamestop

Since we are going to track two stocks, my sensor looks like this:

sensor:
  - platform: avanza_stock
    stock:
      - id: 194698
        name: Gamestop
      - id: 450548
        name: AMC

Note: If you already have a sensor: defined in configuration.yaml, just add everything starting at - platform:For example, I already had several sensors created for my Upcoming Sports Card so I just added this at the very top of my existing sensor:

Step 4: Create a Basic Entities Card to Show Stock Prices

As a quick test, you can add a basic “entities” card to Lovelace by adding sensor.gamestop and sensor.amc. Here’s what that will look like:

Optional: Create Custom Card with Graph

Like I said at the beginning of the guide, you will need HACS installed for this part. Install the Mini-Graph-Card frontend integration.

Add each of these as separate Lovelace cards (Add Card > Manual). Note that if you only see a flat line, that doesn’t mean the graph isn’t working. That just means you are probably tracking prices after the markets have closed at 4pm ET, therefore the stock price hasn’t changed. Wait until the market opens again at 9:30am ET and view it again.

Gamestop Card

entities:
  - sensor.gamestop
graph: line
hour24: false
hours_to_show: 24
name: Gamestop Price - Last 24hrs
show:
  extrema: true
  icon: true
  name: true
type: custom:mini-graph-card

AMC Card (bar graph)

type: custom:mini-graph-card
name: AMC Price - Last 24hrs
icon: hass:currency-usd
entities:
  - sensor.amc
hour24: false
hours_to_show: 24
show:
  extrema: true
  icon: true
  graph: bar
  name: true
  name_adaptive_color: true
  icon_adaptive_color: true

For even more card customization, you can add any of these under the show: section:

  • graph: false (doesn’t show graph)
  • name_adaptive_color: true (changes name to match bar graph color)
  • icon_adaptive_color: true (changes icon to match bar graph color)
  • A full list of options can be found here

Wrapping Up

Hopefully you found this stock tracker within Home Assistant guide useful!

The next thing I need to figure out is how to create an alert once a stock price hits a certain threshold. Once I get that figured out, I’ll be sure to update this guide.

15 Likes

This looks awesome, nice work!

:rocket: :full_moon: :gem: :raised_hands: :gorilla:

You’re welcome, glad you like it!

Thanks for this, very nice howto! Do you guy’s found a way to have these graph with candle bar (created using high, low, open, close) ?

Like explained in this post: Lovelace: Bar Card - #532 by mirekmal

Thank you. This works so well.

You’re welcome!

I wish Avanza did UK stock prices as well. Great tutorial

1 Like

Anyone who know how I can get an index like OMX30 from Stockholm Nasdaq?

i use the following to scrape stock price from a public website

  - platform: scrape
    name: AEX
    resource: https://www.iex.nl/Koersen/Indices/Europa.aspx
    select: '#\31 2272LastPrice'
    value_template: '{{value[:-3] | float}}'
    scan_interval: 1800

2 Likes

Same here. I’m having some success with Yahoo Finance. Give that a go.

1 Like

Heres some colorfull line setup :slight_smile:

type: custom:mini-graph-card
icon: hass:currency-eur
entities:
  - entity: sensor.nokia
name: Nokia (NOKIA)
line_width: 2
hour24: true
hours_to_show: 160
points_per_hour: 1
show:
  labels: true
color_thresholds:
  - value: 4.2
    color: '#C70039'
  - value: 4.3
    color: '#FF5733'
  - value: 4.35
    color: '#FFC300'
  - value: 4.38
    color: '#008000'
1 Like

Yahoo fnance is great and setup is simple. you can use native HA charts to display/ I am using Entities card to display values.
image

image

1 Like

Greetings, I really like your entities card with multiple lines. Would you mind sharing your entities card yaml? I would really appreciate it!!
Thanks,
Norm

type: entities
entities:
  - entity: sensor.yahoofinance_enb_to
    name: Enbridge Inc.
    type: custom:multiple-entity-row
    state_header: Price
    format: precision2
    secondary_info:
      attribute: regularMarketChangePercent
      unit: '%'
    entities:
      - attribute: regularMarketDayLow
        name: Low
        format: precision2
      - attribute: regularMarketDayHigh
        name: High
        format: precision2
  - entity: sensor.yahoofinance_fts_to
    name: Fortis Inc.
    type: custom:multiple-entity-row
    state_header: Price
    format: precision2
    secondary_info:
      attribute: regularMarketChangePercent
      unit: '%'
    entities:
      - attribute: regularMarketDayLow
        name: Low
        format: precision2
      - attribute: regularMarketDayHigh
        name: High
        format: precision2
  - entity: sensor.yahoofinance_nxr_un_to
    name: NEXUS REALESTATE
    type: custom:multiple-entity-row
    state_header: Price
    format: precision2
    secondary_info:
      attribute: regularMarketChangePercent
      unit: '%'
    entities:
      - attribute: regularMarketDayLow
        name: Low
        format: precision2
      - attribute: regularMarketDayHigh
        name: High
        format: precision2
  - entity: sensor.yahoofinance_pd
    type: custom:multiple-entity-row
    state_header: Price
    format: precision2
    secondary_info:
      attribute: regularMarketChangePercent
      unit: '%'
    entities:
      - attribute: regularMarketDayLow
        name: Low
        format: precision2
      - attribute: regularMarketDayHigh
        name: High
        format: precision2
  - entity: sensor.yahoofinance_pep
    type: custom:multiple-entity-row
    state_header: Price
    format: precision2
    secondary_info:
      attribute: regularMarketChangePercent
      unit: '%'
    entities:
      - attribute: regularMarketDayLow
        name: Low
        format: precision2
      - attribute: regularMarketDayHigh
        name: High
        format: precision2
2 Likes

EDIT: I didnt sent the scan interval. Whoops. Thanks!

Hello! I’m still learning the ropes so please bear with me.
Maybe im doing something wrong. The stock price shows up fine along with the attributes. The only thing I cant get to show up in a graph. If one does show up its just a strait line. I know the stock is moving though. I would appreciate any help. thank you.
Example with attributes but shows flat graph when i click on it

type: entities
entities:
  - entity: sensor.yahoofinance_recaf
    name: RECAF
    type: custom:multiple-entity-row
    state_header: Price
    format: precision2
    secondary_info:
      attribute: regularMarketChangePercent
      unit: '%'
    entities:
      - attribute: regularMarketDayLow
        name: Low
        format: precision2
      - attribute: regularMarketDayHigh
        name: High
        format: precision2

Example that shows graph all the time but its flat.

entities:
  - sensor.yahoofinance_recaf
graph: line
hour24: false
hours_to_show: 24
name: RECAF
show:
  extrema: true
  icon: true
  name: true
type: custom:mini-graph-card

Just an FYI, I’m using the yahoo finance integration and its great. As a UK user, I have noticed one issue. If the stock is priced in GBp (lowercase p), that means its in pence, not pounds. This makes it look like its worth 100 times its value.

I tried to fix this using the target_currency parameter for the symbol, but it did nothing, so I ended up making a super simple template sensor for this:-

- sensor:
  - name: yahoofinance_iitu_l_gbp
    state: >
      {{ states('sensor.yahoofinance_iitu_l')|float(0)/100 }}

Could you (or someone else) please give me a helping hand which integration that is and if it’s working from Europe?