Using Homeassistant as my self hosted stock alert

I recently start to take an interest into the stock markets and investing into stocks directly instead of funds or the like. For some interesting stocks I was looking for an online stock alert website or app but soon realized that all of them required at least my email and would start sending me tons of spam mails. Why not use this great system I have at home the one which is focused on privacy?

Available Stock Sensors

Homeassistant does have the built in component alpha_vantage which requires an API key and in return offers you information on stocks and cryptocoins. Unfortunately the stock information is currently broken and I can only use it to track my Monero investment.

I took a short look in the HACS store and found the custom component avanza_stock.

The first stock sensor

Following the instructions I searched for the Microsoft stock, got the ID from the URL and created my first sensor:

sensor:
  - platform: avanza_stock
    stock: 3873
    name: avanza_microsoft
    monitored_conditions:
      - change
      - changePercent
      - country
      - currency
      - directYield
      - dividends
      - flagCode
      - hasInvestmentFees
      - highestPrice
      - id
      - isin
      - lastPrice
      - lastPriceUpdated
      - loanFactor
      - lowestPrice
      - marketCapital
      - marketList
      - marketMakerExpected
      - marketPlace
      - marketTrades
      - morningStarFactSheetUrl
      - name
      - numberOfOwners
      - orderDepthReceivedTime
      - priceAtStartOfYear
      - priceEarningsRatio
      - priceFiveYearsAgo
      - priceOneMonthAgo
      - priceOneWeekAgo
      - priceOneYearAgo
      - priceSixMonthsAgo
      - priceThreeMonthsAgo
      - priceThreeYearsAgo
      - pushPermitted
      - quoteUpdated
      - sector
      - shortSellable
      - superLoan
      - tickerSymbol
      - totalNumberOfShares
      - totalValueTraded
      - totalVolumeTraded
      - tradable
      - volatility

I am not sure if I will not all the attributes but I can always get rid of them again later. To display the sensor in the form of a stock tracker I chose the custom card mini-graph-card:

mini_graph_card_microsoft_stock

The configuration for this card is as follows:

- animate: true
  entities:
    - sensor.avanza_microsoft
  graph: line
  hour24: true
  hours_to_show: 720 # 1 month
  name: Microsoft Stock last Month
  show:
    extrema: true
    icon: true
    name: true
  type: 'custom:mini-graph-card'

The stock alert system

I want to be notified when the tracked stock reaches a certain upper or lower limit. These limits change frequently so I don’t want to restart homeassistant every time I want to adjust the limits.

I chose input_text elements to hold my tresholds:

input_text:
  avanza_microsoft_min:
    icon: mdi:arrow-collapse-down
    name: Min Alert
  avanza_microsoft_max:
    icon: mdi:arrow-collapse-up
    name: Max Alert

I combined them with an automation which sends me a notification as soon as the sensor is above the max or under the min limit:

automation:
- id: avanza_microsoft
  alias: Avanza Microsoft
  trigger:
    - platform: template
      value_template: >
        
    - platform: template
      value_template: >
        
  action:
    - service: notify.kevin
      data_template:
        message: >
          
          "Stock alert:  under threshold of "
          

I use Telegram as my notification system but you can use every supported notification component.

The whole sensor with the stock alert controls looks like this:

complete_microsoft_stock

As soon as the sensor reaches a treshold I get a notification:

stock_alert_johnson_and_johnson

The Microsoft Alert did not trigger yet so I had to choose another stock ;-)

As always you can find the whole configuration in my repo under https://github.com/eifinger/homeassistant-config

Comments