BMW ConnectedDrive component

@gerard33

Thanks for the script, heres an output from my BMW i3.

C:\bmwcd>bmwcdapi.py -p
...running bmwcdapi.py
--------------START CAR DATA--------------
DCS_CCH_Activation: None
DCS_CCH_Ongoing : None
Segment_LastTrip_ratio_electric_driven_distance: 100
Segment_LastTrip_time_segment_end: 26.01.2018 16:46:00 UTC
Segment_LastTrip_time_segment_end_formatted: 26.01.2018 16:46
Segment_LastTrip_time_segment_end_formatted_date: 26.01.2018
Segment_LastTrip_time_segment_end_formatted_time: 16:46
battery_size_max: 35820
beMaxRangeElectric: 170.0
beMaxRangeElectricKm: 170.0
beMaxRangeElectricMile: 105.0
beRemainingRangeElectric: 168.0
beRemainingRangeElectricKm: 168.0
beRemainingRangeElectricMile: 104.0
beRemainingRangeFuel: 0.0
beRemainingRangeFuelKm: 0.0
beRemainingRangeFuelMile: 0.0
chargeNowAllowed: NOT_ALLOWED
chargingHVStatus: FINISHED_FULLY_CHARGED
chargingLevelHv: 100.0
chargingLogicCurrentlyActive: NOT_CHARGING
chargingSystemStatus: CHARGINGENDED
charging_connection_type: CONDUCTIVE
charging_inductive_positioning: not_positioned
charging_status: CHARGINGENDED
check_control_messages:
condition_based_services: 00003,OK,2019-04,;00017,OK,2019-04,
connectorStatus: CONNECTED
door_driver_front: CLOSED
door_driver_rear: CLOSED
door_lock_state: SECURED
door_passenger_front: CLOSED
door_passenger_rear: CLOSED
gps_lat: 60.000000
gps_lng: 10.500000
head_unit: NBT
head_unit_pu_software: 07/14
heading: 166
hood_state: CLOSED
kombi_current_remaining_range_fuel: 0.0
lastChargingEndReason: CHARGING_GOAL_REACHED
lastChargingEndResult: SUCCESS
lastUpdateReason: VEHCSHUTDOWN_SECURED
lights_parking: OFF
lsc_trigger: VEHCSHUTDOWN_SECURED
mileage: 7165
prognosisWhileChargingStatus: NOT_NEEDED
remaining_fuel: 0
shdStatusUnified: CLOSED
single_immediate_charging: isUnused
soc_hv_percent: 96.4
sunroof_position: 0
sunroof_state: CLOSED
trunk_state: CLOSED
unitOfCombustionConsumption: l/100km
unitOfElectricConsumption: kWh/100km
unitOfEnergy: kWh
unitOfLength: km
updateTime: 26.01.2018 21:53:12 UTC
updateTime_converted: 26.01.2018 22:53
updateTime_converted_date: 26.01.2018
updateTime_converted_time: 22:53
updateTime_converted_timestamp: 1517007192000
vehicle_tracking: 1
window_driver_front: CLOSED
window_driver_rear: CLOSED
window_passenger_front: CLOSED
window_passenger_rear: CLOSED
--------------END CAR DATA--------------
--------------START CAR NAV--------------
auxPowerEcoPro: 1.2
auxPowerEcoProPlus: 0.4
auxPowerRegular: 1.4
driveTrain: bev_ohne_rex
eco: 6b1,4c8,427,427,453,4ce,5f7,68f,728,859,98b
ecoEv: 6b1,4c8,427,427,453,4ce,5f7,68f,728,859,98b
isoCountryCode: NOR
kAccEco: 2520000
kAccReg: 2412000
kDecEco: 3240000
kDecReg: 3240000
kDown: 3384000
kUp: 3132000
latitude: 60.000000
longitude: 10.500000
norm: 6f0,4f5,429,429,453,4cf,5fb,694,72d,85f,991
normEv: 6f0,4f5,429,429,453,4cf,5fb,694,72d,85f,991
pendingUpdate: False
soc: 29.70800018310547
socMax: 29.699997
vehicleMass: 1260
vehicleTracking: True
--------------END CAR NAV--------------
--------------START CAR EFFICIENCY--------------
characteristicList: [{'characteristic': 'TOTAL_CONSUMPTION', 'quantity': 0}, {'characteristic': 'AUXILIARY_CONSUMPTION', 'quantity': 0}, {'characteristic': 'DRIVING_MODE', 'quantity': 0}, {'characteristic': 'ACCELERATION', 'quantity': 2}, {'characteristic': 'ANTICIPATION', 'quantity': 2}]
communitySwitch: True
efficiencyQuotient: 20
lastTripList: [{'name': 'LASTTRIP_DELTA_KM', 'unit': 'KM', 'lastTrip': '3.0'}, {'name': 'ACTUAL_DISTANCE_WITHOUT_CHARGING', 'unit': 'KM', 'lastTrip': '--'}, {'name': 'AVERAGE_ELECTRIC_CONSUMPTION', 'unit': 'KWH_PER_100KM', 'lastTrip': '25.7'}, {'name': 'AVERAGE_RECUPERATED_ENERGY_PER_100_KM', 'unit': 'KWH_PER_100KM', 'lastTrip': '5.0'}, {'name': 'CUMULATED_ELECTRIC_DRIVEN_DISTANCE', 'unit': 'KM', 'lastTrip': '7147.0'}]
lifeTimeList: []
modelType: I3
scoreList: [{'attrName': 'AVERAGE_ELECTRIC_CONSUMPTION', 'attrUnit': 'KWH_PER_100KM', 'minValue': 9.5, 'averageValue': 16.5, 'maxValue': 25.6, 'lifeTime': 16.3}, {'attrName': 'AVERAGE_RECUPERATED_ENERGY_PER_100_KM', 'attrUnit': 'KWH_PER_100KM', 'minValue': 0.3, 'averageValue': 3.1, 'maxValue': 10.6, 'lifeTime': 1.8}, {'attrName': 'CUMULATED_ELECTRIC_DRIVEN_DISTANCE', 'attrUnit': 'KM', 'minValue': 22.0, 'averageValue': 14845.3, 'maxValue': 97810.0, 'lifeTime': 7147.0}, {'attrName': 'LONGEST_DISTANCE_WITHOUT_CHARGING', 'attrUnit': 'KM', 'minValue': 0.0, 'averageValue': 163.4, 'maxValue': 270.0, 'lifeTime': 152.0}]
--------------END CAR EFFICIENCY--------------
--------------START CAR SERVICEPARTNER--------------
businessPartnerId: 00212581
businessPhone: +47 11 111111
city: Oslo
country: NO
countryIsoCode: NO
dealerName: Nameofdealer AS
email: [email protected]
lastChangeDate: 2017-04-09T09:04:58.000+02
partnerNumber: 43948
portfolioCodes: B1,BI,MI
portfolioNames: BMW Cars,BMW i,MINI
street: Nameofstreet 2
subsidiaryNumber: 1
url: http://nameofcardealer.bmw.no/no/nameofcardealer/no/index.html
zipCode: 
--------------END CAR SERVICEPARTNER--------------

GPS position, states of doors, charging status, climate control is on my wish list.

Model: i3 (REX)
E-Code: I01
Production Code: 1Z41
Manufactured: 2017

Keep up the good work and let me know if a novice like myself can help out :slight_smile:

@Svellem, thanks for the output of your i3. This will help me to put the specific values for an electric car in the code :slight_smile:
Can you help me a bit more by sending some screenshots from your BMW ConnectedDrive app with the relevant values and another output of the script which contains the same data? That makes mapping the data from the API a bit easier :relieved:.

Yes that’s possible. Place the script somewhere in the config dir and SSH into hass.io (there is an addon for that). Then you can start the script.
And you can of course also run it from a Windows PC with Python.

No problem, there is already a post with the output of an i3 in the meanwhile. The script isn’t changed for that part as I think it will not work without the customer.bmwgroup.com part. I have added a link in the first post which points to the instructions.

Will try to share a version which can be used as custom component asap, but currently it’s still work in progress.

See my comments above, it is currently not available as a custom component yet. You can try to run the script (see first post for instructions) to see if you get data. If that’s the case then this component will work for your car.

I will add interaction with the car is well :sunglasses:
The following actions are possible from the API: lock doors, unlock doors, light signal, sound horn, climate.
The locking and unlocking of the doors is something that I already tried, but now to add it to Home Assistant as a lock. The other actions I will add as a switch.

Latest screenshots including lock and switches :smiley:

screenshot_8

Your latest HA screenshots looks promising, much better than the BMW app :slight_smile:

This is how the “BMW i Remote” app looks like:
BMW i Remote" app screenshots

This script doesn’t seem to work for Canadian cars.

We log in at https://www.bmw.ca/en/ssl/ConnectedDrive.html,

and the script just shows

...running bmwcdapi.py
Traceback (most recent call last):
  File "./bmwcdapi.py", line 454, in <module>
    main()
  File "./bmwcdapi.py", line 423, in main
    c = ConnectedDrive()
  File "./bmwcdapi.py", line 113, in __init__
    self.get_car_data()
  File "./bmwcdapi.py", line 209, in get_car_data
    self.token_valid()  # Check if current token is still valid
  File "./bmwcdapi.py", line 150, in token_valid
    self.generate_credentials()
  File "./bmwcdapi.py", line 184, in generate_credentials
    tokenType = m.group(2)
AttributeError: 'NoneType' object has no attribute 'group'

In Canada, we don’t seem to have a www.bmw-connecteddrive.ca or whatever to login. customer.bmwgroup doesn’t work either.

Hello, is there an URL for Mini. I supposed “Mini Connected” is quite the same as “Bmw Connected” ?

1 Like

Here’s the output from a 2017 225xe hybrid

--------------START CAR DATA--------------
DCS_CCH_Activation: None
DCS_CCH_Ongoing : None
Segment_LastTrip_ratio_electric_driven_distance: 17
Segment_LastTrip_time_segment_end: 29.01.2018 14:23:00 UTC
Segment_LastTrip_time_segment_end_formatted: 29.01.2018 14:23
Segment_LastTrip_time_segment_end_formatted_date: 29.01.2018
Segment_LastTrip_time_segment_end_formatted_time: 14:23
battery_size_max: 7700
beMaxRangeElectric: 32.0
beMaxRangeElectricKm: 32.0
beMaxRangeElectricMile: 20.0
beRemainingRangeElectric: 12.0
beRemainingRangeElectricKm: 12.0
beRemainingRangeElectricMile: 7.0
beRemainingRangeFuel: 546.0
beRemainingRangeFuelKm: 546.0
beRemainingRangeFuelMile: 339.0
chargeNowAllowed: NOT_ALLOWED
chargingHVStatus: CHARGING
chargingLevelHv: 47.0
chargingLogicCurrentlyActive: DIRECT_CHARGING
chargingSystemStatus: CHARGINGACTIVE
chargingTimeRemaining: 96.0
charging_connection_type: CONDUCTIVE
charging_status: CHARGINGACTIVE
check_control_messages: 
condition_based_services: 00001,OK,2018-11,23000;00003,OK,2019-11,;00100,OK,2020-11,60000
connectorStatus: CONNECTED
door_driver_front: CLOSED
door_driver_rear: CLOSED
door_lock_state: SECURED
door_passenger_front: CLOSED
door_passenger_rear: CLOSED
gps_lat: x
gps_lng: x
head_unit: EntryNav
head_unit_pu_software: 11/15
heading: 18
hood_state: CLOSED
kombi_current_remaining_range_fuel: 534.0
lastChargingEndReason: UNKNOWN
lastChargingEndResult: UNKNOWN
lastUpdateReason: CHARGINGSTARTED
lights_parking: OFF
lsc_trigger: CHARGINGSTARTED
mileage: 11190
prognosisWhileChargingStatus: IS_PERFORMED
remaining_fuel: 36
single_immediate_charging: isUnused
soc_hv_percent: 32.2
trunk_state: CLOSED
unitOfCombustionConsumption: l/100km
unitOfElectricConsumption: kWh/100km
unitOfEnergy: kWh
unitOfLength: km
updateTime: 29.01.2018 14:29:06 UTC
updateTime_converted: 29.01.2018 15:29
updateTime_converted_date: 29.01.2018
updateTime_converted_time: 15:29
updateTime_converted_timestamp: 1517239746000
vehicle_tracking: 1
window_driver_front: CLOSED
window_driver_rear: CLOSED
window_passenger_front: CLOSED
window_passenger_rear: CLOSED
--------------END CAR DATA--------------
BMW 225xe: current credentials from BMW Connected Drive API still valid (token: x expires at: 2018-01-29 17:29:06 UTC)
--------------START CAR NAV--------------
auxPowerEcoPro: 1.2
auxPowerEcoProPlus: 0.4
auxPowerRegular: 1.4
isoCountryCode: SWE
latitude: x
longitude: x
pendingUpdate: False
soc: 2.4831900596618652
socMax: 5.44
vehicleTracking: True
--------------END CAR NAV--------------
BMW 225xe: current credentials from BMW Connected Drive API still valid (token: x expires at: 2018-01-29 17:29:06 UTC)
--------------START CAR EFFICIENCY--------------
characteristicList: [{'characteristic': 'CONSUMPTION', 'quantity': 0}, {'characteristic': 'DRIVING_MODE', 'quantity': 2}, {'characteristic': 'CHARGING_BEHAVIOUR', 'quantity': 1}, {'characteristic': 'ELECTRIC_DRIVING', 'quantity': 1}]
efficiencyQuotient: 23
lastTripList: [{'unit': 'KM', 'name': 'LASTTRIP_DELTA_KM', 'lastTrip': '3.0'}, {'unit': 'MINUTES', 'name': 'LASTTRIP_DELTA_TIME', 'lastTrip': '5.0'}, {'unit': 'L_PER_100KM', 'name': 'COMBINED_AVG_CONSUMED_LITERS_OVERALL', 'lastTrip': '3.3'}, {'name': 'LASTTRIP_TIME_SEGMENT_END', 'lastTrip': '29.01.2018 14:23'}, {'unit': 'PERCENTAGE', 'name': 'LASTTRIP_RATIO_ELECTRIC_DRIVEN_DISTANCE', 'lastTrip': '17'}]
lifeTimeList: [{'unit': 'KM', 'name': 'CUMULATED_ELECTRIC_DRIVEN_DISTANCE', 'lifeTime': '4628.2'}, {'unit': 'LITER', 'name': 'SAVED_LITERS_OVERALL', 'lifeTime': '363.0'}, {'name': 'TIMESTAMP_STATISTICS_RESET', 'lifeTime': '29.01.2018'}]
modelType: PHEV
--------------END CAR EFFICIENCY--------------
BMW 225xe: current credentials from BMW Connected Drive API still valid (token: x expires at: 2018-01-29 17:29:06 UTC)
--------------START CAR SERVICEPARTNER--------------
removed
1 Like

Great work!
I am running Hass on Docker. How do I get “requests” to work?
I want this! Gerard, I promise you get a ride if you come to Sweden.

1 Like

@Trebor5 you could just run sudo pip install requests on your server where Docker is running.
Check this post for some more details BMW ConnectedDrive component.

Going to fill my car with petrol now for a ride to Sweden :wink:

Can you try to enter this url in the script?
b2vapi.bmwgroup.us
Not sure if that works, but it’s mentioned here.

CC @JesseWebDotCom

Some more screenshots.

To do:

  • Check if car is electric, hybrid or fuel and show the relevant items
  • Add items for electric and hybrid car in the scripts
  • Checks and cleanup of the code
  • Cleanup the API script and put it on Pypi

When that’s ready I will post the custom component here for some more testing and then I can go for my first HA PR :sunglasses:

Untitled-1

Untitled-2

Untitled-3

3 Likes

Great work!

Just one minor note: Does the API work while your driving ?

Asking since the Mobile App is disabled during drive…

Nope, it doesn’t work when driving because there are no updates available from the API then.

1 Like

Okay, so now I have the file bmwcdapi.py in my config folder. I use terminal in Mac and typ ssh root@hassio, type my credentials and then try to run:
core-ssh:~# bmwcdapi.py -p
-bash: bmwcdapi.py: command not found

Also tried
./bmwcdapi.py -p
-bash: ./bmwcdapi.py: No such file or directory

What am i missing?

Did you go to the config dir before starting the script?
cd /config

core-ssh:~# cd /config
core-ssh:/config# bmwcdapi.py -p
-bash: bmwcdapi.py: command not found
core-ssh:/config# ./bmwcdapi.py -p
env: can’t execute ‘python3’: No such file or directory
core-ssh:/config# python bmwcdapi.py
-bash: python: command not found

I tried that, and I get

...running bmwcdapi.py
Traceback (most recent call last):
  File "./bmwcdapi.py", line 467, in <module>
main()
  File "./bmwcdapi.py", line 436, in main
c = ConnectedDrive()
  File "./bmwcdapi.py", line 116, in __init__
self.generate_credentials()
  File "./bmwcdapi.py", line 192, in generate_credentials
tokenType = m.group(2)
AttributeError: 'NoneType' object has no attribute 'group'

Can you comment out that line 192 and try it again?
That part of the code is not used anyway.