HASS Addon TTLock offline integration

Just an update to my experience. It seems to work but after restarting the addon it cant read the access controls. I’m not sure if this is a common problem or just me.
Edit: so… this was true, but all of the sudden it started to work. not sure why. Its either a bit flaky, or behaves differently from a cron job and when run manually with a debugger attached. Very odd indeed.
Edit again: even being run as a service it works… but required a restart on the add on and a few tries. Must be what OP referred to bluetooth being unstable

I had my pi zero close to the lock and worked fine but as soon as i put the lock and the pi about 4 to 5 meters away, it doesn’t work anymore. I guess the only real way is to get a pi per lock. I don’t mind that but that will require changes to allow for that.

Looking at ttlock-sdk-js/tree/main/src/scanner/noble/NobleWebsocketBinding.ts
It seems to me that it would be possible to alter the webserver instance with an array and pass a comma separated list of IPs as a quick and dirty way to get this working with multiple proxies. Unfortunately i dont know how to make changes to the dependency an have the addon use it.

But will keep at looking at this

As a matter of interest, which Pi OS did you use for this?

The standard Raspberry Pi OS. Its pretty much debian and is very stable on the pi.

the pi zero w 2 might be overkill but i had no other PIs at hand and the zero 2 wasn’t that expensive

Thanks for the instructions! I’ve just moved in to a place with one of these locks and have been struggling to manage it without the very limited app. I saw your post, and grabbed a Pi zero W 2, fresh install, update/upgrade, followed your instructions but get “Error: Cannot find module ‘…/build/Release/bluetooth_hci_socket.node’” when ruinning ‘sudo npm run server-tool’. Am I missing something?

Hard to tell without checking all the previous steps :joy:

I took some notes when I did my setup if it helps …

Fresh install to SD Card using RaspImager
Using Bookworm 32 No Desktop to 16Gb

Power up with Card
Wait for connection to WIFI and get IP address

ssh [email protected]

might need to remove fingerprint from known hosts by using
ssh-keygen -R 192.168.1.208

Enter password to get to cmd line:

[email protected]’s password:
Linux rp32z2w 6.6.31+rpt-rpi-v7 #1 SMP Raspbian 1:6.6.31-1+rpt1 (2024-05-29) armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
pi@rp32z2w:~ $

sudo apt update :heavy_check_mark:

sudo apt full-upgrade ✓

sudo reboot ✓

sudo apt autoremove ✓

sudo apt clean ✓

sudo apt install git ✓

sudo apt install npm ✓

git clone GitHub - kind3r/ttlock-sdk-js: JavaScript port of the TTLock Android SDK

cd ttlock-sdk-js

Now need to edit the TTDevice.ts file to resolve some errors in the code .
refer HASS Addon TTLock offline integration - #136 by TheTrueWanderer

sudo nano ./src/device/TTDevice.ts

cut and paste updated TTDevice.ts code into here and SAVE/EXIT

Check file looks OK
cat ./src/device/TTDevice.ts

sudo npm I

pi@rp32z2w:~/ttlock-sdk-js $ sudo npm I
npm WARN deprecated [email protected]: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm WARN deprecated [email protected]: This package is no longer supported.
npm WARN deprecated [email protected]: This package is no longer supported.
npm WARN deprecated [email protected]: Rimraf versions prior to v4 are no longer supported
npm WARN deprecated [email protected]: This package is no longer supported.
npm WARN deprecated [email protected]: Glob versions prior to v9 are no longer supported
npm WARN deprecated [email protected]: Glob versions prior to v9 are no longer supported
npm WARN deprecated [email protected]: TSLint has been deprecated in favor of ESLint. Please see Roadmap: TSLint -> ESLint · Issue #4534 · palantir/tslint · GitHub for more information.

[email protected] prepare
npm run build

[email protected] build
rm -rf ./dist && tsc

added 212 packages, and audited 213 packages in 2m

24 packages are looking for funding
run npm fund for details

found 0 vulnerabilities

sudo npm run server-tool

pi@rp32z2w:~/ttlock-sdk-js $ sudo npm run server-tool

[email protected] server-tool
NOBLE_REPORT_ALL_HCI_EVENTS=1 node ./tools/server.js

20241005 21:35:36.674 noble - ws slave - server mode
20241005 21:35:36.806 → {“type”:“stateChange”,“state”:“poweredOn”}

Regards

That did it.

The only difference of note was the OS. I was using the default 64bit Raspbian from the flash tool. Switching to 32bit seems to have solved the issue.

Kia ora bro, thank you so much!

I must admit i didn’t pay any attention to what bitness OS i installed.
I have another pi on the way to try to connect multiple gateways to the addon and i’ll try to pay a bit more attention on the next install

I think a small change on ttlock-sdk-js might allow for that although i have absolutely no idea how to deploy it. I don’t really know how docker works :frowning:
but we’ll cross that bridge when we reach it

Yesterday I started to look into how I could make some changes to the Addon to see if I could change the slider for AutoLock time (I wanted more than 60sec max currently) and maybe implement a process to set up PassageMode on my Lock?
I have never done any dev work with HA AddOn so it simply started as a learning exercise. I soon found it was very easy to setup a Local Add-on. This then led me to take a copy of the existing Github TTLock Addon and install it as Local. I was thinking this may be a way to solve your multiple lock problem. You could try having two copies of the add on, with different names (Say TTLock1 TTLock2). Then point each to the respective Noble Gateways ? Just a thought, I only have one lock at the moment. So I don’t have a need.

Im now trying to setup a development environment working on vue files (never done that before) to tweak the Addon.

That would segregate each lock into its own addon. not exactly an ideal solution.
Also addons are docker containers and each uses some resources even if its not much, it adds up.
I think that creating an array of proxies and send the messages to all of them would probably work.

I just got a second pi zero 2 w. i’m going to install the gateway in the next few days and attempt to connect the single addon to multiple locks.
I must admit i only had one lock but went and bought a padlock so i can try to connect more than one lock. If it works, i’m replacing all my locks for TTLock ones. I have tuya locks right now and they aren’t great

1 Like

This might be useful for those who don’t know linux very well just like me. This is a way to get the proxy server to start on boot

if you dont have nano installed run:

sudo apt install nano

feel free to ignore the above if you have nano or prefer to use VI.

Create a file

sudo nano /etc/systemd/system/myapp.service

keep the filename or change, but make sure it matches later

in the file paste:

[Unit]
Description=TTLock SDK Node.js Service (Run as [USER])
After=network.target

[Service]
ExecStart=sudo /usr/bin/npm run server-tool --prefix /home/[USER]/ttlock-sdk-js
WorkingDirectory=/home/[USER]/ttlock-sdk-js
Restart=always
User=[USER]
Environment=NODE_ENV=production
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=ttlock-sdk

[Install]
WantedBy=multi-user.target

replace [USER] with your username. This is assuming that the ttlock git was cloned into the home directory of the user, if not change accordingly.

refresh the services:

sudo systemctl daemon-reload

enable the service to start on boot:

sudo systemctl enable myapp.service

at this point if you wish to also start the service the command is:

sudo systemctl start myapp.service

this creates a service that runs under your user and runs the server using sudo to elevate permissions.
It was a bit of a pain to get it to work, that’s why i though t it might be of use

1 Like

Mind pointing me on the right direction to do the same?
I thought i could just change the dependency and force it to do what i want, but no luck. I’ll have to debug the addon

To get the Add-on running local you need :
Access to the HomeAssistant ADDONS directory (I use SAMBA Share Addon)
To get a copy of the Add-on files and put them in the ADDONS directory

Go to :

Then choose Download Zip option

In Downloads folder refer file ‘hass-addons-master.zip’
Expand into ‘hass-addons-master’ folder
Open this folder to see:
repository.json

Copy the ttlock-hass-integration folder into the ADDONS directory.

Back in HomeAssistant check that your no longer running Original TTLock Addon
Probably best to do a Browser EmptyCaches and a HomeAsistant Restart.

Now in HomeAsistant go to Settings/Add Ons and hit AddOn Store.
You should now see a section called Local Addons along with the usual Offical Addons, Community Addons etc

Im still working thru the structure of the code and am working to figure out process steps to make some changes.
I have managed to make a few very small changes (to some Vue files) and get them to show up but I am still not completely clear on how it all hangs together.
So Im not confident in sharing this yet as it is not clear in my head yet.

Any help would be great! I am a somewhat newbie to iOT things. Running HA for about 6 months now. I had TTLock using the product app. I was hoping to integrate into HA. I have everything up and going. I was only able to register one fingerprint. I keep getting errors trying to add more fingerprints, key cards, or pins. I also can not adjust the auto lock timing.

So what do you see in the TTLock Addon log?

I have added cards, fingerprints and pins. My suggestion is (after initial pair) to just add a pin and test it all out before you work on cards and fingerprints. I had a bit of trouble at first. And the code seems to not be implemented for date validation or PIN type (permanent, cyclic, etc) so I ignored the inputs for dates/times. When you run a ‘Add Pin’ go and check the log file for more complete picture of each step.

As for Autolock timing, what are your requirements for this?

I have managed to change the ADDon ‘Settings’ Interface that allows the Autolock ‘slider’ to go to 120 (secs). It’s a bit involved but I could give you some steps to follow if your game.

How/What have you setup for the Bluetooth Gateway?

I have just managed today to control the setup of the lock directly from the bluetooth gateway (running on PiZero2W) via SSH and NPM Run commands to add Passage mode to the lock. But this journey was again quite complicated as I had to alter some of the example code provided and make sure the config files generated from the PAIR operation that was performed via the ADDon did not get altered.

The initial Pair operation creates a set of values for AES key, adminPs, unlockKey, and adminpassword. It is very easy to screw these up if you’re trying to send commands from two different sources!

The aim for my situation, single lock in HA, was to be able to set a slightly longer AutoLock time (120) and to set a specific Passage mode definition. I have done that and am happy with the outcome. Not sure I’ll be doing much more, other than write up the steps as my elderly brain is running out of space for these things.

Thanks for your work on this guys but -
All set up and working for me on a PiZero 1.x w , seems ok.
I can add codes , cards etc but after a while of inactivity and i press unlock it doesnt seem unlock without pressing unlock again.

I’ve not had much time to check it out fully, maybe later in the week.

Wondered if anyone alse had experienced this ?

I have a lock but its not setup with home assistant yet.

I bought a padlock to try to get this to work but i don’t keep the addon working too long so i haven’t seen this happening.

Just to be sure, after a while the first press of unlock fails, but pressing again will work. is that correct?
If that is the case it’s possible that the first connect request might be failing. do you have any error messages on the logs?

thanks for your input but it all seems to be working fine now.
There are a couple of seconds delay clicking the unlock button to the lock actually unlocking and
I was wondering if the zero w 1.x was the problem and changing to a w 2 might improve it.
Not sure yet, i have a HA running on an RP 5 not too far away so might try that also.

i have a pi zero 2 w about 20cm from the lock and the signal is miserable.
maybe someone who understands bluetooth communication can chime in .

Any success with running multiple gateways (ESP) on on e add-on?
If not, anyone tried multiple local add-ons on same Home Assistant instance?
Thank you

I managed to connect multiple gateways but didn’t have the time to test it.
It connected to the multiple gateways and its not throwing errors.
I connected to a single lock over one of the two connected gateways.
Still need to connect to a second lock but time has been scarce lately.
I’m fairly hopeful this works

I’ll drop a message here when i had the time to connect two locks to different gateways at the same time.