Skip to main content

Syncing HomeAssistant With GitHub

·3 mins

I use HomeAssistant for my home automation needs. It does a fantastic job, but does require regular editing of config files. Although we can install vim on the server, I’m much more at home editing on my local machine, so I was in search of an easy way to keep config files in sync.

There are a whole host of ways to achieve this, but I settled on what I think is the simplest: a git repository hosted on GitHub. Rather than setting up a git server on the HomeAssistant machine, it’s simpler to manually pull to keep things in sync, but a little less convenient. With a little tweak, though, it works perfectly. Here’s a quick walkthrough.

I’m using Hass.io on HassOS, so locations and installed packages may differ.

I’ve also opted to use the Community SSH addon which persists data in ~/.ssh/.

SSH into the Hass.io box:

ssh root@hassio.local

Generate an SSH key:

ssh-keygen -t rsa -b 4096 -C "hassio" && cat ~/ssh/id_rsa.pub

Copy the output (the key) and add it as a new key on GitHub.

Create a new repo on GitHub.

Now initialize your git repo in /config:

cd /config && git init

Edit your .gitignore so you don’t add anything sensitive or transient to your repo. I ended up with the following:

secrets.yaml
*.log
*.xml
*.db
*.sqlite
.storage/
OZW_Log.txt
harmony_harmony_hub.conf
known_devices.yaml

At this point it would be wise to ensure that you don’t have any sensitive information in your YAML files. If you do, move them over to secrets.

Add everything to and make an initial commit:

git add . && git commit -m "Initial commit"

Push it up to GitHub:

git remote add origin git@github.com:USERNAME/REPONAME.git
git push -u origin master

You should now see that your repo is on GitHub. Now it’s time to pull it down on your local machine.

git clone git@github.com:USERNAME/REPONAME.git

You now have the same git repo on both your local machine and on HomeAssistant, so you can go ahead and edit in your preferred environment on the local machine. After pushing the repo to GitHub, you’ll need to SSH into the HomeAssistant box and run a git pull in /config; that’s a little too inconvenient, though, so what can we do?

Make an alias! cd into the local copy of the repo and:

git config alias.pushpull '!git push && ssh root@hassio.local "cd /config && git pull"'

Now you can make your commits and run git pushpull to both push to GitHub and pull down on HomeAssistant.

> git commit -m "Change sensor platform to buienradar"
[master dc13cef] Change sensor platform to buienradar
 1 file changed, 1 insertion(+), 2 deletions(-)
~/repos/homeassistant-config master
> git pushpull
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 977 bytes | 977.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:shkm/homeassistant-config.git
   7cdd2c1..dc13cef  master -> master
debug1: permanently_set_uid: 0/0
Updating 7cdd2c1..dc13cef
From github.com:shkm/homeassistant-config
   7cdd2c1..dc13cef  master     -> origin/master
Fast-forward
 configuration.yaml | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)