Connect to new BMW CarData service through MQTT

Did you configure your telegram items that contain the reply ID and last message text?

Yes, I double-checked and when I send messages from my phone to the bot they appear in the Last Message item. I also tried deleting the client on the BMW portal and creating a new one with a new Client ID. Still nothing. I guess I’m missing something on my system, compared to yours.

So you set your Client_ID and then you don“t receive a Telegram message?

Could you please open the log viewer and filter for bmw.
When you change or update your Client_ID item, you should see this message:
Starting BMW Device Code Flow for Client ID:

I’m not used to working with file based rules and might be doing something silly here. So before i try again, let’s make sure files are in the right place.

I assume i should have:
$OPENHAB_CONF/automation/js/BMW_Token_Setup.js
$OPENHAB_CONF/automation/js/BMW_ID_Token_Updates.js
$OPENHAB_CONF/items/carBMW.items

Correct?

Yes looks good.
Does you log say something like ā€˜Rule loaded: BMW Token Setup - Step 1 …’?

Do you have the Javascript Scripting installed?
Itā€˜s an option of openHABian and includes openhab-js.

Javascript Scripting is installed.
Steps taken and log output:

Created $OPENHAB_CONF/items/carBMW.items

22:12:41.784 \[INFO \] \[del.core.internal.ModelRepositoryImpl\] - Loading model ā€˜carBMW.items’

Created $OPENHAB_CONF/automation/js/BMW_Token_Setup.js
[

INFO \] \[port.loader.AbstractScriptFileWatcher\] - (Re-)Loading script ā€˜/etc/openhab/automation/js/BMW_Token_Setup.js’
\[INFO \] \[g.openhab.automation.openhab-js.rules\] - Adding rule: BMW Token Setup - Step 1: Device Code Request
\[INFO \] \[g.openhab.automation.openhab-js.rules\] - Adding rule: BMW Token Setup - Step 1.5: Handle Telegram Reply
\[INFO \] \[g.openhab.automation.openhab-js.rules\] - Adding rule: BMW Token Setup - Step 2: Token Request

At this point i do for 1.2 openHAB Telegram in the instructions step 1,2,3 and put the Client_ID into the openHAB item carBMW_Client_ID

\[INFO \] \[del.core.internal.ModelRepositoryImpl\] - Loading model ā€˜carBMW.items’

Then i would expect things starting to happen but nothing does. I don’t see the message

Starting BMW Device Code Flow for Client ID:

But you see logs that your carBMW_Client_ID was updated/changed to the new value?

The trigger listens to an update to the item.
triggers.ItemStateUpdateTrigger(clientIDItem)

When i change the Client_ID, i see in the log ā€œLoading model ā€˜carBMW.itemsā€™ā€, but not that the carBMW_Client_ID was updated/changed to the new value explicitly. The value in the item changes, though.

I“m not sure what you“re doing
The carBMW.items file shouldn“t be reloaded when you change the state of carBMW_Client_ID.
Just to make sure, how do you set your Client_ID?

There are different ways to set the state and i don“t know which one you“re using.
You could use the API Explorer inside the Main UI.

  1. Open http://<openHAB>:8080/developer/api-explorer
  2. On a second tab open http://<opeNHAB>:8080/developer/log-viewer
    replace <openHAB> with the hostname or ip-address of your oH instance
  3. Open Items and search for PUT - /items/{itemname}/state and open it.
  4. Click on Try it out, enter carBMW_Client_ID as itemname and your Client_ID as Request body.
  5. Click on execute
  6. Open the log viewer tab and filter for bmw

Do you see an entry like openhab.event.ItemStateUpdatedEvent Item 'carBMW_Client_ID' updated to 1234567890?

I had been editing carBMW.items directly with vi over an ssh connection. Told you i might be doing something silly :wink:

All works now, i have a stable connection with the BMW broker and have watched the token being refreshed a couple of times. Now it’s just a matter of configuring channels/topics.

There’s quite some logging going on though, but i guess i could just comment out the relevant lines in BMW_Token_Refresh.js for that.

Thanks a million, finally there’s a fire-and-forget solution for communicating with the bimmer now! :slight_smile:

1 Like

Perfect!

For the logging, just change all references of logger.info to logger.debug

1 Like

@Bredmich Hi, today I noticed that problem with refreshing token begin. I have such line in the logs:

Full JSON Response: {ā€œerrorā€: ā€œinvalid_requestā€, ā€œerror_descriptionā€: ā€œThe request is missing a required parameter, includes an unsupported parameter value (other than grant type), repeats a parameter, includes multiple credentials, utilizes more than one mechanism for authenticating the client, or is otherwise malformedā€}

When i had these errors it was coming from the BMW backend.
You can try to setup a new Token but youā€˜ll probably receive the same error for a few hours.

My tokens has been refreshed for three weeks now.

Did you stop and start the openhab service in these three weeks? This may be coincidential: after restarting the openhab service tonight, the bmw broker first came online without problems. But when the tokens needed to be refreshed after half an hour, i got this:

22:06:00.956 \[INFO \] \[tomation.openhab-js.BMW_Token_Refresh\] - Token needs to be updated! → Starting update
22:06:00.991 \[ERROR\] \[tomation.openhab-js.BMW_Token_Refresh\] - Exception during token refresh: TypeError: null has no such function ā€œtoStringā€

I’ll leave it for now and see if it recovers.

Yes, multiple times.
It always recovers.

You should try to do the setup again, as the tokens are probably aleady expirer. Including the refresh token.

Doing the setup again worked smoothly, however this ā€˜event’ resulted in trying to refresh the token every 30 seconds (is that supposed to happen?), exceeding my bmw quota and spamming the Telegram bot all day as a result. I’ll try again when they’ve recovered a bit :wink: Not sure if it was the OH restart that triggered this or a glitch at bmw.

Strange behavior.
Iā€˜m currently out of office and canā€˜t check it.

Thx a lot, works like a charme.
We got our new BMW yesterday and immediately tried this out.

First I went the manual way and was not successful, then I tried the Telegram way and it worked out of the box.

Without your work I would never have been able to find out myself.

Are you sure it comes from refreshing the token? I had a similar problem with a syntax error in one of the Channels and the log messages came from MQTT trying to reconnect, which was not possible because of the syntax error.
I commented out the Items with the Channel linking one by one until I found the bad one …

1 Like

I had a strange bug today, but it“s not openHAB related.
Just want to make you aware of it since some use the location of the car for rules.

I went to a shop, stopped the engine, opened the trunk and unloaded something.
The war was not locked while i did this and the whole stop took around 10 minutes.
After this stop, the car stopped sending data to the backend.
I just noticed this at home, as the car was still showing were i stopped to unload.
The MyBMW app also showed the car at this location and the trunk as open.
I used the app to send a locking command and this updated the data.

Seems like a stop without locking can potentially interrupt the connection between the car and the backend.

1 Like

It looks like 2 Items are missing in your Items example in 3.1 (although setup worked without them):