More on Home Automation, Homebridge, Nest, and Apple’s HomeKit

I got Homebridge, the Node.js-based software Bridge for use with home automation and Nest (and other Internet of Things – aka IoT) home automation devices, working and running in my environment. And then my WeMo bridge started failing. But more herein about getting Nest to work with Homebridge and Apple Homekit because, as usual, the documentation is not quite sufficient to make it easy to set up.

It was sufficient to power cycle the WeMo bridge, so at least that was a quick fix. But I hope it doesn’t crash frequently. Only time will tell. But this happened within about 48 hours of setting it up, so not a great first impression.

For the Nest and Homebridge setup/configuration notes:

  1. I set up Node.js with homebrew natively within my macOS server that runs High Sierra at home.
  2. Because I’m running High Sierra, the install of Homebridge was uneventful, but I did need to specify “–unsafe-perms” on my execution of npm (Node Package Manager)

For setting up Nest with Homebridge, the main instructions on the Homebridge plugin live in the repository’s main file, and are pretty good. They are, however, not exact, in a few frustrating ways (which I’ll note here, and, as part of the geek discipline/open source discipline, I’ll also clone the repo, fix the docs, and send the maintainer of the main plugin the docs updates as a pull request):

  1. Setting up the Nest API:
    1. While setting up the Nest API, a Product Description is REQUIRED for each permission level you specify. It should probably be bolded or something in step 8.
    2. Steps 9 – 14 are done in conjunction with the sample JSON code given in the Configuration Sample section below the instructions. You have to go back and forth, you have to know JSON, or enough of it to get by, you have to know about JSON LINT validators, and so on. It’s interactive, iterative, and kind of a bear to work with. My revised, first draft notes for doing that are as follows:

Instructions and notes for setting up Nest Homebridge plugin with token-based security

Required information:

  • Product ID from Nest API setup (clientID in Homebridge config.json file)
  • Product Secret from Nest API setup (clientSecret in Homebridge config.json file)
  • PIN code from Nest API setup Authorization URL (code in Homebridge config.json file)
    • To get PIN code:
      1. Copy Authorization URL from Nest API setup
      2. Open URL in a browser tab
      3. Accept the terms
      4. PIN code will display on next page (8 characters, alphanumeric, all caps)
  • Later you will get a Token, but more on that later.
  • Know that by default, homebridge will “log” messages and errors to the terminal/console it’s running on. Some of the original instructions say “you should find a log”, but don’t go around the Operating System looking for a nonexistent log like I did. Just look at the output in the same terminal window that you use to run homebridge.
  • Also, if you get a sample of the Nest config.json entries that includes a line for "token": "...", delete that line or copy it to another empty file until it’s time to have a token. Otherwise you’ll get an error.
  • Similarly, some Nest config.json samples include a line for "structureId": "...". This is a great idea if you need it and use it and know what it’s for. Otherwise, if you don’t, it’ll likely case an error when Homebridge starts, so remove it unless you really want to use it.

Procedure to get the connection between Homebridge and Nest set up (after you create your Nest API Product):

  1. Use Product ID, Product Secret, and PIN code to temporarily connect with Nest and obtain your permanent token.
    1. In config.json, add the following code to products, in between the [ and the ].
    2. If you are adding to a products field that already has an entry with { and } around it, append a “,” to the last } before the ], and insert this code below. So e.g., the code JSON should look a bit like:

      (Notes: Take out any ellipses – “…”, and remember NOT To put a comma at the end of any nested list, and maybe run through a JSON validator like before saving your config.json file.)

    3. Start Homebrew. Part of the startup text that it generates will include a line that looks like:
      "CODE IS ONLY VALID ONCE! Update config to use {'token':'c.0AAbCde11f2ghIJkLmn...'} instead."

      Copy the text starting at “c.0AAb…”. This is your token. Now you can go ahead with the permanent connection.

    4. Use CTRL-C to halt the Homebrew server.
  2. Make the permanent connection by using the token you just copied into the config.json file.
      1. In your config.json in the Nest platform part of the file, put a
        "token": "",
        line in just after the
        "platform": "Nest",
        line. You shouldn’t need the "clientID", "clientSecret", or "code" lines any more, but may as well keep them in unless you really have security concerns.
      2. Inside the quotes in the "token" line, put your text that you copied from the Homebrew startup logs. A somewhat comprehensive version of your config.json should look like this.
              "name": "Homebridge",
              "username": "CC:22:3D:E3:CE:30",
              "port": 51826,
              "pin": "031-45-154"
           "description": "Description.",
           "accessories": [
           "platforms": [
                 "platform": "Some-other-platform."
                 "platform": "Nest",
                 "token": "c.ABCDEfGHiJKLmN1opQRst2uVwXy3ZABCDEfGHiJKLmN1opQRst2uVwXy3ZABCDEfGHiJKLmN1opQRst2uVwXy3ZABCDEfGHiJKLmN1opQRst2uVwXy3ZABCDEfGHiJKLmN1opQRst2uVwXy3ZABCDEfGHiJKLmN1",
                 "clientId": "11aa2222-3333-444b-5555-66c66ccccc6",
                 "clientSecret": "AAAAA1BcDDeFFF2gHHHi3JJkL",

        (Note: I read a lot of advice that I could drop the clientId and clientSecret nodes from the token version of the config.json file, but they also said I could keep it in, so I kept it, for fear of causing more problems by removing it. Also this config.json file assumes no accessories, two platforms – including Nest, and note I anonymized all the secrets but kept formatting and string length.)

      3. Once your config.json has the token in it, start homebridge again, and look for entries from the homebridge startup like:
        [3/17/2018, 3:57:25 PM] [Nest] Initializing Nest platform...
        [3/17/2018, 3:57:25 PM] [Nest] Fetching Nest devices.
        [3/17/2018, 3:57:27 PM] [Nest] initing thermostat "Dining Room Thermostat (Nest)": deviceId: Ab1ccdeFGhIjkLmNO3pqrSTuVWX4Yzab structureId: AbCDEf1GHIjKLMn-o2pQrSTUVWxYzABcde3F-GhijklM4NoPq_R5st
        [3/17/2018, 3:57:27 PM] [Nest] initing protect "Basement Nest Protect": deviceId: a1bcDeFgH23iJKl_MnO4PQRsTUV5Wxyz structureId: AbCDEf1GHIjKLMn-o2pQrSTUVWxYzABcde3F-GhijklM4NoPq_R5st
        [3/17/2018, 3:57:27 PM] [Nest] Initializing platform accessory 'Dining Room Thermostat (Nest)'...
        [3/17/2018, 3:57:27 PM] [Nest] Initializing platform accessory 'Basement Nest Protect'...

        These are an indication that your new token is working!

    1. Now that homebridge is working properly and logging in correctly to the Nest API, you’ll want to set it up to run as a service at your server’s startup, and also set it up to restart if/when it crashes. For that, I recommend following the blog, “Joe’s Projects” and the entry, “Homebridge, Smart Things, Nest and Insteon”, and his section on “AUTOMATICALLY LAUNCHING HOMEBRIDGE WITH LAUNCHD”, about 1/3 of the way down the blog entry.

This is all I have for you. I hope it’s enough.


Leave a Reply