elden1337 / hass-peaqnext

Utility sensors to help find the next best usage of your appliances
Other
7 stars 0 forks source link

Failed to setup: More than one Nordpool entity found. Cannot continue. #21

Closed SkywiperSolutions closed 1 year ago

SkywiperSolutions commented 1 year ago

Installed the peaqnext integration. Rebooted HA. One sensor was added. The sensor failed to setup with the folowing message in Log: "more than one Nordpool entity found. Cannot continue".

My Nordpool integration contains one device and one entity?

Any clues?

elden1337 commented 1 year ago

Hmm, possible hickup if you haven't got two sensors. It's a fairly straightforward check in nordpool.py demanding that the number of sensors from nordpool-integration is just one.

Can you try to reboot again and see if problem is gone?

SkywiperSolutions commented 1 year ago

Tried another reboot. Same error:-( image image

elden1337 commented 1 year ago

ok, and in developer-tools/state if you query for .nordpool you will only get one sensor? no double installation or anything like that that's lingering in your system? I'm asking for the domain "nordpool" and then querying for sensors in that check.

Are you keen on debugging it a bit yourself? if so you can replace the nordpool.py in peaqnext with this code to get a better understanding of what it's seeing as sensors in that check:

import logging
import homeassistant.helpers.template as template
import asyncio
from custom_components.peaqnext.service.nordpool.nordpool_dto import NordpoolDTO

_LOGGER = logging.getLogger(__name__)
NORDPOOL = "nordpool"

class NordPoolUpdater:
    def __init__(self, hub, test:bool = False):
        self.hub = hub
        self._nordpool_entity: str | None = None
        self._is_initialized: bool = False
        self._currency: str = ""
        self._state: float = 0.0
        self._prices: list[float] = []
        self._prices_tomorrow: list[float] = []
        self._use_cent: bool = False
        if not test:
            self.state_machine = hub.state_machine
            self.setup()

    @property
    def is_initialized(self) -> bool:
        return self._is_initialized

    @property
    def currency(self) -> str:
        return self._currency

    @property
    def state(self) -> float:
        return self._state

    @state.setter
    def state(self, val) -> None:
        if self._state != val:
            self._state = val

    @property
    def prices(self) -> list[float]:
        return self._prices

    @prices.setter
    def prices(self, val) -> None:
        self._prices = val

    @property
    def prices_tomorrow(self) -> list[float]:
        return self._prices_tomorrow

    @prices_tomorrow.setter
    def prices_tomorrow(self, val) -> None:
        self._prices_tomorrow = val

    @property
    def use_cent(self) -> bool:
        return self._use_cent

    @property
    def nordpool_entity(self) -> str:
        return getattr(self, "_nordpool_entity", "")

    async def async_update_nordpool(self) -> None:
        if self.nordpool_entity is not None:
            ret = self.state_machine.states.get(self.nordpool_entity)
            if ret is not None:
                await self.async_set_nordpool(ret)
            else:
                _LOGGER.debug(
                    f"Could not get nordpool-prices. Nordpool-entity: {self.nordpool_entity}. Retrying..."
                )

    async def async_set_nordpool(self, ret) -> None:
        _result = NordpoolDTO()
        await _result.set_model(ret)
        if await self.async_update_set_prices(_result):
            await self.hub.async_update_prices(
                (self.prices, self.prices_tomorrow)
            )
            self._is_initialized = True

    async def async_update_set_prices(self, result: NordpoolDTO) -> bool:
        ret = False
        if self.prices != result.today:
            self.prices = result.today
            ret = True
        if result.tomorrow_valid and result.tomorrow is not None:
            if self.prices_tomorrow != result.tomorrow:
                self.prices_tomorrow = result.tomorrow
                ret = True
        else:
            self.prices_tomorrow = []
            ret = True
        self._currency = result.currency
        self._use_cent = result.price_in_cent
        self.state = result.state
        return ret

    def setup(self):
        try:
            entities = template.integration_entities(self.state_machine, NORDPOOL)
            _LOGGER.debug(f"Found {list(entities)} Nordpool entities.")
            if len(list(entities)) < 1:
                raise Exception("no entities found for Nordpool.")
            if len(list(entities)) == 1:
                self._nordpool_entity = entities[0]
                _LOGGER.debug(
                    f"Nordpool has been set up and is ready to be used with {self.nordpool_entity}"
                )
                asyncio.run_coroutine_threadsafe(
                    self.async_update_nordpool(),
                    self.state_machine.loop,
                )
            else:
                _LOGGER.error(f"more than one Nordpool entity found. Cannot continue. These are the sensors found: {list(entities)}")
        except Exception as e:
            _LOGGER.error(
                f"I was unable to get a Nordpool-entity. Cannot continue.: {e}"
            )
elden1337 commented 1 year ago

I am unable to reproduce this. Do tell if you still have an issue otherwise please close issue.

SkywiperSolutions commented 1 year ago

Hi. Thanks for your swift replies. Sorry I've been away for some days. So investigated a bit further:

  1. Yes, indeed I have two Nordpool entities, even though the integration UI shows only one. My conclusion is that when the configuration option "additional_costs:" is used; a second entity is created that looks exactly like the "raw" entity but has prices with the additional cost calculation applied.
  2. I believe almost everyone uses the "additional_costs:" option, so why am I the first to report this issue? Shouldn't it be supported to choose either the "raw" or "full-price" entity by peaq-next?
  3. In Denmark there's an option get a refund of the tax paid per kWh to charge EV vehicles. This means that a peaq-next device used for EV charging should actually use the "full-price"-Nordpool entity, BUT subtracted a specific amount per kWh. Would you consider adding an optional configuration option called something like "price_subtract" that defaults to "0" and is subtracted the Nordpool values?

Thanks again for you excellent add-on and support!

elden1337 commented 1 year ago

I also use additional but only have one. Hmm. Guess ill have to redo some steps of the init. Problem is that i want it to be automatic and not user config (ie error prone).

Please tell the exact name of these two sensors. Perhaps i can come up with a pattern.

elden1337 commented 1 year ago

And for your (3, yes i can consider that. Can you copy that into a new issue?

SkywiperSolutions commented 1 year ago

Both my sensors have names that have been renamed. Another user have the two as well and he haven't renamed them. They are called:

elden1337 commented 1 year ago

I'll have to look at how the integration constructs the names i guess. Or look at attributes in the sensors and favor the one with additional costs if there are more than one.

SkywiperSolutions commented 1 year ago

Maybe you could distinguish them by the attribute "Additional costs current hour". This attribute has the value "0" in the Raw sensor.

elden1337 commented 1 year ago

Handled in https://github.com/elden1337/hass-peaqnext/releases/tag/v0.4.0. Reopen issue if not working.