Custom Component Avanza Stock

Glad I could finally contribute as well.

Since you and me (and perhaps a few others in this thread) are Swedish speaking (i guess :slight_smile: ), here’s the same script, but in Swedish:

script: 
  dagens_avanza:
    alias: "Dagens avansa"
    sequence:
      - service: tts.google_translate_say
        data_template:
          entity_id: media_player.googlehome_koket            
          message: >
            HĂ€r kommer de senaste aktiekurserna. 
            {%- for device in states %}
            {%- if 'avanza_stock' in device.entity_id %}
            {%- set forandring = device.attributes.changePercent %}
            {{ device.attributes.name }}: 
            {%- if forandring > 0 %} Upp {{ forandring | replace("."," komma ") }} procent.
            {%- elif forandring < 0 %} Ner {{ forandring | replace("."," komma ") }} procent.
            {%- elif forandring == 0 %} OförÀndrad.
            {%- endif %}
            {%- endif %}
            {%- endfor %}
          language: 'sv'
          cache: false
1 Like

Thank you for this component. Just came across it myself when Alpha Vantage free tier kicked in at 5 per minute. For the scan interval, do you know if the API has limits that would not allow me to drop the scan interval to every 5 minutes or something more frequent? I’d like to create some stop loss alerts using input_numbers and then have the automation trigger when it drops below those numbers. I think something more frequent then every 60 minutes would be best but I dont want to hit API limits too.

I don’t know of any limitations so you can freely change the scan interval to whatever you like.

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

Beware of that the data is not realtime, I think it is delayed 15min.

Installed this integration on my HA 0.106.5. Rebooted the server and HA would not start. Had to remove all files för Avanza under Custom Componets to get HA working again

Was this the first time you installed the integration? Some error logs would be helpful :slight_smile:

Log-filés are reset when Home Assistant Is restarted so I dont have anything. All I remember is that there where lots of python errors

I followed instructions on GitHub. I also added the example there to configuration.yaml

Thoose errors would have been useful, hard for me to help without logs. I’ll see if I have time to try out 0.106.5, on 106.2 right now. I would recommend to install it using HACS.

Tried again to install and now it worked :thinking:. Strange

Its a great component. Does exactly what I was after

1 Like

Really nice component, thank you for great job!

One question, maybe just me being stupid, but when using monitored_conditions I do get readings from them, but how can I display them in my frontend? When creating a card in lovelace I can only find the sensor.stockname, but when the card is created I can click on it and then I can see the rest of the monitored conditions. But how can I pick one of them to display on its own in a card?

Thank you! :slight_smile:

One way is to create a template sensor for each attribute that you want access to. https://www.home-assistant.io/integrations/template/

I’ve used https://www.home-assistant.io/lovelace/markdown/ to do this sometimes.

There is also a lot of awesome custom-cards, a quick googling gave me https://github.com/custom-cards/entity-attributes-card. If you are not using it already I would recommend to start using HACS (https://hacs.xyz/).

If you are up for it, you can always create your own custom-card that is specific to this sensor.

Aha, shouldn’t be that hard then!
I am already using HACS and quick look at the links you shared it seems like it should not be that hard to get what I want actually :slight_smile:
Ill give it a try tonight

Thanks for the help!
And again, great job on the component :slight_smile:

Works great! Thanks, I also saw you put the " % gain/loss calculation " on your todo list! Bravo. Great work on this, seems to be more robust and works better then the alpha vantage integration. I’d love to have this integrated directly into hass for easier updates / watching the repo, but you can’t win them all I suppose. I’m going to open up a short ticket for something that may interest you as an enhancement. Fractional shares! I’m one of many people now who partake in fractional share buying/selling. I know this integration doesn’t do a whole lot of that, but for people (and when you have gain loss calc working) who use fractional shares, this would mean more accurate reporting in hass. Just a thought, cheers!

Tjena @claha!
Thanks for this component - love it! have you considered to specify the opening hours for each market in order to not update the stocks when the markets aren’t open?

Has anyone come up with a beautiful way of presenting this? I’m quite new to Home Assistant and haven’t found the best way for that yet?

1 Like

I created mini-graph-cards and a price alerts for my sensors with this integration. You can find my config here
I also have two blog posts on this:

https://blog.kevineifinger.de/archive/2019/10/17/Using-Homeassistant-As-My-Self-Hosted-Stock-Alert.html

https://blog.kevineifinger.de/archive/2019/11/02/Creation-Script-For-My-Avanza-Stock-Trackers.html

3 Likes

Wow, that’s really helpful! Very nice! Perfect for a rookie like me to piggyback :slight_smile:

Can I just ask, in order to display any of the monitored_conditions/attributes, do I use a template sensor or what is the easiest way to get the changePercent dispayed?

How do you @eifinger get a history graph of the stock, mine is flat and the dates are todays dates only:

Template sensor is one alternative, there exist some custom lovelace cards, there are probably more

Or you could try to create a custom card yourself aimed at this sensor :wink:

1 Like

I’ll look into that once I start understanding how HA in general works :wink:

Maybe not even possible but has anyone managed to get a refresh button to get an instant refresh of the stock prices?

Go to Developer Tools -> Services and use homeassistant.update_entity :blush: You might need to enable Developer Tools
 You can write a script that updates all your stocks using that service, I use it update all my stocks after stock market closes and then send a daily summary

1 Like

You need to have the hours of data in the database. If you just added the sensors it will take some days until the graphs show something else than a flat line

1 Like

Thanks, makes sense :slight_smile:

Can I ask if you have solved it so that the graph show this year in a graph, meaning how a stock/market has developed since January 1st or since the first of the current month (April 1st)?