Setup Homebridge to Start on Bootup

I’ve recently been playing with Homebridge on my Raspberry Pi. I’ve setup my Harmony Hub and my wireless power outlets to be controlled by Siri on my iPhone. Overall it has been pretty simple to setup but I did run into an issue trying to get Homebridge to start on bootup. Homebridge suggest 3 different methods

I decided to use systemd to start Homebridge on bootup. I prefered this method because it will restart if an error occurs. I followed the gist setup step by step but ran into some problems.

My main issues were:

  • The location of my homebridge binary. Step 2
  • Permissions were not correct and the service failed to load. Step 7
  • I needed the persist folder in /var/homebridge directory. Step 6

Here are the steps that worked for me:

  1. sudo nano /etc/default/homebridge and paste this gist
  2. sudo nano /etc/systemd/system/homebridge.service and paste this gist
    1. I had to remove local from:  ExecStart=/usr/local/bin/homebridge $HOMEBRIDGE_OPTS  because my homebridge installed in /usr/bin/
  3. Create a user to run service: sudo useradd --system homebridge
  4. sudo mkdir /var/homebridge
  5. sudo cp ~/.homebridge/config.json /var/homebridge/
    1. This copies your current user’s config. This assumes you have already added accessories etc.
  6. sudo cp -r ~/.homebridge/persist /var/homebridge
  7. sudo chmod -R 0777 /var/homebridge
  8. sudo systemctl daemon-reload
  9. sudo systemctl enable homebridge
  10. sudo systemctl start homebridge

Type systemctl status homebridge to check the status of the service.

Hopefully this helps anyone who is having trouble with Homebridge starting on boot on a Raspberry Pi. Please comment below if you have any questions.


Also published on Medium.

129 thoughts on “Setup Homebridge to Start on Bootup

  1. Hi Tim

    I’m sorry to plague you for advice.

    I have homebridge running on boot up of my Raspberry Pi 3 as guided by your instructions above re systemd.

    I was hoping to install a second plugin, in addition to the one governing my LIFX lightbulbs.
    However, when I type:

    npm install -g homebridge-platform-wemo

    I get:

    npm WARN checkPermissions Missing write access to /usr/lib/node_modules
    /usr/lib
    └─┬ [email protected]
    ├─┬ [email protected]
    │ └── [email protected]
    └─┬ [email protected].2
    ├─┬ [email protected]
    │ └── [email protected]
    ├── [email protected]
    ├─┬ [email protected]
    │ └── [email protected]
    ├─┬ [email protected]
    │ ├── [email protected]
    │ └─┬ [email protected]
    │ └── [email protected].4
    └── [email protected]

    npm ERR! Linux 4.4.50-v7+
    npm ERR! argv “/usr/bin/nodejs” “/usr/bin/npm” “install” “-g” “homebridge-platform-wemo”
    npm ERR! node v6.10.1
    npm ERR! npm v3.10.10
    npm ERR! path /usr/lib/node_modules
    npm ERR! code EACCES
    npm ERR! errno -13
    npm ERR! syscall access

    npm ERR! Error: EACCES: permission denied, access ‘/usr/lib/node_modules’
    npm ERR! at Error (native)
    npm ERR! { Error: EACCES: permission denied, access ‘/usr/lib/node_modules’
    npm ERR! at Error (native)
    npm ERR! errno: -13,
    npm ERR! code: ‘EACCES’,
    npm ERR! syscall: ‘access’,
    npm ERR! path: ‘/usr/lib/node_modules’ }
    npm ERR!
    npm ERR! Please try running this command again as root/Administrator.

    npm ERR! Please include the following file with any support request:
    npm ERR! /home/pi/npm-debug.log

    I don’t seem to have permission to install the second plugin. Would you have any advice re how to do so?

      1. Hi Tim

        I’m sorry, I realise that was the equivalent of not trying the plug it out plug it back in approach. Sorry, I’m a newbie!

        Thanks for your help, I’ve managed to get it installed! Would you also be able to tell me how to edit the config.json on my raspberry pi from the command line (I’ve sshed in to a headless system).

        Kind regards

        Tom

  2. Hey Tim,

    it just seems to have worked without me evening editing the config file! I can see my wemo devices and they all work! I’m not entirely sure what to make of that, but I’m happy!

    Thanks for your help!

  3. Hi Tim,

    I followed this and was able to get it working! but only temporarily. If I run `systemctl status homebridge` a few times, I can see that it starts up for a couple seconds, but then gets killed and is usually in the auto-restart state.

    Did you ever run into this problem?

      1. I found a fix, which was to comment out the `User=homebridge` in the homebridge.service file.

        I had my homebridge working as an accessory in HomeKit before, when I ran `sudo homebridge`. But when it is running as a service, my HomeKit app does not find the accessory. Is this a different instance of homebridge?

          1. It was something relating to the config.

            I think the problem was that the copied config.json from my non-bootup setup had the same bridge name/username/HomeKit PIN. I’m not sure which of the above it is, but I changed all 3 to be a new unique value, added that new accessory to HomeKit, and it works now.

  4. Hi Tim thought I was doing so well.
    I got Homebrige running but did not put in any accessories. Then went and got half way through start on boot and realised with no accessories i needed to stop.
    Tried to load an accessory and getting an error.

    Think I probably need to start all over again even though Hombridge is running

    npm install -g homebridge-gpio-garagedoor
    npm WARN checkPermissions Missing write access to /usr/lib/node_modules
    /usr/lib
    └─┬ [email protected]
    ├── [email protected]
    └─┬ [email protected]
    └─┬ [email protected]
    ├── [email protected]
    └── [email protected]

    npm ERR! Linux 4.4.50-v7+
    npm ERR! argv “/usr/bin/nodejs” “/usr/bin/npm” “install” “-g” “homebridge-gpio-garagedoor”
    npm ERR! node v7.8.0
    npm ERR! npm v4.2.0
    npm ERR! path /usr/lib/node_modules
    npm ERR! code EACCES
    npm ERR! errno -13
    npm ERR! syscall access

    npm ERR! Error: EACCES: permission denied, access ‘/usr/lib/node_modules’
    npm ERR! { Error: EACCES: permission denied, access ‘/usr/lib/node_modules’
    npm ERR! errno: -13,
    npm ERR! code: ‘EACCES’,
    npm ERR! syscall: ‘access’,
    npm ERR! path: ‘/usr/lib/node_modules’ }
    npm ERR!
    npm ERR! Please try running this command again as root/Administrator.

    npm ERR! Please include the following file with any support request:
    npm ERR! /home/pi/.npm/_logs/2017-04-11T12_48_18_707Z-debug.log
    [email protected]:~ $ /home/pi/.npm/_logs/2017-04-11T12_48_18_707Z-debug.lo
    -bash: /home/pi/.npm/_logs/2017-04-11T12_48_18_707Z-debug.lo: No such file or directory
    [email protected]:~ $ systemctl status homebridge
    ● homebridge.service – Node.js HomeKit Server
    Loaded: error (Reason: Invalid argument)
    Active: inactive (dead)
    [email protected]:~ $ systemctl status homebridge
    ● homebridge.service – Node.js HomeKit Server
    Loaded: error (Reason: Invalid argument)
    Active: inactive (dead)

      1. Hi the sudo command as you suggested has moved me forward.

        I presume the line
        [4/13/2017, 1:37:25 PM] config.json (/home/pi/.homebridge/config.json) not found.

        Is now what is causing the problem.
        The accessory is showing .

        Appreciate your input.
        Regards

        Tony
        [email protected]:~ $ homebridge
        *** WARNING *** The program ‘nodejs’ uses the Apple Bonjour compatibility layer of Avahi.
        *** WARNING *** Please fix your application to use the native API of Avahi!
        *** WARNING *** For more information see
        *** WARNING *** The program ‘nodejs’ called ‘DNSServiceRegister()’ which is not supported (or only supported partially) in the Apple Bonjour compatibility layer of Avahi.
        *** WARNING *** Please fix your application to use the native API of Avahi!
        *** WARNING *** For more information see
        [4/13/2017, 1:37:24 PM] Loaded plugin: homebridge-gpio-garagedoor
        [4/13/2017, 1:37:25 PM] Registering accessory ‘homebridge-gpio-garagedoor.GPIOGarageDoor’
        [4/13/2017, 1:37:25 PM] —
        [4/13/2017, 1:37:25 PM] config.json (/home/pi/.homebridge/config.json) not found.
        Scan this code with your HomeKit App on your iOS device to pair with Homebridge:

        ┌────────────┐
        │ 031-45-154 │
        └────────────┘

        [4/13/2017, 1:37:25 PM] Homebridge is running on port 46058.

          1. hi just reinstalled using sudo npm install -g –unsafe-perm homebridge

            Still same error
            config.json (/home/pi/.homebridge/config.json) not found

  5. Hi Tim sorry to keep sending so many posts but making progress.

    I put a config.json file in the home bridge folder and now get this error.

    rageDoor’
    [4/13/2017, 2:48:35 PM] —
    [4/13/2017, 2:48:35 PM] There was a problem reading your config.json file.
    [4/13/2017, 2:48:35 PM] Please try pasting your config.json file here to validate it: http://jsonlint.com
    [4/13/2017, 2:48:35 PM]
    /usr/lib/node_modules/homebridge/lib/server.js:197
    throw err;

    The config file comes from https://www.npmjs.com/package/homebridge-gpio-garagedoor

    And is

    “accessories”: [
    {
    “accessory”: “GPIOGarageDoor”,
    “name”: “Garage Door”,
    “doorSwitchPin”: 23,
    “doorSensorPin”: 24,
    “isNCSensor”: false,
    “doorOpensInSeconds”: 14
    }
    ]

    Maybe I am not copying it in correctly?

    Kind regards

    Tony

    1. Sounds like it’s not correct json format. Look at the sample and make sure you are not missing anything. Validate it using a json parser

  6. Hi Tim, Im newbie here and just managed to finish all the setup. but can you provide the result of “systemctl status homebridge”, Im a bit confused here whether my systemd bootup is running or not.

    Also I configure my rasp-pi 3 with ssh on mac, when I run $ homebridge in the terminal, its running successfully. What should i do to keep it running because when i quit the terminal (Command+q), the homebridge hub in my iphone home app has no response which means it stopped running. Is there some command that I miss here?

    Thanks for the help…

          1. So the login page. I would recommend going through the steps again to see if you missed anything. You shouldn’t have to login. It should start automatically.

  7. This worked perfectly for me.

    Just one question: now when I enter $ homebridge it gives me the error that homebridge is already in use (which of course it is). No problem, except what happens when I want to make a change to homebridge? How do I kill the process and start it again manually in a new command line window?

    Thanks for the help!

          1. To stop homebridge service type “systemctl stop homebridge” to start the service again type “systemctl start homebridge”

  8. Hi! Great tutorial but I messed some stuff up and I think I have multiple instances of Homebridge running now and it’s not working properly. How do I remove homebridge and reinstall it now that I have it running on startup?

Leave a Reply

Your email address will not be published. Required fields are marked *