Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to calculate charging cost / energy for charging process #44

Closed
ObJuanK opened this issue Aug 27, 2023 · 6 comments
Closed

Failed to calculate charging cost / energy for charging process #44

ObJuanK opened this issue Aug 27, 2023 · 6 comments

Comments

@ObJuanK
Copy link

ObJuanK commented Aug 27, 2023

Hello,
I have recently setup Teslamate Agile and am using Home Assistant to track import costs of provider.
My particular provider has a plan where my electricity price drops to $0 between the hours of 12pm and 2pm Saturday and Sunday.
There is no API integration with this provide, so I have Home assistant just running a number helper to automate the price change in import costs from the provider.
The helper triggers on schedule and changes the price of import costs, all other services in HA that depend on this cost appear to be representing their data correctly, so I assume I have set that up right.

I have seen this on a few charges and it has cleared itself automatically in the past, but this one is not clearing and has now been overtaken by a newer charge that was calculated.

Attached is the charge id in question
data-1693143085068.csv

info: Microsoft.Hosting.Lifetime[0]
      Content root path: /app/
info: TeslaMateAgile.PriceHelper[0]
      Looking for finished charging processes with no cost set started less than 6 day(s) ago in the 'Home' geofence (id: 1)
info: TeslaMateAgile.PriceHelper[0]
      Calculating cost for charges 08/24/2023 23:43:53 UTC - 08/25/2023 03:19:42 UTC
info: TeslaMateAgile.PriceHelper[0]
      Phase correction: 2 -> 3
fail: TeslaMateAgile.PriceHelper[0]
      Failed to calculate charging cost / energy for charging process 65
      System.Exception: Charge calculation failed, pricing calculated for 628 / 629, likely missing price data
         at TeslaMateAgile.PriceHelper.CalculateChargeCost(IEnumerable`1 charges) in /src/TeslaMateAgile/Helpers/PriceHelper.cs:line 138
         at TeslaMateAgile.PriceHelper.Update() in /src/TeslaMateAgile/Helpers/PriceHelper.cs:line 74
info: TeslaMateAgile.PriceService[0]
      Waiting 14400 seconds until next update

This keeps repeating in the logs. Sadly I recreated the container without saving the old logs, but I am now up to charge process 66 and that one calculated fine.

While reading through closed issues, I saw that it was mentioned that the phase correction was not needed and to be removed, I have done that recently but has not fixes my issue. Here is my compose file

  teslamateagile:
    image: mattjeanes/teslamateagile:latest
    restart: always
    environment:
      - DATABASE_USER=teslamate
      - DATABASE_PASS=secret
      - DATABASE_NAME=teslamate
      - DATABASE_HOST=database
      - TeslaMate__UpdateIntervalSeconds=14400
      - TeslaMate__GeofenceId=1
      - TeslaMate__EnergyProvider=HomeAssistant
      - TeslaMate__LookbackDays=6
      #- TeslaMate__Phases=3 not required https://github.com/MattJeanes/TeslaMateAgile/issues/17
      - HomeAssistant__BaseUrl=<IP>
      - HomeAssistant__AccessToken=<TOKEN>
      - HomeAssistant__EntityId=input_number.re_import_price

Bonus question: Since I only charge every other day, I don't need this process checking every 5 minutes if a charge has completed, so have increased the update interval a little to an arbitrary time (4 hours) so it would only run 6 times a day.
My price only changes for 2 hours in a day, most charging sessions last 4-5 hours and the free energy period will usually be in the middle of the 4-5 hour session. Would the calculations be able to account for that without checking for charge events every 5 minutes?

Thanks

@MattJeanes
Copy link
Owner

MattJeanes commented Aug 27, 2023

Hey, first of all many thanks for the detailed report - super appreciated!

Can I ask you to please run this call against the Home Assistant API and tell me the results?

GET <home-assistant-url>/api/history/period/2023-08-24T23:43:53Z?end_time=2023-08-25T03:19:42Z&filter_entity_id=input_number.re_import_price

Ensure you set the header Authorization: Bearer <home-assistant-token> as well!

What appears to be happening is when querying HA, the dates of the returned data don't line up with the charges in the CSV you sent me for some reason, as there is a matching step to designate all charges to a relevant price from the returned data. I suspect it may be a timezone related issue or perhaps something off by a second or minute etc.

For your bonus question, you can run it as infrequently as you like, but make sure your lookback days and HA history will work with what you set. In your particular scenario this would be fine because HA history would still be available which is where it gets the price data from, it doesn't really matter when it runs as long as the charge is within the lookback days and HA history is available for the date/time range 🙂

@ObJuanK
Copy link
Author

ObJuanK commented Aug 31, 2023

Sorry for the late reply, has been a busy few days and glad you liked the detail. I suffer from a lack of detail in reported problems at work every day so I know the struggle :)

Sure thing, here is the output of that get request

[[{"entity_id":"input_number.re_import_price","state":"0.2748","attributes":{"initial":null,"unit_of_measurement":"AUD/kWh","icon":"mdi:lightning-bolt","friendly_name":"red_energy_import_price"},"last_changed":"2023-08-24T23:43:53+00:00","last_updated":"2023-08-24T23:43:53+00:00"}]]

I suspected it may be time based thing, I do have my HA set to UTC+10 but I dont think I configured TMA to any timezone (not sure if its possible)

Thanks for the info on the bonus question, i'll back this off to run once a day then as I am only charging every other day. At first read of the doco it appeared that you may need to run this constantly to keep a running tally so I tried to space it out to the average of my charge sessions

@MattJeanes
Copy link
Owner

Thank you! It looks like there was just one price of 0.2748 AUD/kWh for the duration of that charge, does that sound correct?

I'll run it through the debugger when I'm back home next week, I'm on holiday at the moment 🙂

If there's a particular section of the docs which implies TeslaMateAgile needs to run often to track charging within a charge session let me know and I'll try and reword it to be be clearer (or you could submit a PR of course!)

@MattJeanes
Copy link
Owner

Hey @ObJuanK, I have now fixed this in v1.12.1 🙂

If you're interested, it was caused by the final price having the exact same time as the final charge entry, down to the millisecond. This combined with my usage of charge.Date < price.ValidTo instead of charge.Date <= price.ValidTo was the issue!

That charge you supplied worked out as 5.88 cost by the way in case the Home Assistant data has expired by now and you need to enter it manually.

Also I hope you don't mind, I have added your data to some unit tests to help prevent this issue re-occurring in future, but please let me know if this is an issue and you want me to remove it.

@ObJuanK
Copy link
Author

ObJuanK commented Sep 7, 2023

Hey Matt,

Thanks for looking into it and taking the time to reply on holidays.
Ive been on holidays myself but did not have the luxury of being anywhere with any form of reception, I guess thats a good thing 😄

I came home to find a myriad of failed calculations after my initial report, I have those logs if you are interested and can pull the data for those session if it helps you, just let me know.
But I pulled down v1.12.1 and when the container started it was able to calculate everything correctly as far back as HA could see, so looks like it did the trick! thank you 🙏
No issues with you using my data.

On the doco implying it needed to be run regularly: It was not anything specific and it could possibly be how I interpret things, but I inferred from context a little when reading the Required Environment Variables section.

  • The variable to check is in seconds, suggesting this can be configured to run near real time.
  • The example showing a default of 5 minutes,
  • The comment Check for completed charges without a set cost every x seconds to me, suggest you need to check for a completed charge regularly otherwise you'll miss it. Could be the every x seconds bit that did it for me.

What I think might help would be adding some subtext under Required Environment Variables with the explanation you provided me in your original reply.
Set the refresh value as in/frequent as you like. Ensure this does not exceed lookback days or HA data
I'd make a PR but I have not looked at how this works very much with other provides so can only offer my 2 cents on HA.

Thanks again and keep up the awesome work :)

@MattJeanes
Copy link
Owner

Thank you, glad to hear it's all working for you now, and thanks for letting me use your data in the tests.

I've made a little commit here to change the wording and increase the example to an hour, hopefully this will make the purpose of it clearer: 1ebe629

🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants