[Tutorial] Homebridge for Nest

Wanted to share the steps I used to get setup with Homebridge for Nest working as a service (starts automatically on boot).

Flash your pi with DietPi
After initial setup (network, etc.), use dietpi-software to install:

  • build-essentials
  • avahi-daemon
  • node.js
  • python

Install homebridge
http://github.crookster.org/Extending-HomeKit-with-Homebridge-and-Raspberry-Pi/
^ I couldn’t get the steps listed here to work verbatim, so I roughly used these steps:

# apt-get install -y libavahi-compat-libdnssd-dev
# npm install -g --unsafe-perm homebridge hap-nodejs node-gyp
# npm which default
# cd /usr/local/lib/node_modules/
# npm install --unsafe-perm bignum
# node-gyp BUILDTYPE=Release rebuild
# which node-gyp BUILDTYPE=Release rebuild

https://github.com/nfarina/homebridge/blob/master/README.md#installation
^ Then finish up with the rest of these steps to install homebridge.

Basically launch homebridge once:

# homebridge

You should see a QR code and a Homekit code logged to console. You can use the Home app on your iOS device to “add” the homebridge device, now. Just make sure you set the same Homekit code in your config.json later (below).

Install nest module for homebridge
https://www.npmjs.com/package/homebridge-nest
^ Follow the instructions here on how to setup your Nest Developer account, which allows the nest module to communicate with your thermostat through the Nest APIs.

Once you have your Nest Dev. account setup, edit the config.json, populating it with info from your Nest Dev. account:

# nano ~/.homebridge/config.json

Here’s a sample of my config.json prior to adding the “code” value (see below about the code). I set the “username” to match my Pi’s MAC address (run ifconfig from the Pi console to get network adapter info, including the MAC address of your Ethernet/WiFi adapter). The port is dynamically generated the first time you launch homebridge, but it seems you can also just force a specific port like 35565. The pin value is also dynamically generated on first time launch. You can copy the pin from the console into your config.json.

{
    "bridge": {
        "name": "Homebridge",
        "username": "B8:27:EF:34:3D:6F",
        "port": 35565,
        "pin": "032-64-154"
    },

    "description": "Homebridge for Nest automation.",

    "accessories": [
    ],

    "platforms": [
        {
        "platform": "Nest",

        "token" : "",

        "clientId": "yourNestDevId",
        "clientSecret": "yourNestDevSecret",
        "code": "yourNestPinCodeFromNestAuthUrl"
    }
    ]
}

Note there’s also a sample config.json here: https://github.com/nfarina/homebridge/blob/master/config-sample.json

Now run homebridge again, and if it works properly you should see your code value dumped to the console. Copy/paste that into the config.json as the “token” value, save, and then relaunch homebridge one more time. You should be in business! Check the Home app on your iOS device.

Setup homebridge to run on boot (after power cycle)
https://timleland.com/setup-homebridge-to-start-on-bootup/
^ I tried the older init.d method, but it didn’t work at boot. The (newer?) systemctl method linked above worked.

These are my steps. Stop homebridge before proceeding, if its running.

# nano /etc/default/homebridge

Paste in:

# 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=*



# nano /etc/systemd/system/homebridge.service

Paste in:

[Unit]
Description=Node.js HomeKit Server 
After=syslog.target network-online.target

[Service]
Type=simple
User=homebridge
EnvironmentFile=/etc/default/homebridge
ExecStart=/usr/local/bin/homebridge $HOMEBRIDGE_OPTS
Restart=on-failure
RestartSec=10
KillMode=process

[Install]
WantedBy=multi-user.target

The finish up by adding the homebridge system user, setting up the system config for homebridge, and enabling/starting your new homebridge service:

useradd --system homebridge
mkdir /var/lib/homebridge
cp ~/.homebridge/config.json /var/lib/homebridge/
cp -r ~/.homebridge/persist /var/lib/homebridge
chmod -R 0777 /var/lib/homebridge
systemctl daemon-reload
systemctl enable homebridge
systemctl start homebridge

Check service logs
If you need to check the logs for the homebridge service this command will output the entire log to console:

journalctl -u homebridge --no-pager

I accidentally removed the homebridge’s API access while using the Nest iOS app. To re-enable access, login to the Nest Developer site and use the same Authorization URL as we used previously to generate a new PIN. Update the /var/lib/homebridge/config.json file, removing the “token” and updating the new PIN in the “code” value.

Restart the homebridge service and check the logs using the above command. You’ll find an entry in the logs providing you with a new token value - just copy that token and edit the config.json again, adding in the new token. The same format we used in the previous steps in this guide works.

Hi benb,

nice write up, thanks.
Will move this over to Community Tutorials, be surely a better place for it.

cu
k-plan

Thanks for sharing these useful tips. This would be very helpful.

Thank you. Great

Hi i catch an error when tried to install homebridge, could you please help me with that?

Errors occur during the installation of bignum

sudo npm install --unsafe-perm bignum
What can be wrong?

Python 2.7.13
Node.js v13.0.1

Thank you in advance!

Try using python3/pip3

It doesn’t work. I tried. Other advises?

You might want to create a new topic/thread to get more replies to your question

Hi,
I am experiencing issues running your tutorial (I also tried another one)
while running root@DietPi:~# npm which default I am getting Unknown command: "which"
I also tried npm install which but the issue persists. What is the purpose of this? moving npm’s default dir from ~/.npm to /usr/local/lib/node_modules/ ?
Also: If I want a fresh start of HB and not load an old config, how do I install homebridge-ui-x?
Thx in advance.