HomeAssistant - IFTTT, Traefik, and Let’s Encrypt

The primary reason I’ve spent all the time getting Traefik and Let’s Encrypt working is so that I could expose HomeAssistant, safely and responsibly, to the internet. Home Assistant (aka HASS) is an open-source home automation platform that can run on a wide-array of ARM and x86 machines, and it backs a lot of the smarts behind my home automation setup (though I’m looking at migrating some of that intelligence to Node-RED). Check my previous post if you need to get Traefik and Let’s Encrypt running.

In this post, I’ll walk through adding an existing HASS install to my Traefik front-end, configuring HASS for external access, and then setting up an IFTTT trigger that sends a call to HASS.

Integrating with Traefik

Because, as covered in a previous post, Traefik is magic, configuring HASS to work with it is as simple as re-deploying it with labels. Traefik monitors the Docker socket for containers labeled to be interesting to Traefik, and responds accordingly — Traefik itself does not need to be restarted to uptake the changes as nginx or a similar reverse proxy would.

If you’re using docker run or create, the flag to set a label is:

–label=

To modify your run/create command for HASS, you’ll need to add the following labels to your command:

--label=”traefik.backend=hass” #This backend should be the name of your hass container, mine is called hass.
--label=”traefik.docker.network=network” # This network should be the network that your hass container normally sits on
--label=”traefik.frontend.rule=Host:hass.avocado.duckdns.org” #This tells Traefik to direct traffic to hass if someone tries to visit this subdomain.
--label=”traefik.enable=true” #This flag is either super-important, telling Traefik to route for this container, or unnecessary, if you changed your Traefik defaults to always do this, which I don’t recommend. Literally, there’s no reason not to use this label.
--label=”traefik.port=8123″ #This is the port the HASS container normally listens on. Note that it is not the host port, which could be something else.
--label=”traefik.default.protocol=http” #Because we’re not going to bother trying to set up HASS with SSL, and instead have Traefik do this, we don’t need to use an https interface on HASS.
Change the values in bold to match your setup. Then move to the next step before turning on your HASS container.

Enabling the front-end

Before turning on the HASS container, you need to set an API password and configure the base URL in the configuration.yaml file for HASS.

Make sure to include the following configuration lines.

http: 
#Uncomment this to add a password (recommended!)   
api_password: !secret api_pass 
#Uncomment this if you are using SSL or running in Docker etc
base_url: hass.avocado.duckdns.org

I like to keep my secrets out of the configuration.yaml file, but if you don’t know how to do this, you can just type it there. It’s important to make sure the base_url matches the one you told Traefik about in the last step.

Now you can turn on your container, and you should be able to visit the front end by visiting that base_url in your browser. If you run into any issues, try using incognito mode in your broweser. before troubleshooting — this exposed some ‘false negatives’ for a few of us that were playing with this.

Integrating with IFTTT

To test this integration, we’ll now go to IFTTT and try to set up an ‘applet’ — we’ll use a simple ‘if you press the button, turn off the Hue lights’

Create a new applet by selecting the button widget (previously the ‘Do’ button) as your trigger:

2018-01-07-18_47_09-Make-an-Applet-IFTTT-300x199.png

For your action, select the Webhooks channel (previously the Maker Channel):

2018-01-07-18_48_17-Make-an-Applet-IFTTT-300x197.png

The documentation on hass.io didn’t seem all that helpful. I think it’s out of date, but I was able to get it working with some Google Fu.

2018-01-07-19_31_23-Turn-off-lights-on-button-press-IFTTT-100x300.png

Fill in your URL first: https://hass.avocado.duckdns.org/api/services/light/turn_off?api_password=avocados

Set the method to POST, the content type to json, and then leave the body filled with:

{“entity_id”: “light.all_hue_lights” }

Save your applet, then give it a test run! Pressing the Do button should shut off all the Hue lights.

Debugging

I had some issues with this part, since the documentation on Hass’ site seemed to be out of date. To debug, I found it helpful to monitor the docker logs as well as the activity tab on IFTTT. If you have malformed API calls, the failures will be best explained from IFTTT’s Activity tab, whereas if you have a malformed payload, tailing the Docker logs will help you suss that out.

The most annoying part, honestly, seems to be figuring out the right REST calls to send, but at the end of this, we have a fully configured Home Assistant, secured to interact with the outside world and take all manner of external interaction. This is just the first step to getting Hass to interact with a plethora of external services, not limited to just IFTTT.

Enjoy!

Written on January 8, 2018