custom-components / sensor.avanza_stock

Custom component to get stock data from Avanza for Home Assistant
MIT License
46 stars 10 forks source link

Will it work for funds too? #120

Closed jamiekris closed 3 years ago

jamiekris commented 3 years ago

I tried the same approach for a couple of funds, however seems not to work. Has this been discussed before? Might be a cool feature to add funds too.

claha commented 3 years ago

It should work for funds too, I'm tracking a few myself. Can you share an example config that does not work and any error messages?

jamiekris commented 3 years ago

I think I misunderstood it initially. It works fine I think.

How would one track one's fund evolution over time? Simply plot ((current price - buy price) * number of shares)? In my config I have as below:

sensor:

How will I get entities that track the purchase price and number of shares?

claha commented 3 years ago

If you have set shares to some value there should be an attribute totalProfitLoss which does exactly that calculation. To save that over time I would create a template sensor that extracts that attribute, look here https://www.home-assistant.io/integrations/template/#sun-angle for a simple example.

jamiekris commented 3 years ago

Is this the right approach? Seems like I am missing something still.

sensor:
  - platform: avanza_stock
    stock: 878733
    name: Avanza Global
    purchase_price: yyyy
    shares: xxxx
template:
  - sensor:
      - name: Avanza Global Total Value
        unit_of_measurement: "SEK"
        state: "{{ '%+.1f'|format(state_attr('Avanza Global', 'TotalValue')) }}"
claha commented 3 years ago

Looks about right except that TotalValue should be totalValue, so that probably gives you some error.

jamiekris commented 3 years ago

Strange... I tried to change to as below, but still the attribute value is not reported as an entity

template:
  - sensor:
      - name: Avanza Global Total Value
        unit_of_measurement: "SEK"
        state: "{{ '%+.1f'|format(state_attr('avanza_global', 'totalValue')) }}"
claha commented 3 years ago

If it does not work there should be some kind of error in the log. I would otherwise try and remove the format stuff just use "{{ state_attr(...) }}"

jamiekris commented 3 years ago

The following seemed to do the trick


- platform: template
    sensors:
      avanza_global_current_holding:
        value_template: '{{ states.sensor.avanza_global.attributes.totalValue}}'
        unit_of_measurement:  SEK

Thanks. I will close this issue! Very useful project btw!