Let op: Tweakers stopt per 2023 met Tweakblogs. In dit artikel leggen we uit waarom we hiervoor hebben gekozen.

HomeBridge on a UniFi CloudKey Gen1

By ro3lie on vrijdag 26 april 2019 22:00 - Comments (8)
Category: -, Views: 8.692

So this is my first blogpost! I'm used to document allot if information but never really shared any online (like this). Since I am active in Tweakers and Reddit I wrote this post in English.
Feel free to reply in English or Dutch.
If you have any comments or tips I would like to hear them.

This post is ment to be a tutorial/how-to page for installing HomeBridge on a UniFi CloudKey (Gen1).

Introduction:
I've been using HomeBridge for a few months now to link my Nest/Sonos/INNR equipment to Apple HomeKit. I used a Ubuntu VM to run HomeBridge on but found this to be instable/slow and also big.

Recently I acquired a UniFi CloudKey (Gen1) which was decommissioned at a customer site.
I found out this in fact a "Linux Stick" based on Debian 8 Jesse (the same as a Raspberry Pi).
I took the CloudKey home and saved it for some experiments to install HomeBridge on, which resulted in this post.

Why would you use a UniFi CloudKey Gen1? For me this reason was simple:
It has a Quad Core SoC Processor, 2GB RAM, 16GB NAND storage, 1Gbit NIC with 802.3af (Power over ethernet) with a max consumption of 5W and it has a MicroSD slot.

So in short.
- It is smaller then a Raspberry Pi
- It has a MicroSD card slot which I am going to use to write back-ups to.
- It has PoE functionality, which safes me a power adapter and gives me options to manage it remotely from my switch (complete power off/on)
- It might be cheaper then a Raspberry Pi (Allot of CloudKey Gen1's are now available due to the new Gen2), also it doesn't require an additional case, power supply and SD card

In the manual below I used the following:
UniFi CloudKey Gen1, PuTTY SSH Client, UniFi CloudKey Firmware 0.13.10 and HomeBridge.

Please notice: Performing the next steps is at own risk. I don't take any responsibility if a CloudKey is bricked due to removing to many packages. }:O

How to install HomeBridge on a UniFi CloudKey Gen1

Step 1: Factory reset/firmware update the CloudKey:
For the steps bellow I used the Emergency Recovery Mode which gives you the option to update the firmware or perform a factory reset.

1a. Download the latest firmware update from UniFi at: link

1b. Follow the Emergency Recovery steps according the KB-article
Once the CloudKey is in Recovery Mode it will get a DHCP IP-address from your DHCP server.
You can find this in your DHCP leases on your firewall/router or use an IP-scanning tool.

1c. First perform the steps for a firmware update. Once this is finished you need to reboot.
After the reboot repeat the steps to enter the Recovery Mode and continue.

1d. Perform a 'Reset to factory defaults'. To complete the reset you need to reboot.
The last reboot will take a few minutes to complete since all the packages are being restored to the 'default'.

Once everything is ready the UniFi CloudKey should be available on your network.
You can find this in your DHCP leases on your firewall/router or use an IP-scanning tool.

Step 2: Access the UniFi CoudKey by SSH.
For SSH access I prefer to use the tool PuTTY but you can use any SSH tool.

2a. Start a SSH-session and log-in with the default login/password (ubnt/ubnt).


Step 3: Cleaning up CloudKey packages and updating the OS/software.
Now lets start cleaning up the CloudKey. It contains allot of packages which are no longer required for our usage. The first thing we do is remove the UniFi Network Controller.

Informational: The CloudKey hosts two packages/portals.
The CloudKey Configuration Page and the Network Controller.
The Network Controller is used to manage your UniFi equipment like access points/switches or the USG.
If you don't have any UniFi equipment or use a separate CloudKey for this we can safely remove this package.




The procedure for this is also noted in a KB-article of UniFi at the following page: link

3a. Remove the UniFi Controller with the following command:
code:
1
sudo dpkg -P unifi
The purging process will take place, removing all UniFi Network Controller information.

3b. Now let's remove the sources.list entries which are mainly looking at some old repo's and UniFi repo's.
code:
1
2
3
4
sudo rm /etc/apt/sources.list
sudo rm /etc/apt/sources.list.d/nodejs.list
sudo rm /etc/apt/sources.list.d/security.list
sudo rm /etc/apt/sources.list.d/ubnt-unifi.list

3c. In above steps I removed the original sources.list file. So now we need to create a new file with the next command:
code:
1
vi /etc/apt/sources.list
Enter the following text and save the file.
code:
1
2
3
4
5
6
deb http://deb.debian.org/debian stretch main contrib non-free
deb-src http://deb.debian.org/debian stretch main contrib non-free
deb http://deb.debian.org/debian-security/ stretch/updates main contrib non-free
deb-src http://deb.debian.org/debian-security/ stretch/updates main contrib non-free
deb http://deb.debian.org/debian stretch-updates main contrib non-free
deb-src http://deb.debian.org/debian stretch-updates main contrib non-free
And create a second file:
code:
1
vi /etc/apt/sources.list.d/nodesource.list
Enter the following text and save the file.
code:
1
2
deb https://deb.nodesource.com/node_10.x stretch main
deb-src https://deb.nodesource.com/node_10.x stretch main
3d. Verify the file with the following command:
code:
1
cat /etc/apt/sources.list
3e. After the this command we follow up with the next commands:
code:
1
2
sudo apt-get update
sudo apt-get clean && sudo apt-get clean all && sudo apt-get autoclean && sudo apt-get update
3f. Before we continue, let's do a reboot of the CloudKey. This should take a few seconds (30sec).
code:
1
reboot
After the reboot you can still start a SSH connection and manage the CloudKey with the management page.
If desired you can now adjust the network settings, admin credentials and timezone of the CloudKey.
This can also be done at the end of the tutorial.

You can access the CloudKey Management page with: HTTPS://<IP-ADDRESS>/login

3g. Now we are going to update the CloudKey to a newer Debian version (v9 stretch) and software packages.
UniFi CloudKey default runs on Debian 8.11 "Jessie" which is familiar to a Raspberry Pi.

To start the update procedure enter the following command:
code:
1
sudo apt-get update && sudo apt-get upgrade
During the upgrade you may be asked what to do with the unattended-upgrades configration file.
I choose to 'Keep the local version currently installed'.



Once all the updates are finished you need to perform the following command to remove the old configration file:
code:
1
rm /etc/apt/apt.conf.d/50unattended-upgrades.ucf-dist
3h. Remove some additional packages which are not needed or create conflicts for HomeBridge:
Removing FreeRadius:
code:
1
2
sudo apt-get remove freeradius
sudo apt-get purge freeradius
Removing NodeJS v6.16.0.
UniFi CloudKey already contains a NodeJS installation with a legacy version.
To check the version, perform the following command: "node -v". This will show the version: v6.16.0
https://tweakers.net/ext/f/GmO0Xp8vQiZU5PJLYY2yYV9f/full.png

To remove and install the NodeJS v10 enter the following commands:
code:
1
2
3
sudo apt-get purge nodejs npm
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs npm
At this point the CloudKey is cleaned and ready to install HomeBridge.
If desired you can stop at this point and install any other package you like (for example: PiHole).

To install HomeBridge follow the next steps:


Step 4. Install HomeBridge

4a. Install the required software with the following commands:
code:
1
2
3
4
5
6
sudo apt-get install build-essential curl git m4 ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev
sudo apt-get install g++
sudo apt-get install git make
sudo apt-get install libavahi-compat-libdnssd-dev
sudo npm install -g --unsafe-perm homebridge
sudo npm install -g --unsafe-perm homebridge-config-ui-x
After this you have installed HomeBridge and HomeBridge Config UI (webgui for easy management).

4b. Before we can create a config.json file we just need to start HomeBridge (temporarily).
code:
1
sudo homebridge
Once it is started we can close HomeBridge with CTRL+C.
During the start process the required folders are created automatically.

https://tweakers.net/ext/f/ETWGfdAWsUN7EpJNC23v78rc/full.png

4c. Create a config file required to build a HomeBridge Setup
code:
1
sudo vi .homebridge/config.json
Enter the following text and save the file.
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
    "bridge": {
        "name": "Homebridge",
        "username": "CC:22:3D:E3:CE:30",
        "port": 51826,
        "pin": "031-45-158"
    },
    "description": "This is an example configuration file with one fake accessory and one fake platform. You can use this as a template for creating your own configuration file containing devices you actually own.",
    "platforms": [
        {
            "name": "Config",
            "port": 8080,
            "theme": "red",
            "tempUnits": "c",
            "platform": "config"
        }
    ]
}
4d. Now let's test HomeBridge by starting it manually.
code:
1
sudo homebridge
If everything works well HomeBridge will start and will be accessible at the webpage http://<IP-address>:8080







Now we are basically done. But we don't want to start homebridge manually by SSH everytime we reboot the CloudKey. So in the next steps a service is created based on serviced.

Step 5: Configure HomeBridge as a Systemd Service.

For this I have used an existing article which can be found at: link

5a. Create a new file in the folder /etc/default.
code:
1
sudo vi /etc/default/homebridge
Enter the following text:
code:
1
2
3
4
5
6
7
# Defaults / Configuration options for homebridge
# The following settings tells homebridge where to find the config.json file and where to persist the data (i.e. pairing and others)
HOMEBRIDGE_OPTS=-U /var/lib/homebridge

# If you uncomment the following line, homebridge will log more 
# You can display this via systemd's journalctl: journalctl -f -u homebridge
# DEBUG=*
5b. Create a file in the folder /etc/systemd/system:
code:
1
sudo vi /etc/systemd/system/homebridge.service
Enter the following text:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Unit]
Description=Node.js HomeKit Server 
After=syslog.target network-online.target

[Service]
Type=simple
User=homebridge
EnvironmentFile=/etc/default/homebridge
# Adapt this to your specific setup (could be /usr/bin/homebridge)
# See comments below for more information
ExecStart=/usr/bin/homebridge $HOMEBRIDGE_OPTS
Restart=on-failure
RestartSec=10
KillMode=process

[Install]
WantedBy=multi-user.target
Please note! The configuration file above here is not identical with the GitHub article.
You need to check the folder where HomeBridge is installed. In the article there is been refered to /usr/local/bin/homebridge. But we need to use /usr/bin/homebridge.

To verify the location where HomeBridge is installed, enter the following command:
code:
1
which homebridge
5c. Now we need to copy the config.json file to the directory used by the service and set the required access rights.
code:
1
2
3
4
5
sudo mkdir /var/lib/homebridge
sudo useradd --system homebridge
sudo chown -R homebridge:homebridge /var/lib/homebridge
sudo chmod 777 -R /var/lib/homebridge
sudo cp .homebridge/config.json /var/lib/homebridge/config.json
Please note: The config.json file at .homebridge is not used anymore.

5d. Create the HomeBrigeServer Service with the following commands:
code:
1
2
3
systemctl daemon-reload
systemctl enable homebridge
systemctl start homebridge
To check if the HomeBridge service is created succesfully and running, enter the following command:
code:
1
systemctl status homebridge


For a complete logfile check use the following command:
code:
1
journalctl -u homebridge
At this point HomeBridge is running as a service. This means you can safely reboot the CloudKey and HomeBridge will come back online automatically.
For example: if you enter the 'reboot' button in the HomeBridge Webpage or the CloudKey get's disconnected/connected.
HomeBridge can be accessed by at the webpage mentioned before which is hosted by the NPM plug-in (homebridge-config-ui-x).

Step 6- Setting up a HomeBridge back-up to the MicroSD card:
Now the finishing part is to create a back-up job for the HomeBridge Folder to the MicroSD card.

The HomeBridge-config-ui-x plug-in creates a copy file of every config change in the HomeBridge folder. This feature ensures you of config versions which can be rolled back anytime. Due to the fact all the previous config versions are available in this folder we only need to sync the folder content to the MicroSD card.

For the synchronisation of this data we are using RSYNC which will be performed by a crontab.
The CloudKey auto-mounts the MicroSD card to /data, we will be creating a subfolder in homebridge-backup to store all the files.

6a. Create a back-up folder and add a crontab job:
code:
1
2
sudo mkdir /data/homebridge-backup
sudo crontab -e
Insert the following line in the crontab.
code:
1
0 * * * * rsync -av --delete /var/lib/homebridge/ /data/homebridge-backup/
If you would like to test the crontab you need to replace the zero (0) with a asterisk (*), this means it runs every minute.

After setting up the crontab verify the folder /data/homebridge-backup on it's contents.
code:
1
ls /data/homebridge-backup/

You are now done with setting up HomeBridge on a UniFi CloudKey. :Y)

Please note the following things:
- If you perform a UniFi Firmware update you will probably lose your HomeBridge set-up and/or data.
- The username/password of the CloudKey can be set at the CloudKey Management page (mentioned above).
- The IP-address (DHCP/Manual), HostName and NTP-settings/TimeRegion can be set at the CloudKey Management page (mentioned above).
- You might need to set sudo rights for HomeBridge, for this see: link
- Looking for a wall-mount for the UniFi CloudKey? Check this out: https://www.thingiverse.com/thing:3297801
- I have performed this tutorial two times and haven't experienced any issue. If you run into any problem please let me know.


***UPDATE***
Due to spam in the comments I have disabled this option.
For questions please DM.