joeshaw / carwings

Go package and CLI tool for the Nissan Leaf Carwings API
MIT License
31 stars 11 forks source link

Monthly statistics show incorrect daily summary with unit being kWh/100km #19

Closed danielzoba closed 5 years ago

danielzoba commented 5 years ago

Example:

Trips on 2019-09-02 Monday 07:49 37.1 km 11.5 kWh/100km 4.3 kWh 17:32 36.9 km 13.5 kWh/100km 5.0 kWh ============ ============== ============ 74.0 km 0.1 kWh/100km 9.2 kWh

The total consumption per 100km is not correct. I think its this line in the code:

efficiency := (power / metersToUnits(cfg.units, distance)) / 1000

which is wrong in this case. Maybe it works if a different "ms.EfficiencyScale" is in effect. I have no idea how to handle this the correct way. Simply dividing by 10 instead of 1000 would fix it for me and probably break it for others.

Is there a more elegant way than to check the string content of "ms.EfficiencyScale"?

joeshaw commented 5 years ago

Unfortunately I also don't know how to handle this -- this feature has never worked for me. (I get empty results from the server whenever I made this request)

If you could attach the output from the API by running with -debug (be careful to scrub sensitive information) that might help me decode the API better.

Also cc @nigelm who most recently worked on this feature.

stelian42 commented 5 years ago

Interestingly, my "ms.EfficiencyScale" is "kWh/km". So the total is correct, but on the other side I found that the resolution of the display is too big: I locally modified the %5.1f into %7.3f for Efficiency in order to have something more readable.

We need to either parse EfficiencyScale or find the way to have a consistent unit but I have no idea where the kWh/km versus kWh/100km come from...

nigelm commented 5 years ago

I'll have a look at this, but unfortunately won't be for a few days due to other commitments.

It will also need to be at a time when I'm not getting errors back from the API due to Nissan having issues - ERROR: json: invalid use of ,string struct tag, trying to unmarshal "" into int

joeshaw commented 5 years ago

It will also need to be at a time when I'm not getting errors back from the API due to Nissan having issues - ERROR: json: invalid use of ,string struct tag, trying to unmarshal "" into int

Given my previous experience with this API, I'd say this is probably not an operational issue on the API side but rather just sloppy implementation. In other places we have to work around the lax types in this API (a side effect of it being written in PHP, and a downside to using Go for this sort of thing).

We might need to do the same thing here: defining that field as json.RawMessage and poking at it directly to see if it should be converted to an int.

danielzoba commented 5 years ago

Hi,

below is the relevant debug output you asked for.

In my fork, I simply applied the quick&dirty change to get proper scale.

Date: Fri, 13 Sep 2019 05:06:55 GMT Server: Apache Strict-Transport-Security: max-age=15768000

1e58 {"status":200,"PriceSimulatorDetailInfoResponsePersonalData":{"TargetMonth":"201909","TotalPowerConsumptTotal":"94.8715","TotalPowerConsumptMoter":"124.63493","TotalPowerConsumptMinus":"29.76343","ElectricPrice":"0.28","ElectricBill":"26.564020","ElectricCostScale":"kWh\/100km","MainRateFlg":"USER","ExistFlg":"EXIST","PriceSimulatorDetailInfoDateList":{"PriceSimulatorDetailInfoDate":[{"TargetDate":"2019-09-02","PriceSimulatorDetailInfoTripList":{"PriceSimulatorDetailInfoTrip":[{"TripId":"1","PowerConsumptTotal":"4251.52","PowerConsumptMoter":"5866.21","PowerConsumptMinus":"1614.69","TravelDistance":"37123","ElectricMileage":"11.5","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-02T05:49:53"},{"TripId":"2","PowerConsumptTotal":"4968.12","PowerConsumptMoter":"6294.25","PowerConsumptMinus":"1326.13","TravelDistance":"36923","ElectricMileage":"13.5","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-02T15:32:57"}]},"DisplayDate":"Sep 02"},{"TargetDate":"2019-09-03","PriceSimulatorDetailInfoTripList":{"PriceSimulatorDetailInfoTrip":[{"TripId":"1","PowerConsumptTotal":"4254.94","PowerConsumptMoter":"5875.74","PowerConsumptMinus":"1620.8","TravelDistance":"37137","ElectricMileage":"11.5","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-03T05:43:42"},{"TripId":"2","PowerConsumptTotal":"4792.7","PowerConsumptMoter":"6055.23","PowerConsumptMinus":"1262.53","TravelDistance":"36924","ElectricMileage":"13.0","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-03T15:32:33"}]},"DisplayDate":"Sep 03"},{"TargetDate":"2019-09-04","PriceSimulatorDetailInfoTripList":{"PriceSimulatorDetailInfoTrip":[{"TripId":"1","PowerConsumptTotal":"4233.84","PowerConsumptMoter":"6009.52","PowerConsumptMinus":"1775.68","TravelDistance":"37128","ElectricMileage":"11.4","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-04T05:38:58"},{"TripId":"2","PowerConsumptTotal":"4793.62","PowerConsumptMoter":"5947.05","PowerConsumptMinus":"1153.43","TravelDistance":"36910","ElectricMileage":"13.0","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-04T15:31:31"}]},"DisplayDate":"Sep 04"},{"TargetDate":"2019-09-05","PriceSimulatorDetailInfoTripList":{"PriceSimulatorDetailInfoTrip":[{"TripId":"1","PowerConsumptTotal":"4197.32","PowerConsumptMoter":"5826.43","PowerConsumptMinus":"1629.11","TravelDistance":"37112","ElectricMileage":"11.3","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-05T05:22:39"},{"TripId":"2","PowerConsumptTotal":"4736.06","PowerConsumptMoter":"5922.8","PowerConsumptMinus":"1186.74","TravelDistance":"36921","ElectricMileage":"12.8","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-05T15:32:47"}]},"DisplayDate":"Sep 05"},{"TargetDate":"2019-09-06","PriceSimulatorDetailInfoTripList":{"PriceSimulatorDetailInfoTrip":[{"TripId":"1","PowerConsumptTotal":"3956.66","PowerConsumptMoter":"5534.49","PowerConsumptMinus":"1577.83","TravelDistance":"37141","ElectricMileage":"10.7","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-06T05:52:01"},{"TripId":"2","PowerConsumptTotal":"4779.5","PowerConsumptMoter":"6007.02","PowerConsumptMinus":"1227.52","TravelDistance":"36939","ElectricMileage":"12.9","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-06T15:21:24"}]},"DisplayDate":"Sep 06"},{"TargetDate":"2019-09-07","PriceSimulatorDetailInfoTripList":{"PriceSimulatorDetailInfoTrip":[{"TripId":"1","PowerConsumptTotal":"2090.58","PowerConsumptMoter":"2945.04","PowerConsumptMinus":"854.46","TravelDistance":"19492","ElectricMileage":"10.7","CO2Reduction":"3","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-07T15:31:23"},{"TripId":"2","PowerConsumptTotal":"3080.98","PowerConsumptMoter":"3654.28","PowerConsumptMinus":"573.3","TravelDistance":"19153","ElectricMileage":"16.1","CO2Reduction":"3","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-07T20:12:18"}]},"DisplayDate":"Sep 07"},{"TargetDate":"2019-09-08","PriceSimulatorDetailInfoTripList":{"PriceSimulatorDetailInfoTrip":[{"TripId":"1","PowerConsumptTotal":"913.02","PowerConsumptMoter":"1755.97","PowerConsumptMinus":"842.95","TravelDistance":"8788","ElectricMileage":"10.4","CO2Reduction":"2","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-08T12:50:11"},{"TripId":"2","PowerConsumptTotal":"1269.14","PowerConsumptMoter":"1645.59","PowerConsumptMinus":"376.45","TravelDistance":"8465","ElectricMileage":"15.0","CO2Reduction":"1","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-08T19:09:54"}]},"DisplayDate":"Sep 08"},{"TargetDate":"2019-09-09","PriceSimulatorDetailInfoTripList":{"PriceSimulatorDetailInfoTrip":[{"TripId":"1","PowerConsumptTotal":"4392.98","PowerConsumptMoter":"5786.56","PowerConsumptMinus":"1393.58","TravelDistance":"37144","ElectricMileage":"11.8","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-09T05:45:28"},{"TripId":"2","PowerConsumptTotal":"5182.62","PowerConsumptMoter":"6358.46","PowerConsumptMinus":"1175.84","TravelDistance":"36958","ElectricMileage":"14.0","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-09T15:32:16"}]},"DisplayDate":"Sep 09"},{"TargetDate":"2019-09-10","PriceSimulatorDetailInfoTripList":{"PriceSimulatorDetailInfoTrip":[{"TripId":"1","PowerConsumptTotal":"4116.56","PowerConsumptMoter":"5713.16","PowerConsumptMinus":"1596.6","TravelDistance":"37140","ElectricMileage":"11.1","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-10T05:47:10"},{"TripId":"2","PowerConsumptTotal":"4914.4","PowerConsumptMoter":"6242.42","PowerConsumptMinus":"1328.02","TravelDistance":"37708","ElectricMileage":"13.0","CO2Reduction":"7","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-10T14:58:49"},{"TripId":"3","PowerConsumptTotal":"2251.22","PowerConsumptMoter":"3109.83","PowerConsumptMinus":"858.61","TravelDistance":"21017","ElectricMileage":"10.7","CO2Reduction":"4","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-10T16:49:08"},{"TripId":"4","PowerConsumptTotal":"3249.28","PowerConsumptMoter":"4037.96","PowerConsumptMinus":"788.68","TravelDistance":"20436","ElectricMileage":"15.9","CO2Reduction":"4","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-10T20:30:55"}]},"DisplayDate":"Sep 10"},{"TargetDate":"2019-09-11","PriceSimulatorDetailInfoTripList":{"PriceSimulatorDetailInfoTrip":[{"TripId":"1","PowerConsumptTotal":"4295.4","PowerConsumptMoter":"5843.52","PowerConsumptMinus":"1548.12","TravelDistance":"37138","ElectricMileage":"11.6","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-11T05:46:19"},{"TripId":"2","PowerConsumptTotal":"4787.88","PowerConsumptMoter":"6016.51","PowerConsumptMinus":"1228.63","TravelDistance":"36926","ElectricMileage":"13.0","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-11T15:30:34"}]},"DisplayDate":"Sep 11"},{"TargetDate":"2019-09-12","PriceSimulatorDetailInfoTripList":{"PriceSimulatorDetailInfoTrip":[{"TripId":"1","PowerConsumptTotal":"4444.0","PowerConsumptMoter":"5921.59","PowerConsumptMinus":"1477.59","TravelDistance":"37107","ElectricMileage":"12.0","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-12T05:15:02"},{"TripId":"2","PowerConsumptTotal":"4919.16","PowerConsumptMoter":"6265.3","PowerConsumptMinus":"1346.14","TravelDistance":"36926","ElectricMileage":"13.3","CO2Reduction":"6","MapDisplayFlg":"NONACTIVE","GpsDatetime":"2019-09-12T14:47:32"}]},"DisplayDate":"Sep 12"}]},"PriceSimulatorTotalInfo":{"TotalNumberOfTrips":"24","TotalPowerConsumptTotal":"94.8715","TotalPowerConsumptMoter":"124.63493","TotalPowerConsumptMinus":"29.76343","TotalTravelDistance":"764656","TotalElectricMileage":"0.0124","TotalCO2Reductiont":"131"},"DisplayMonth":"Sep\/2019"}} 0

nigelm commented 5 years ago

So the ElectricCostScale (which becomes the EfficiencyScale) can have at least three different values:-

I guess we need to change the efficiency calculation dependant on that (which can be done by having a branch for the last option, and the existing calculation for the other two and assume for now any other weird units (post Brexit it seems likely the UK will change to Horsepower-hours/furlong) follow the original calculation.

Do we need an enum for that field (sorry my go terminology is likely a bit off here - its not one of my better languages).

stelian42 commented 5 years ago

It would also be nice to have a config option for choosing the preferred format: I for one would like to have the results in kWh/100km, just like the car dashboard shows...

stelian42 commented 5 years ago

I've implemented the config option in #21 , I'm not sure if this also fixes the OP issue, please check.

stelian42 commented 5 years ago

I've added a commit to #21 which I believe it fixes the OP issue.

joeshaw commented 5 years ago

Fixed in #21