Custom Component Avanza Stock

Oh what a pity… :neutral_face:

If you would use a stock course API being more general it might enhance the number of people interested in using your custom component. It has more functionality, than the Alpha Vantage component. I like the option to enter the number of stocks and to calculate the total capital (and the automation you described in github (which I haven’t tried right now, because my stocks do not work)…

Yep, I know. BUT: That’s only the price in US$, but I buy those in €uro; therefore the calculated values do not reflect my stocks

Do you know of any such API that is free to use? Or at least allows maybe 100calls/hour.

Done some work using this component today. Works great once I had all the stock id numbers correctly (only tried Swedish Market and found the ids quite easily after searching on avanza.se). Thank you so much for it.

I have a question on how often the values are updated? Can I set that myself somehow?

Happy to hear that you enjoy using it. :slight_smile:

Yeah it takes some time to find all the ids. I will try and see if I can loop through all ids and make a list and upload it to github, then we could just search that list.

They are updated once an hour, if you are interested in the code you can search for SCAN_INTERVAL. You can change it yourself to 60s or whatever you like :slight_smile:

sensor:
  - platform: avanza_stock
    stock: 5361
    scan_interval: 60

I also use the homeassistant.update_entity service call just before I send my daily summary.

Nice. Every hour is good enough for me, but it’s nice to know it could be changed. Also thanks for the advice on the update_entity, hadn’t thought about that.

Also, as regards getting all the ids, maybe a webscraping effort of https://www.avanza.se/aktier/lista.html and the following could make that list happen? I haven’t tried yet, just had a look on the avanza site to find somewhere with all or most available ids…

Hi Claes,
I configured your component yesterday for a french stock, it works like a charm !
Thanks for your work

1 Like

Here is a list of all Swedish stocks, used the link you provided :slight_smile:

I currently have a scripts that goes through all the ids from 0 to 1 000 000, but it takes a little time :stuck_out_tongue: So that should include everything I hope.

1 Like

I looped through all id between 0 and 1000000 and came up with this list. Over 50000 items, not all of them are stocks. Should be easy enough to find what you are looking for.

1 Like

Big fan of this custom component, great work! :smiley: However, I got tired of copying stock id, names, shares etc for every stock I want to put in a sensor, so instead I created a custom Tampermonkey script that generates the config for me! If you want to check it out, I made it available here: https://github.com/Miicroo/TamperMonkey/tree/master/avanza.

How it works: you navigate to an avanza page and the script scrapes the data from the page and adds a link to a modal popup. If you click the link it shows the configuration together with a button to copy the contents. You can also change which monitored_conditions that you want to use.

Best feature yet: If you are logged in it will parse the number of shares that you own :smiley:

Demo:

2 Likes

Wow! This makes it so simple to add new stocks :smile: Would be cool to integrate this into the config_flow directly in HA :grin:

1 Like

Sweet, never heard of the config_flow before. In the docs it seems to be a python integration or some sort of python scripts to configure the sensor?

This looks really good! Will try it out. Since Avanza added info for US stocks I guess I can create a list of upcoming dividend payouts!

It is a feature in the latest release of HACS (https://github.com/custom-components/hacs/releases). If I implement that you will be able to add sensors through Configuration-> Integrations.

Edit. Maybe thay is also what Home Assistant calls it.

1 Like

Would it be possbile to export csv from avanza with holdings and load your tool with it?

Exactly one of the things I use it for :grin: I have a python_script for it, I can’t access it right now but I could publish it later today.

That is a feature of Home Assistant, the bonus HACS gives is that if you use that to initially “install” the integration, you do not have to restart HA before you can configure it.

1 Like

It would be possible, but I think that you might want to just write a custom python script for that. The only feature of having my tampermonkey script is that you get the data directly from the avanza stock webpage that you are on. If you already have the exported csv you already have that data somewhere else, so I guess that it is just a matter of formatting it from csv to yaml.

On the other hand, it would be fun to create a new script that you can trigger from for instance your profile which creates a configuration out of all of your owned stocks. :smiley:

My python_script that sends a summary of upcoming dividend payouts, The reason I decided to use python_script is that it makes it really easy to sort the data.

# Stocks
stocks = data.get('stocks', [])

# Collect data
stock_data = []
for entity_id in stocks:
    entity = hass.states.get(entity_id)
    attributes = entity.attributes
    ticker_symbol = attributes['tickerSymbol']
    payment_date = attributes['dividend0_paymentDate']
    amount_per_share = attributes['dividend0_amountPerShare']
    shares = attributes['shares']

    if payment_date == 'unknown':
        continue

    stock_data.append((payment_date, amount_per_share, shares, ticker_symbol))

# Sort stock data, soonest dividend at top
stock_data.sort()

# Create message
message = '<b>Dividend Summary {}</b>'.format(hass.states.get('sensor.date').state)
message = message + '<code>\n'
for (payment_date, amount_per_share, shares, ticker_symbol) in stock_data:
    message = message + '{}{}{}{}\n'.format(ticker_symbol.ljust(10),
                                            payment_date,
                                            '{:.2f}'.format(amount_per_share).rjust(6),
                                            "({0:.2f})".format(shares*amount_per_share).rjust(10))
message = message + '</code>'

# Send notification
hass.services.call(
    'notify',
    'telegram',
    {'message': message})

I call it like this

  action:
    - service: python_script.telegram_dividend
      data:
        stocks:
          - sensor.stock_a
          - sensor.stock_b
          - sensor.stock_c
1 Like

This is really cool. I currently use https://nnava.github.io/ for creating a calendar. When do you run the action? What trigger?

BR