MattJeanes / TeslaMateAgile

Integration to automatically fill in prices for charge data captured by TeslaMate for smart energy providers
MIT License
76 stars 9 forks source link

Calculation fails when the end time of a charge is very close to the beginning of the hour #40

Closed kkthxbye-code closed 1 year ago

kkthxbye-code commented 1 year ago

Hi, awesome project, but ran into an issue using the Energinet provider. I had a charge that finished at the following timestamp: 2023-04-02 13:00:19.299.

When calculating it failed at the following line:

https://github.com/MattJeanes/TeslaMateAgile/blob/179ddd7c201abd178c0a1c79d42e24bb6149aa63/TeslaMateAgile/Helpers/PriceHelper.cs#LL127

With the error:

Charge calculation failed, pricing calculated for 364 / 365, likely missing price data

To fix it, I change the last timestamp of the charges table to 2023-04-02 13:01:19.299 via. psql, that is one minute later. I assume there's some issue related to charging finishing within the first minute of an hour. I didn't dig any deeper as the database modification solved my issue this time.

Let me know if any other information is needed.

MattJeanes commented 1 year ago

It looks like the Energinet provider is requesting data down to the minute precision level and not to the second which would explain the behaviour you are seeing. There is a window of +/- 2 hours already to account for timezones as we cannot specify the date to come back in UTC but it seems during CEST we're right on the edge of that which is why we're only seeing this now as Denmark only switched to CEST a week ago or so.

I've pushed image version v1.11.3 - would you please give it a go by setting the cost field to NULL for that charging process (and reset the time back to the original time if needed) and see if it works?

Cheers!

kkthxbye-code commented 1 year ago

Thank's for the quick fix, it seemed to work as intended!

dbug: TeslaMateAgile.PriceHelper[0]
      Calculated charge cost for 04/02/2023 11:00:00 UTC - 04/02/2023 12:00:00 UTC (unit cost: 0.94817500875, fee per kWh: 0): 10.572977343900205207444027852 for 11.15087114333333266884 energy
dbug: TeslaMateAgile.PriceHelper[0]
      Calculated charge cost for 04/02/2023 12:00:00 UTC - 04/02/2023 13:00:00 UTC (unit cost: 0.83773750125, fee per kWh: 0): 7.1852287972819619459826124506 for 8.57694538749999876048 energy
dbug: TeslaMateAgile.PriceHelper[0]
      Calculated charge cost for 04/02/2023 13:00:00 UTC - 04/02/2023 14:00:00 UTC (unit cost: 0.84313750, fee per kWh: 0): 0.00000000 for 0 energy

I assume the 0 energy on the last one is to be expected, the last charges event show 0 current.

Again, thank you for the awesome project!

MattJeanes commented 1 year ago

That's correct, yeah

No worries, glad to hear that's sorted it out! 🙂