TTLOCK Integration with HA

i used the ttlock api to establish this. if you like i can help

1st step you’ll need a developer account and it must be reviewed.
once its reviewed you’ll need to send the client_id, client_secret, grant_type, a new username, a new password, and a redirect url… this can be done with a simple html form

		Access token<br>
<form action="https://api.sciener.com/oauth2/token" method="post" enctype="application/x-www-form-urlencoded">
		<input type="text" name="client_id" value="your app id"><br>
		<input type="text" name="client_secret" value="your app secret"><br>
		<input type="text" name="grant_type" value="password"><br>
		<input type="text" name="username" value="your new username"><br>
		<input type="text" name="password" value="your new password"><br>
		<input type="text" id="lname" name="redirect_uri" value="https://home assistant address/auth/authorize"><br>
		<input type="submit" value="Submit">
		</form>

when you do this it will give you a access token and refresh token
KEEP THIS SAFE!!!

you will need the access token to make rest calls to the api
to get the last user who unlocked the door i used a sensor

sensor:
  - platform: rest
    name: "unlock name"
    scan_interval: 5
    resource_template: https://api.ttlock.com/v3/lockRecord/list?clientId= your client id/app id &accessToken= your access token &lockId= your lock id number &pageNo=1&pageSize=1&date={{ (now().timestamp()  | int  * 1000 ) }}
    value_template: '{{ value_json.list[0].username }}'

the api doc has all the information from here.

4 Likes

thanks, I have managed to integrate my TTLock with HA and everything is working fine, I am getting the desired values and doing the wanted action such as lock and unlock.
I am planning to do tutorial or write a guideline for references within these two days.

3 Likes

Yo, I already gave you the tutorial. Lol. If you don’t mind toss a quick shout-out. :rofl:

hi
i don’t fiend the “your lock id number”

hi i use this sensor to get the lockId

If you’re interested, there is now a local integration of the TTLocks: HASS Addon TTLock offline integration

3 Likes

I have also just started using this API

I can now get the access token and make other requests but I don’t understand how my developer account is associated with the locks?

Can I make requests for any lock if I have the lockid?
At the moment my request for locklist returns zero entries

Thanks for any help
Tom

Let me do my steps here to help somebody, but my lock is still not listed… I don’t know why. I already have any lock on my IOS App working, just want to integrate with HA:

  1. Created Account on open.ttlock.com
  2. Created App there and waited it do be approved
  3. got client_id and client_secret from the page
  4. registered a user (returns username to be used later) with the same as my IOS App with:

curl -i -X POST -d “clientId=XXXXXXX&clientSecret=YYYYYYYYY&username=IOS_USERNAME&password=PASS_IN_MD5_FORMAT&date=date +%shttps://api.ttlock.com/v3/user/register

5. Got Token from API running:

curl -i -X POST -H ‘Content-Type: application/x-www-form-urlencoded’ -d “client_id=XXXXXXX&client_secret=YYYYYYY&username=ZZZZZZZ&password=PASS_MD5_FORMAT” https://api.ttlock.com/oauth2/token

6. Tried to list my locks:

curl -i -X POST -H ‘Content-Type: application/x-www-form-urlencoded’ -d “clientId=XXXXX&accessToken=TTTTTTT&pageNo=1&pageSize=20&date=date +%s000https://api.ttlock.com/v3/lock/list

{“list”:[],“pageNo”:1,“pageSize”:20,“pages”:0,“total”:0}


Don't know what is happening... will try again later.

Updating, after a lot of tries, what I did to get it recognised:

  1. In my old account (the one I am logged into IOS App), selected transfer lock
  2. Choose to transfer to the new user I created on the developer account, the pone that starts with fccd_

Thats it… ater transferred, now I can get LockerID and configure it into HA. Now I need to test what I can do with it.

does this integration still work. I have tried to use it. However, im struggling in creating new account. Does the new account has to be in an email format?
Thanks

I was able to make an account with TTLock but I am completely lost as to how to integrate the TTLock add-on and actually use it. I did use an email when I created the account. I then set and confirmed the password. Then on the right side of the registration form it says “Get Code”. When you complete that process and before you can hit NEXT there will be a code emailed to you. Check you spam folder in your email as well as keep refreshing your email because there is a countdown for how long the code is valid for and it seemed like I recieved the email with the code seconds before it times out. Enter the code even if it does time out and click NEXT and you should be able to proceed. However, as for integrating it with HA I am still lost.

Hi. Any update on the tutorial? I’m having some trouble understanding this.

I’m hoping to give this a crack soon. I have two different TTLocks on the iOS app, so I’ll keep a log of what I’m doing in case I am successful then post a tutorial.

After moving into our own place a few weeks ago, I’ve been building up the smart devices on Google Home, until recently when I reached a good amount to start migrating it all to HA. So far I have been successful with some garden lights, a downlight globe, solar system and Deco Mesh Wifi. I failed on the Livolo Roller Blinds and a random Garage Door switch which I really want to get working. Next will be the TTLock Door locks and a dozen indoor downlights. Oh yeah and the Kommander board I just ordered to replace the crappy Bosch home security system so I can add in all the PIR sensors for light automation… All equally fun and frustrating!!! but something to do during lockdown!

Has anyone managed to get this api to work? I’m going crazy, I did all the steps mentioned above but nothing, I managed to get token by registering a user through the api, but I can’t control my lock, nor do I get the lockid.

this method for ttlock2mqtt

i managed this to work with ha and here what i did
first you need to register at this site : https://open.ttlock.com/login
then click on create application and fill application name , logo , type (choose whatever you want , i chosed them all) and description .
after you get the approval you will be having 1- client_id 2- client_secret (save them to text file for use later ) .
** important note : the user for this application you can view it on the same site , press on top right at your email beside managment , and there is you can see your user name (not email) and write it in the text file with previous info .
now open terminal and paste this to link account with the application

** please note you have to md5 encrypted your password , open this site https://www.md5online.org/md5-decrypt.html  and put your password to decrypt 

$ curl --location --request POST 'https://api.ttlock.com/v3/user/register?clientId=put here your client id&clientSecret=put here your client secret&username=put here the user name from the site not the email&password=put here the password md5 decrypted for account at site&date=CURRENTMILLIS' \
--header 'Content-Type: application/x-www-form-urlencoded' \

next step i assume you already have the app at the mobile working since before , so do this the following command at terminal to get the token

curl --location --request POST 'https://api.ttlock.com/oauth2/token?client_id=put here your client id&client_secret=put here your client secret&username=put here the user name from your account&password=put here your decrypted password&grant_type=password&redirect_uri=https://yourdomain.com/' \
--header 'Content-Type: application/x-www-form-urlencoded' \

if all goes successfully you will get token and refresh token and other info , save them all

add this to your repo add ons

tonyldo HassIO Addo-ns
[email protected]

install the TTlock2MQTT
at configuration fill the client ID and ttlock token and fill the mqtt data , server , port , mqtt user , mqtt password and start the addon

at Mosquitto broker you will get two devices , the lock and the gateway , enjoy

and many sorry if my english wasnt clear enough

** few references

https://www.md5online.org/md5-decrypt.html

1 Like

Any luck with this ? I get this error - HTTP Status 400 -

type Status report

message

description The request sent by the client was syntactically incorrect.

Apache Tomcat/7.0.57

After painfully working through the ttlock api to get all the relevant info I have TTlock2MQTT installed and it seems to have found my locks (two) and the gwy in the logs but I am also getting an comm error. Not sure where to go from here and how to configure ha to see/use the locks. Any help would be appreciated.

Log file snippet, this just repeats over and over. (I x’d out part of the code numbers for privacy)
‘’’
2022-01-02 20:21:45 - [INFO] TTLOCK2MQTT: Client LOCK-4353816-xxxxx72905 TTlock Mqtt Created
2022-01-02 20:21:45 - [INFO] TTLOCK2MQTT: Client LOCK-4353816-xxxxx72905 TTlock Mqtt on client_loop
2022-01-02 20:21:55 - [ERROR] TTLOCK2MQTT: Client LOCK-4353816-xxxxx72905 Bad connection Returned code= 5
2022-01-02 20:21:55 - [INFO] TTLOCK2MQTT: Client LOCK-4353816-xxxxx72905 disconnected!
2022-01-02 20:22:45 - [INFO] TTLOCK2MQTT: Client LOCK-4309730-xxxxx72965 TTlock Mqtt Created
2022-01-02 20:22:45 - [INFO] TTLOCK2MQTT: Client LOCK-4309730-xxxxx72965 TTlock Mqtt on client_loop
2022-01-02 20:22:45 - [ERROR] TTLOCK2MQTT: Client LOCK-4353816-xxxxx72905 has 5 times bad connection
2022-01-02 20:22:45 - [ERROR] TTLOCK2MQTT: Client LOCK-4309730-xxxxx72965 Bad connection Returned code= 5
2022-01-02 20:22:45 - [INFO] TTLOCK2MQTT: Client LOCK-4309730-xxxxx72965 disconnected!
2022-01-02 20:23:45 - [INFO] TTLOCK2MQTT: Current threads: 3
2022-01-02 20:23:45 - [ERROR] TTLOCK2MQTT: Client LOCK-4309730-xxxxx72965 has 5 times bad connection
2022-01-02 20:23:46 - [INFO] TTLOCK2MQTT: Client GATEWAY-240978-xxxxx73026 TTlock Mqtt Created
2022-01-02 20:23:46 - [INFO] TTLOCK2MQTT: Client GATEWAY-240978-xxxxx73026 TTlock Mqtt on client_loop
2022-01-02 20:23:56 - [ERROR] TTLOCK2MQTT: Client GATEWAY-240978-xxxxx73026 Bad connection Returned code= 5
2022-01-02 20:23:56 - [INFO] TTLOCK2MQTT: Client GATEWAY-240978-xxxxx73026 disconnected!
‘’’

Tried some more trouble shooting, thought maybe it was not connecting to my mqtt server but if I change any of those parms I see mqtt errors that were not there before. I put them back and fudged the token and I see good initiation but no locks or gwys appear. When the correct data is running as above with the locks and gwy repeating I have sniffed the mqtt messages and don’t see any from ttlock. Any help would be greatly appreciated, I seem so close. J

thanks all of you, i managed to have the lock in HA but with very limited actions.
i will write down the steps I used:

  • created account on https://open.ttlock.com

  • created application and filled in the requested informations

  • after the application was approved, extracted the client_id and client_secret

  • opened a SSH connection to HA and entered the next commands:
    curl --location --request POST 'https://api.ttlock.com/v3/user/register?clientId=<YOUR_CLIENT_ID>&clientSecret=<YOUR_CLIENT_SECRET>&username=<USERNAME>&password=<MD5_ENCRYPTED_PASSWORD>&date=CURRENTMILLIS'

          - where USERNAME I think it might be anything but in my case when I logged out from dev account and clicked on login window I was presented with a 6 digit username. It didn't  worked to login with that but that is what I used next
          - password needs to be encrypted md5; you can use https://www.md5online.org/md5-encrypt.html
         - after running this command, in the output I received my username but with a prefix, something like bhdca_<username>
    
  • now you can run the next command:
    curl --location --request POST 'https://api.ttlock.com/oauth2/token?client_id=<YOUR_CLIENT_ID>&client_secret=<YOUR_CLIENT_SECRET>&username=<YOUR_USERNAME_FROM_PREVIOUS_COMMAND_OUTPUT>&password=<MD5_ENCRYPTED_PASSWORD>&grant_type=password&redirect_uri=https://yourdomain.com/' --header 'Content-Type: application/x-www-form-urlencoded'

      - in the output you should have the token and other info
    
  • install the add-on and configure as @tammeryousef mentioned in the above post; at this moment you should not see any new device in MQTT broker (I’m using Mosqoitto Broker add-on)

  • open the TTLock app on your phone and transfer the locks and gateways to your new user created with 1st command (e.g. bhdca_111111)

  • logout and login using the new user and the same password used for developer account.

  • in 2 - 3 minutes you should be able to see the gateway and the loks in MQTT in Configuration → Integrations

At the moment I can lock /unlock using HA but that is it, no real time status :frowning:

p.s actually there is a 60 sec delay set in config for status update

4 Likes

I got my TTLock connected to Home Assistant thanks to @xbecomingx

Question. Has anyone figured out how to create an Automation based on the door being unlocked? Or is that too “real time” for this integration.

1 Like