FilipDem / Domoticz-BMW-plugin

Domoticz plugin working with BMW Connected Drive
4 stars 2 forks source link

Calculated Charging Time is wrong for electric vehicles #10

Closed RutgerBeyen closed 2 years ago

RutgerBeyen commented 2 years ago

There is an error in the datetime format used for calculating the remaining charging time (plugin.py line 384 till 387).

self.myVehicle.timestamp returns an offset naive datetime: 2022-05-05 17:59:34 self.myVehicle.fuel_and_battery.charging_end_time returns an offset-aware datetime: 2022-05-05 20:56:00+02:00

This results in the error "General error TaskHandler: can't subtract offset-naive and offset-aware datetimes" The plugin keeps running but the TaskHandler crashes, consequently no BMW values are updated anymore in Domoticz.

Solution would be to convert both timestamps to UTC: import pytz self.myVehicle.timestamp.replace(tzinfo=datetime.timezone.utc) => 2022-05-05 17:59:34+00:00 self.myVehicle.fuel_and_battery.charging_end_time.astimezone(pytz.utc) => 2022-05-05 18:56:00+00:00

Now the remaining charging time can be correctly calculated by subtracting self.myVehicle.timestamp from self.myVehicle.fuel_and_battery.charging_end_time. Not the other way around, as that results in a negative value: charging_time_remaining = round((self.myVehicle.fuel_and_battery.charging_end_time.astimezone(pytz.utc)-self.myVehicle.timestamp.replace(tzinfo=datetime.timezone.utc)).total_seconds()/60,2) =>56.43

If you wish I can create a PR.

FilipDem commented 2 years ago

You are completely correct! I made this function on request of somebody using the plugin. Due to the new bimmer_connected library based on Asyncio (still beta but I am using the geolocation already), there was some refactoring to do... And as I have no electric car, I could not test this part! Analysing your request and code reading seems this is right? Did you update your local version and do you confirm it is working? If yes I will then adapt it.

F

RutgerBeyen commented 2 years ago

Affirm, working perfect. Three charge cycles over the last 24h and it was all logged correctly. (Well, as soon as the car is fully charged, BMW doesn't report the 'charging_end_time' value anymore so the Domoticz counter remains at the last value it recorded at the previous poll interval. But that's how BMW works, it's not related to this plugin)