martinarva / dynamic_energy_cost

35 stars 9 forks source link

Negative power (feed in) is not subtracted from the daily/monthly/yearly costs #26

Open DutchKillerbee opened 1 month ago

DutchKillerbee commented 1 month ago

Describe the bug The daily/monthly/yearly utility sensors are not treating negative power values correctly.

I have a power meter (grid meter) in Watts connected to the dynamic energy cost integration and when the power is negative (feed in power to the grid) the sum is always 0. So for example the daily sensor doesn't go down but stays at a certain value. image the actual sensor looks like this image the yellow parts are negative and power feedin

Expected behavior I expect the daily cost sensor should go down in case of negative values (grid feed in) and should go up in case we consume power from grid.

Or do I miss something here?

gregbarreiro commented 1 month ago

This is not a bug. The P1 meter separates the consumed from the return energy into multiple sensors. The energy that is return ed to the grid is kept in another sensor. The sensor you are using is for the energy consumption from the grid. That's why it flats when you return energy to the grid. You can create a helper to keep track of the net consumption by simply subtracting the consumption from the return.

DutchKillerbee commented 1 month ago

@gregbarreiro Thanks for your reply.

This is the graph I use as input sensor for the dynamic energy cost integration (UoM = power)

image

Should this work?

DutchKillerbee commented 1 month ago

I am not a programmer and my python skills are very poor but I found this code: image Is the reason why negative costs are taken into account?

martinarva commented 1 month ago

This is expected behaviour. At least in our region when you buy electricity you pay electricity price plus tariffs and VAT and when you sell electricity you get only electricity price without VAT.

So the cost of electricity in this case is only the electricity you have bought not sold.

It has the same logic as HA Utility Meter integration default settings. In the Utility Meter integration you can choose net_consumption that will do what you are looking for. At this moment i'm not planning to add the functionality to my integration, but you can feed in Real Time cost entity from my integration to Utility Meter and use net_consumption=true to achieve what you are looking for.

DutchKillerbee commented 1 month ago

@martinarva Thanks for your feedback In our region we have dynamic prices per hour. So currently I get the same price for what I sell and buy (excluding vat). What I try to achieve is a daily sensor with my daily energy cost. So a sensor with the value simular to the "totaal net" cost in the energy dashboard image I thought it would be possible using your integration but if I understand you correct it is not possible

gregbarreiro commented 1 month ago

@gregbarreiro Thanks for your reply.

This is the graph I use as input sensor for the dynamic energy cost integration (UoM = power)

* Negative values = grid feed-in

* Positive values = grid use

image

Should this work?

@martinarva Thanks for your feedback In our region we have dynamic prices per hour. So currently I get the same price for what I sell and buy (excluding vat). What I try to achieve is a daily sensor with my daily energy cost. So a sensor with the value simular to the "totaal net" cost in the energy dashboard image I thought it would be possible using your integration but if I understand you correct it is not possible

Hoi,

The way you can achieve this is by creating a helper that calculates the net-consumption. image So basically a helper consumption - production and use that helper fr this integration. Groet, Greg

DutchKillerbee commented 1 month ago

@gregbarreiro Yes I understand that I need to build "these sensors" myself. I thought it could be possible using this integration..... I will, try to modify the code (to learn) otherwise I will create some helpers.

Pluimvee commented 1 month ago

@martinarva Different casus but strongly related: What happens when the price is negative? Then the cost will be negative and therefore be ignored by the current code. In some cases the marketprice is negative and you are paid for consuming energy (Yep: its a strange world).

martinarva commented 1 month ago

@Pluimvee , it's same in the region where Nordpool electicity prices as well. Just few days ago electricity price was negative.

However at least in our region cost of electricity includes Nordpool price and network tariffs. As the network tariffs are quite high there has not been a case where Nordpool price has been lower than tariffs, hence there is really not a negative total electric price.

But you are correct, calculations should take account this scenario where cost is negative while consumption is positive. I have to double check, but I'm quite sure it's covered.

DutchKillerbee commented 1 month ago

FYI By deactivating this part of the code and increase the accuracy I got a perfect total energy cost sensor. image image in sync with the total cost in the energy dashboard image

Pluimvee commented 1 month ago

@DutchKillerbee Thanks for this, I have made the same changes

@martinarva Wat is the reason you have coded this check on negative cost? Is there any drawback if we remove this? This month we had two situations in which the price was negative, even with taxes and network add-ons (may 1 and may 12). In these cases I still want to track the device energy cost and show that the device was actually making money.

martinarva commented 1 month ago

@DutchKillerbee Thanks for this, I have made the same changes

@martinarva Wat is the reason you have coded this check on negative cost? Is there any drawback if we remove this? This month we had two situations in which the price was negative, even with taxes and network add-ons (may 1 and may 12). In these cases I still want to track the device energy cost and show that the device was actually making money.

I think we have to solve it with a bit more complex way. Issue is that at least in our region when you buy electricity total price will be nordpool + tariff + VAT (export), but when you sell electricity its nordpoool wo. VAT (import).

Now at the moment only one price is given to the integration which in my case is total electricity price.

Cost can be negative in two cases:

  1. Because of electricity price is negative. In this case yes, when total electricity price (nordpool + tariffs + vat) is negative, this device is "earning money" and it's proper to use the total electricity price
  2. When the consumption is negative. In this case consumption should be multiplied with the export price (nordpool wo. VAT) to get the right "earning". For example using grid sensor here.

So to do this right and support all scenarios this should be done:

  1. User must insert both import and export price entities in configuration screen. If the import/export price is same, then user can fill the same enity in both fields
  2. There should be option in configuration screen for user to select if it would be Net consumption like in HA native Utility meter. (Enable if the source is a net meter, meaning it can both increase and decrease)
  3. Logic has to be rewritten to take account the two scenarios of negative price and negative consumption, so that integration calculates with the right electricity price (import or export)

It looks a bit overwhelming for me, so any contributions are welcome.

DutchKillerbee commented 1 month ago

Hello @martinarva

I understand the complexity. But let's try to takle this one. I would go for option 1. Just insert two prices. One import and one export.

Now let's try to define the logic.

As a start I could come up with this:

As a start the logic would be quite simple:

consumption = new_state.state (= current_cost variable in the existing code)

If consumption < 0:
  self._state =+ hours_passed * actual_export_price
else
  self._state =+ hours_passed * actual_import_price

Or do I miss something here?

Pluimvee commented 1 month ago

Please see comments and alternative solution on two price sensors in the pull request https://github.com/martinarva/dynamic_energy_cost/pull/31#issuecomment-2147092179

DutchKillerbee commented 4 weeks ago

@Pluimvee I noticed you changed this part of the code only in the energy sensor code image what was the reason for this? for me resetting daily/monthly power sensor doesn't always work. Could your fix also work for the power sensor? issue: https://github.com/martinarva/dynamic_energy_cost/issues/30