martinarva / dynamic_energy_cost

35 stars 9 forks source link

Error messages about currency and long term statistics #29

Closed dunxd closed 5 days ago

dunxd commented 1 month ago

Describe the bug I see the following statistics message for each device after upgrading to 0.4.2:

The unit of this entity changed to 'EUR' which can't be converted to the previously stored unit, 'GBP'. If the historic statistic values have a wrong unit, you can update the units of the old values. The values will not be updated. Otherwise you can choose to delete all historic statistic values, and start over.

How do you want to fix this issue?

Update the unit of the historic statistic values from 'GBP' to 'EUR', without converting.

Delete all old statistic data for this entity

To Reproduce Steps to reproduce the behavior:

  1. Update to 0.4.2
  2. Click on Developer Tools > Statistics
  3. Scroll down to messages about entities created by this integration.
  4. See error

Expected behavior Maintain the currency set by the electricity price sensor

Additional context Might this be due to how a default currency is applied? Might it be fixed by a similar method to #27 ?

martinarva commented 1 month ago

@JannikL87 could you look into it?

JannikL87 commented 1 month ago

Can you verify if the same sensor is being used? I think a new one has been created, with default EUR, and as such the link with GBP is lost. Suggestions:

Manually do: remove old entities : _entity_some_sensorid rename newly created entities (after 0.4.2): _entity_some_sensor_id2 -> rename to _entity_some_sensorid

I think this would be sufficient.

dunxd commented 3 weeks ago

When I reported this new entities had been created, I think as a result of a new version of the integration being installed.

However, today after upgrading Home Assistant to 2024.6.2 and restarting afterwards all the currencies are back as EUR with no new entities.

Would it be simpler to set the currency manually at the time the device is configured? However the currency is being picked up from the entity with the current price seems unreliable.

dunxd commented 3 weeks ago

I see this error after the Home Assistant server restarted with the update and expect this is the issue:

This error originated from a custom integration.

Logger: custom_components.dynamic_energy_cost.energy_based_sensors
Source: custom_components/dynamic_energy_cost/energy_based_sensors.py:129
integration: Dynamic Energy Cost (documentation, issues)
First occurred: 07:47:01 (12 occurrences)
Last logged: 07:47:01

Unit of measurement not available or invalid for sensor sensor.octopus_energy_electricity_19e5120262_1610030568550_current_rate, defaulting to 'EUR'.
JannikL87 commented 3 weeks ago

I am not affected by this, presumably because I have EUR currency. I know there is EUR set somewhere as default.

dunxd commented 3 weeks ago

I left it a day in case the currency changed back to GBP with the next charging session. Unfortunately it has remained on EUR 24 hours later.

dunxd commented 3 weeks ago

Reloading the integration for each device fixes the currency. I suspect that if the integration starts before a value exists for the rate, then the currency is set as EUR as default, and this is not evaluated again till the integration is restarted.

Unfortunately this seems to happen at least some of the time when Home Assistant restarts after an update.

dunxd commented 3 weeks ago

This happened today after the 2024.6.3 update of HA was applied.

The issue was not resolved by restarting Home Assistant after the update completed, nor subsequent restarts. Reloading each instance of this integration resolves the issue, and then requires clearing up the different units in Developer Tools > Statistics.

bipsendk commented 2 weeks ago

Also having isues with currency. System currency is set to DKK, which I would have expected to be used by the plugin. When the sensors initially were created, they are in DKK - then after a restart or change of day (not sure which), the currency is changed to EUR. In my price data source, the currency is listed as:

image

and not as unit_of_measurement

dunxd commented 2 weeks ago

Did you try restarting the integration? This fixes it for me.

I think there is a race condition where on starting up HA, if there is no current charge value the currency used by the integration is set to the default, and the integration only checks when it is started.

bipsendk commented 2 weeks ago

Restarting the integration for the different devices after the system has started up solves the issue,

It would be nice if it would possible to put a "dependency" on, so that the integration starts after the device/entity providing the prices has started..

bipsendk commented 2 weeks ago

Not sure how it can be done - but an after_dependencies entry will have to be added during some sort of configuration - pointing to the "device" providing the entity with pricing info.

According to https://developers.home-assistant.io/docs/creating_integration_manifest/ it should be possible to do via a config entry ..

BTW: Just noticed that my entity with prices also have:

unit_of_measurement: DKK/kWh

Last option: Make it possible to specify default currency upon config of each device (if possible) - so you could set another default than EUR

bipsendk commented 2 weeks ago

Been thinking about it... I am not able to program it myself, but maybe a script, that calls homeassistant:reload_config_entry for all devices provided by dynamic_energy_cost integration - and then create an automation to call that script e,g. 3 minutes after HA startup ?

dunxd commented 2 weeks ago

It probably works, but it isn't really user friendly to require setting up an automation, and possibly requires an additional integration, so feels like a big hammer to crack a nut.

dunxd commented 2 weeks ago

Personally I prefer the option to specify the currency, which caters for price entities that don't include the currency so makes this integration more flexible.

martinarva commented 5 days ago

I see that the hardcoded currency is causing a lot of issues. Just a thought, but maybe we can use the home assitant default currency? So it's not tied to the price entity currency. Also it makses sense to use the same currency as the locale used in HA

Also i think that if the default currency is "none", it will work as expected. I'll try this

EDIT: worked perfectly and will be included in the next release. Commit: https://github.com/martinarva/dynamic_energy_cost/commit/e3ecf1aedc5f9cf7fb6487b605c973c960549995

martinarva commented 5 days ago

Closing this for now. Reopen if needed