martinarva / dynamic_energy_cost

35 stars 9 forks source link

Prepare for future 15 minute Spot prices #7

Closed jkkataja closed 2 months ago

jkkataja commented 2 months ago

The spot price market seems to be migrating at some point to 15 minute price intervals as opposed to 60 minute intervals

While it may take some years for this change to take full effect (e.g., in Finland customer import/export balance is on 15 minute level, price is still at 60 minute for now) it may be beneficial to prepare already at this stage the code so, that it can be easily adjusted based on market rules.

There shouldn’t be too much focus at this stage on this, but rather being mindful when coding not to cause issues in the future.

martinarva commented 2 months ago

Actually price change frequency does not matter.

Cost calculation is made every time the energy entity changes. It then calculates the difference between new reading and last reading multiples it with energy price and adds the value to cost.

So it will check the current price on every update. Caveat is that energy sensor must update frequently. Usually it does and if it does not then it would be impossible to calculate cost anyway.

Edge case can be when last known energy reading from the end of hour, let's say 00:59:30 and next time energy sensor updates one minute interval at 01:00:30. Then the cost will be calculated with the 01:00 price, but some of the energy was actually consumed with last hour price. But for let's say 10kw power, the half minute energy will be 0.08kwh, and cost with 0.2€/kwh (again edge case where hours difference is 0.2€/keh) is 0.016€, so not so much.

I went with this logic to mitigate the risk of losing data or doing wrong calculations when restart or what ever happens.

If the new value is lower (for example energy sensor resets) it will not do the calculation and just stores the energy value for the next energy sensor update.

This prevents cost decrease. Kind of similar logic as total_increase in HA.

Edit: and as of calculating cost from Power entity it's the same. Calculations being made every time power entity changes.

jkkataja commented 2 months ago

Sounds good, wanted to bring this up just in case earlier rather than later 😁

martinarva commented 2 months ago

15 minute Spot prices are covered in the logic