hacf-fr / renault-api

https://readthedocs.org/projects/renault-api/
MIT License
109 stars 38 forks source link

How to format start/stop date for charge history? #562

Open jumpjack opened 2 years ago

jumpjack commented 2 years ago

None of these commands work:

I always get:

regex._regex_core.error: bad escape \d at position 7

epenet commented 2 years ago

Works for me.

  Month    Number of charges  Total time charging      Errors
-------  -------------------  ---------------------  --------
 202203                    3  16:26:00                      0

You should run it with logs or with debug: renault-api --log charge history --from 2022-02 --to 2022-03 --period month renault-api --debug charge history --from 2022-02 --to 2022-03 --period month

jumpjack commented 2 years ago

How do I update to 0.1.10? pip install renault-api does not have any effect on version, so I just manually changed the kamereon key.

epenet commented 2 years ago

pip install --upgrade renault-api or pip install renault-api==0.1.10

jumpjack commented 2 years ago

Thanks, updated successfully, but still getting same error (and nothing into the log).

Full error:

C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\Scripts>renault-api --debug charge history --from 2022-02 --to 2022-03 --period month
WARNING:renault_api:Debug output enabled. Logs may contain personally identifiable information and account credentials! Be sure to sanitise these logs before sending them to a third party or posting them online.
Traceback (most recent call last):
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\Scripts\renault-api.exe\__main__.py", line 7, in <module>
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\click\core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\click\core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\click\core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\click\core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\click\core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\click\core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\click\decorators.py", line 38, in new_func
    return f(get_current_context().obj, *args, **kwargs)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\renault_api\cli\helpers.py", line 39, in wrapper
    asyncio.run(run_command(func, *args, **kwargs))
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\asyncio\base_events.py", line 641, in run_until_complete
    return future.result()
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\renault_api\cli\helpers.py", line 30, in run_command
    await func(*args, **kwargs)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\renault_api\cli\charge\history.py", line 81, in history
    parsed_start, parsed_end = helpers.parse_dates(start, end)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\renault_api\cli\helpers.py", line 142, in parse_dates
    parsed_start = dateparser.parse(start)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\dateparser\conf.py", line 92, in wrapper
    return f(*args, **kwargs)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\dateparser\__init__.py", line 61, in parse
    data = parser.get_date_data(date_string, date_formats)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\dateparser\date.py", line 428, in get_date_data
    parsed_date = _DateLocaleParser.parse(
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\dateparser\date.py", line 178, in parse
    return instance._parse()
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\dateparser\date.py", line 182, in _parse
    date_data = self._parsers[parser_name]()
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\dateparser\date.py", line 196, in _try_freshness_parser
    return freshness_date_parser.get_date_data(self._get_translated_date(), self._settings)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\dateparser\date.py", line 234, in _get_translated_date
    self._translated_date = self.locale.translate(
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\dateparser\languages\locale.py", line 131, in translate
    relative_translations = self._get_relative_translations(settings=settings)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\dateparser\languages\locale.py", line 158, in _get_relative_translations
    self._generate_relative_translations(normalize=True))
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\dateparser\languages\locale.py", line 172, in _generate_relative_translations
    pattern = DIGIT_GROUP_PATTERN.sub(r'?P<n>\d+', pattern)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\regex\regex.py", line 700, in _compile_replacement_helper
    is_group, items = _compile_replacement(source, pattern, is_unicode)
  File "C:\Users\_conc\AppData\Local\Programs\Python\Python310-32\lib\site-packages\regex\_regex_core.py", line 1736, in _compile_replacement
    raise error("bad escape \\%s" % ch, source.string, source.pos)
regex._regex_core.error: bad escape \d at position 7

Could it be a windows issue about date format? (windows 10, localized to Italy)

jumpjack commented 2 years ago

Looking at available branches I found this line:

image

But at some point while experimenting (I am very poor at python, so I am just typying&trying :-) ) I noticed that renault-api was still using dateparser 1.1.0, so I tried to apply your suggested "upgrade" command to dateparser:

pip install --upgrade dateparser

Now that I have dateparser 1.1.1 in stalled, script almost works (at least it does not crash).

So I think there is something wrong in dependencies while installing/upgrading the api.

It almost works because the output is wrong:

renault-api charge sessions --from 2020-01 --to 2022-03

Charge start         Charge end           Duration           Power (kW)    Started at    Finished at    Charge gained    Power level    Status
-------------------  -------------------  -----------------  ------------  ------------  -------------  ---------------  -------------  --------
2022-03-16 03:02:16  2022-03-16 05:01:58  4 days, 23:42:00                 62 %          62 %                                           error
2022-03-16 21:18:42  2022-03-16 22:50:08  3 days, 19:26:00                               60 %                                           error
2022-03-17 02:01:10  2022-03-17 06:02:01  10 days, 0:51:00                               74 %                                           error
2022-03-18 02:01:13  2022-03-18 06:02:04  10 days, 0:51:00                               64 %                                           error
2022-03-18 18:34:28  2022-03-18 18:37:57  3:29:00                                        55 %                                           error
2022-03-18 18:40:37  2022-03-18 19:17:22  1 day, 12:45:00                                89 %                                           error
2022-03-19 16:18:18  2022-03-19 17:36:04  3 days, 5:46:00                                99 %                                           error
2022-03-20 22:21:31  2022-03-21 07:32:50  22 days, 23:19:00                              71 %                                           error

Charge duration is not properly calculated: "chargeDuration" is in seconds, not in minutes.

Raw output I get by myself is:

{
    "id": "xxx MY VIN xxx",
    "charges": [
        {
            "chargeStartDate": "2022-03-16T02:02:16Z",
            "chargeEndDate": "2022-03-16T04:01:58Z",
            "chargeDuration": 7182,
            "chargeStartBatteryLevel": 62,
            "chargeEndBatteryLevel": 62,
            "chargeEndStatus": "error"
        },
        {
            "chargeStartDate": "2022-03-16T20:18:42Z",
            "chargeEndDate": "2022-03-16T21:50:08Z",
            "chargeDuration": 5486,
            "chargeEndBatteryLevel": 60,
            "chargeEndStatus": "error"
        },
        {
            "chargeStartDate": "2022-03-17T01:01:10Z",
            "chargeEndDate": "2022-03-17T05:02:01Z",
            "chargeDuration": 14451,
            "chargeEndBatteryLevel": 74,
            "chargeEndStatus": "error"
        },
        {
            "chargeStartDate": "2022-03-18T01:01:13Z",
            "chargeEndDate": "2022-03-18T05:02:04Z",
            "chargeDuration": 14451,
            "chargeEndBatteryLevel": 64,
            "chargeEndStatus": "error"
        },
        {
            "chargeStartDate": "2022-03-18T17:34:28Z",
            "chargeEndDate": "2022-03-18T17:37:57Z",
            "chargeDuration": 209,
            "chargeEndBatteryLevel": 55,
            "chargeEndStatus": "error"
        },
        {
            "chargeStartDate": "2022-03-18T17:40:37Z",
            "chargeEndDate": "2022-03-18T18:17:22Z",
            "chargeDuration": 2205,
            "chargeEndBatteryLevel": 89,
            "chargeEndStatus": "error"
        },
        {
            "chargeStartDate": "2022-03-19T15:18:18Z",
            "chargeEndDate": "2022-03-19T16:36:04Z",
            "chargeDuration": 4666,
            "chargeEndBatteryLevel": 99,
            "chargeEndStatus": "error"
        },
        {
            "chargeStartDate": "2022-03-20T21:21:31Z",
            "chargeEndDate": "2022-03-21T06:32:50Z",
            "chargeDuration": 33079,
            "chargeEndBatteryLevel": 71,
            "chargeEndStatus": "error"
        }
    ]
}
jumpjack commented 2 years ago

Maybe also an improvement is possible: final date not mandatory = show charges up to today.

epenet commented 2 years ago

dateparser issue => you are correct there is an issue with the sub-dependency: https://github.com/scrapinghub/dateparser/pull/1046

epenet commented 2 years ago

duration issue => it is fine on my Zoe40:

Charge start         Charge end           Duration    Power (kW)    Started at    Finished at    Charge gained    Power level    Status
-------------------  -------------------  ----------  ------------  ------------  -------------  ---------------  -------------  --------
2022-03-19 00:15:54  2022-03-19 09:40:29  9:24:00     3.00 kW       29 %          99 %           70 %             slow           ok
2022-03-18 22:02:28  2022-03-18 22:03:31  0:01:00     3.00 kW       29 %          29 %           0 %              slow           ok
2022-03-16 00:30:35  2022-03-16 07:30:46  7:00:00     2.90 kW       38 %          89 %           51 %             slow           ok

I don't know what vehicle you use, but I assume it's Renault changing the units between models :(

jumpjack commented 2 years ago

It's a Zoe50. Maybe the best solution is to manually calculate the difference between the dates.

epenet commented 2 years ago

Can you please send the full JSON from renault-api --debug charge sessions --from 2020-01 --to 2022-03? The json that you pasted doesn't seem to be in the correct format (missing data/type/attributes).

jumpjack commented 2 years ago

Sorry, my script cut something in the output; this is the output from your script (same data, just different JSON structure):

{
  "data": {
    "type": "Car",
    "id": "xxxxxxxxxxxxxxxx",
    "attributes": {
      "charges": [
        {
          "chargeStartDate": "2022-03-16T02:02:16Z",
          "chargeEndDate": "2022-03-16T04:01:58Z",
          "chargeDuration": 7182,
          "chargeStartBatteryLevel": 62,
          "chargeEndBatteryLevel": 62,
          "chargeEndStatus": "error"
        },
        {
          "chargeStartDate": "2022-03-16T20:18:42Z",
          "chargeEndDate": "2022-03-16T21:50:08Z",
          "chargeDuration": 5486,
          "chargeEndBatteryLevel": 60,
          "chargeEndStatus": "error"
        },
        {
          "chargeStartDate": "2022-03-17T01:01:10Z",
          "chargeEndDate": "2022-03-17T05:02:01Z",
          "chargeDuration": 14451,
          "chargeEndBatteryLevel": 74,
          "chargeEndStatus": "error"
        },
        {
          "chargeStartDate": "2022-03-18T01:01:13Z",
          "chargeEndDate": "2022-03-18T05:02:04Z",
          "chargeDuration": 14451,
          "chargeEndBatteryLevel": 64,
          "chargeEndStatus": "error"
        },
        {
          "chargeStartDate": "2022-03-18T17:34:28Z",
          "chargeEndDate": "2022-03-18T17:37:57Z",
          "chargeDuration": 209,
          "chargeEndBatteryLevel": 55,
          "chargeEndStatus": "error"
        },
        {
          "chargeStartDate": "2022-03-18T17:40:37Z",
          "chargeEndDate": "2022-03-18T18:17:22Z",
          "chargeDuration": 2205,
          "chargeEndBatteryLevel": 89,
          "chargeEndStatus": "error"
        },
        {
          "chargeStartDate": "2022-03-19T15:18:18Z",
          "chargeEndDate": "2022-03-19T16:36:04Z",
          "chargeDuration": 4666,
          "chargeEndBatteryLevel": 99,
          "chargeEndStatus": "error"
        },
        {
          "chargeStartDate": "2022-03-20T21:21:31Z",
          "chargeEndDate": "2022-03-21T06:32:50Z",
          "chargeDuration": 33079,
          "chargeEndBatteryLevel": 71,
          "chargeEndStatus": "error"
        }
      ]
    }
  }
}
jumpjack commented 2 years ago

It looks like charge-history returns duration in minutes while charges returns in seconds...

/charge-history:

        {
            "day": "20220317",
            "totalChargesNumber": 1,
            "totalChargesEnergyRecovered": 5.4,
            "totalChargesDuration": 241
        },

/charges:

        {
            "chargeStartDate": "2022-03-17T01:01:10Z",
            "chargeEndDate": "2022-03-17T05:02:01Z",
            "chargeDuration": 14451,
            "chargeEndBatteryLevel": 74,
            "chargeEndStatus": "error"
        },