bimmerconnected / bimmer_connected

🚘 Library to query the status of your BMW or Mini from the ConnectedDrive portal
Apache License 2.0
362 stars 79 forks source link

sensor.cooper_s_e_all4_charging_time_remaining is "unavailbe" , but sensor.cooper_s_e_all4_charging_end_time shows correctly #417

Closed licheng5625 closed 2 years ago

licheng5625 commented 2 years ago

Describe the issue

Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 517, in async_update_ha_state self._async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 563, in _async_write_ha_state state = self._stringify_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 536, in _stringify_state if (state := self.state) is None: File "/usr/src/homeassistant/homeassistant/components/sensor/init.py", line 371, in state value = self.native_value File "/usr/src/homeassistant/homeassistant/components/bmw_connected_drive/sensor.py", line 182, in native_value return cast(StateType, self.entity_description.value(state, self.hass)) File "/usr/src/homeassistant/homeassistant/components/bmw_connected_drive/sensor.py", line 100, in hass.config.units.length(x[0], UNIT_MAP.get(x[1], x[1])), 2 File "/usr/src/homeassistant/homeassistant/util/unit_system.py", line 134, in length raise TypeError(f"{length!s} is not a numeric value.") TypeError: None is not a numeric value.

Expected behavior

sensor.cooper_s_e_all4_charging_time_remaining shows

          "barType": null,
          "chargingStatusIndicatorType": "CHARGING",
          "chargingStatusType": "CHARGING",
          "chargingType": "charging",
          "iconOpacity": "high",
          "infoIconId": 59689,
          "infoLabel": "at ~08:23 PM",
          "isCircleIcon": true,
          "isInaccurate": true,
          "levelIconId": 59689,
          "levelUnits": "%",
          "levelValue": "100",
          "mainBarValue": 100,
          "rangeIconId": 59683,
          "rangeUnits": "km",
          "rangeValue": "36",
          "secondaryBarValue": 0,
          "showBarGoal": false,
          "showsBar": true

Which Home Assistant version are you using?

core-2022.2

What was the last working version of Home Assistant Core?

No response

What is your region?

Rest of world

ConnectedDrive website

Number of cars

Output of bimmer_connected fingerprint

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

rikroe commented 2 years ago

charging_time_remaining was removed in favor of charging_end_time as stated in the breaking changes of HA 2022.2.

Using an end time is more flexible and won't clutter the HA state machine. It will show a text in your Lovelace dashboard, but you will need to adjust your automations.

rhoddan commented 2 years ago

I also get "Unknown" when using sensor.330e_xdrive_charging_end_time I tried to create a template to solve this but it doesn't work for me with the new ..."_end_time" sensor

rikroe commented 2 years ago

But charging_end_time does give you a result, right?

I'm not sure if the device_class: timestamp breaks your template, as it should allow only datetime values (which "Not charging" is not). Please try removing it.

If you want to format the endtime to a date string, you could use one of the two options:

{{ states('sensor.330e_xdrive_charging_end_time') | as_datetime | as_local }}  # e.g. 2022-02-05 01:40:37+01:00
{{ states('sensor.330e_xdrive_charging_end_time') | as_timestamp | timestamp_custom('%H:%M') }}  # e.g. 01:40

If you need the remaining time (like the old sensor did), you could use the following:


{{ (states('sensor.330e_xdrive_charging_end_time') | as_datetime) - now() }} # e.g. 0:57:35.389144
{{ ((states('sensor.330e_xdrive_charging_end_time') | as_datetime) - now()).total_seconds() / 3600 }} # e.g. 0.9560625547222222 (hours)```
rhoddan commented 2 years ago

Thanks! I want to create a template where it shows "charging time left" and instead of the unknown shows a text like "Not charging"

I tried your proposals but get error messages:

{{ (states('sensor.330e_xdrive_charging_end_time') | as_datetime) - now }} gives TypeError: unsupported operand type(s) for -: 'NoneType' and 'function'

{{ ((states('sensor.330e_xdrive_charging_end_time') | as_datetime) - now).total_seconds() / 3600 }} also gives TypeError: unsupported operand type(s) for -: 'NoneType' and 'function'

Thanks in advance for all support!

rikroe commented 2 years ago

Ah shoot, please use now() instead of now.

Also, those will not work if your charging_end_time is unknown (with states('sensor.330e_xdrive_charging_end_time') | as_datetime evaluating to None). So you will need to keep your if/else statement around it.

With the new iif filter you might be able to do something like this (haven't tried it yet, no time to upgrade):


{{ iif(is_state('sensor.330e_xdrive_charging_end_time', 'unknown'), 'Not charging', states('sensor.330e_xdrive_charging_end_time') | as_datetime | timestamp_custom('%Y-%m-%d %H:%M')) }}
rhoddan commented 2 years ago

Now it's working! Also the new "iif" works fine :-)

github-actions[bot] commented 2 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.