heyajohnny / cryptoinfo

Provides Home Assistant sensors for all cryptocurrencies supported by CoinGecko
GNU General Public License v3.0
46 stars 13 forks source link

Allow Multiplier to accept sensor value #26

Closed Shaun-Harrison closed 1 year ago

Shaun-Harrison commented 2 years ago

Hello

I'm currently using secrets for the multiplier - but this means when the value changes, I have to change the value in secrets, restart home assistant, which usually then means that a new cryptoinfo sensor is created with a _* prefix, meaning I have to make changes elsewhere (this is likely another issue)

To get around this, I'd like to use Vars (https://github.com/snarky-snark/home-assistant-variables) to hold the value , and this can then be updated via the ui

however when i put

  - platform: cryptoinfo
    cryptocurrency_name: "bitcoin"
    currency_name: "gbp"
    multiplier: "{{ states ('var.btc_amount') | float (0) }}" 
    update_frequency: 5

It complains that the value cannot be converted from string to float, the quotes around the statement are required as home assistant won't start up without them

Is there anything that can be done here?

TheHolyRoger commented 2 years ago

@Shaun-Harrison have you tried removing the space between states and ( ? Same for float

heyajohnny commented 2 years ago

@Shaun-Harrison Has the solution of @TheHolyRoger fixed it?

Shaun-Harrison commented 2 years ago

Same error unfortunately @TheHolyRoger @heyajohnny

2022-08-30 10:46:02.418 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.cryptoinfo_bitcoin_gbp fails Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 514, in async_update_ha_state await self.async_device_update() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 709, in async_device_update raise exc File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 192, in wrapper result = method(*args, **kwargs) File "/config/custom_components/cryptoinfo/sensor.py", line 154, in _update price_data = self.data["current_price"] * float(self.multiplier) ValueError: could not convert string to float: "{{ states('var.btc_amount') | float(0) }}"

With the code:

  - platform: cryptoinfo
    cryptocurrency_name: "bitcoin"
    currency_name: "gbp"
#    multiplier: !secret btc
    multiplier: "{{ states('var.btc_amount') | float(0) }}"
    update_frequency: 5
heyajohnny commented 1 year ago

I just saw that this issue is still open and I just had an idea about it. Maybe you could use a multiplier of 1 for the real sensor and multiply it in a template? Something like this:

- platform: template
    sensors:
      btc_multiplied:
        value_template: "{{
          ( states('sensor.cryptoinfo_bitcoin_gbp') | float(0)) * (states('var.btc_amount') | float(0))
          }}"
        unit_of_measurement: '£'
        friendly_name: My real BTC portfolio
Shaun-Harrison commented 1 year ago

Ah great idea! this has got around the issue!