Custom Component Avanza Stock

I recieve values, its just not alligning with the actual numbers from Avanza.
Se below the states from this sensor. Unless this is is some currency and not percent, but then it shouldnt be this much

name: Swedbank Robur Technology A
shares: 1
totalValue: 919.8
totalChange: -2.74
purchaseDate: '2022-06-15'
purchasePrice: 26.6957
profitLoss: 893.1
profitLossPercentage: 3345.5
totalProfitLoss: 893.1
unit_of_measurement: SEK
device_class: monetary
icon: mdi:cash
friendly_name: Swedbank R Technology

You have the wrong purchase price, checking the charts the value at 2022-06-15 is more around 700sek, 26sek was back in 1993.

And the math seems coreect since if you bought it for ~26 ans current value is ~900 then it has multiplied more than 30 times which gives 3000%

thats true
 thanks!

Is there a way to set a specific date for the changepercent etc.
Instead of using the 1year, 3year etc marks i would like a set date which would allow me to track the stocks changes since that date.

The 1 year, 3 years etc. are given pÄ the avanza api. When you set a purchasePrice you should get the following attributes profitLoss (i.e. +/- SEK sinve purchase date), profitLossPercent and totalProfitLoss (which is profitLoss multiplied by number of shares). Hope this is what you are looking for :slight_smile:

I see now that this is not at all documented in the README đŸ«Ł

Yeah that iÂŽve figured out :smiley:
but, since im buying stocks montly the number of shares and purchase price changes all the time
 Therefore a % from first purchase date would give me an estimate of how well a stock is performing.

IÂŽm aware this wount show me how much my investment has increased but it will still give me a good indication.

Ok, so what you actually would like is to be able to have changeSinceYYYY_MM_DD and changePercentSinceYYYY_MM_DD for one or more custom dates?

The current api endpoint that I am using doesn’t provide any historical data (only the fixed ones 1 week, three months, etc.). But I guess it should be possible to get the data since they have graphs on the website.

1 Like

exactly, however its not something that should be spent days solving.
I can live with the set dates.

The better, but probably a difficult solution, would be the posibility to automatically update the purchase price (and shares) on a monthly basis depending on how much you are investing. This would make the attributes in the integration usuable. But dont lose sleep over it :sweat_smile:

Hi, Could I get some assistance with setting this up please.

So far I have added sensors: !incude sensors.yaml to the configuration yaml and created a file called sesnors.yaml In the sensors.yaml file I have copied the example template and modified it with my share number. Where do I go from here. How do I see the data? The integration is not setup. When I try to add the integration it refers me back to the configuration.yaml

I am not familair with what I need to do with yaml code. I have migrated from Smartthings and learning HA. I mostly use the UI for automations etc.

First you need to learn how to install custom components (like this one). The simplest way is probably using https://hacs.xyz. I would also suggest to skip doing includes until you have something working in a single yaml file

Thanks, I have HACS installed and have installed the integration. I have many other integrations through HACS but these have a UI for configuration.

How do I setup this integration?

image

image

image

image

Well done then! I havent got around to make it possible to use the gui, I kinda prefer yaml myself

One error I can see directly is that you should remove “sensor:” from your sensor.yaml and the reindent so the “- platform
” is furthest to the left.

Under Developer tools there is a Check configuration button that hopefully will give you some meaningful information if something is wrong with your yaml config :slight_smile:

Thanks, removing the sensor: from the sensor.yaml code and re-indenting the text helped. I can now see the sensor value.

I was copying the code from the example code and also googling how to set up a custom sensor. The thing I have found with all HA integrations is that they presume that you know what you are doing when setting up the configuration.

The check configuration button wansn’t helpful as the wrong code doesn’t prevent HA from starting.

For info this is the code that worked in the sensor.yaml file

image

How can I restart this integration?
Just noticed that I haven’t recive any vaules for 5 days.

Probaly need to restart Home Assistant, do you have any errors in the log. I’ve been getting updated values everyday for at least a month.

Cant I do a reload? Like most other intergrations

Restarted HA and now I have values again but there must be aa easier way

No since this is a yaml-only integration and reloading wouldnt really do anything since the update of each sensor is stateless. Since you didnt provide any errors logs it is impossible to tell if there was something wrong with the integration or something else.

As it stopped so many days ago I dont have any logs. Will check if it stops again

Hi Claes, thank you for setting up and maintaining your integration! It’s easy to use.
With stocks it works fine (price an currency). But for gold/silver/platinum I have a little issue: The price I get is in USD and correct. But HA indicates that the price is in SEK.
Did I do anything wrong? Can I change this or is this a bug from the bank?

It used to work with our currencies, I only have Swedish assets nowdays though. Could I have your config for the “stocks” that does not work for you?

I have some vauge memory that home assistant recently did something about currencies so maybe they try to be smart


Same for me, gold/ silver the currency symbol is SEK