SmartThings, Home Assistant, and MQTT Setup

Hello,

I just went through the process of migrating HA from a VM to a Raspberry Pi, which means I had to set up SmartThings integration again. It’s slightly different on a Pi due to the architecture so I figured I’d share the steps involved. Please keep in mind that these steps might be slightly different for other users depending on the mish-mash of instructions that are out there. The assumption is that you already have HA configured outside of the ST/MQTT requirements.

I’m running a clean build of Raspbian and installed HA on top of that using the virtualenv instructions. Please note that I don’t have most of these set to autostart at boot yet, so you’re on your own for figuring that out.

Step 1: Installing the SmartThings bridge.
This is what allows SmartThings and HA to speak via MQTT. I use the Docker method since it just works and I don’t have to worry about other dependencies.

First, install Docker from their repo. The build included in Raspbian is outdated.

curl -sSL https://get.docker.com | sh

I added the “homeassistant” user to the Docker group so I wouldn’t need to use sudo for Docker configuration. If you don’t add the user, just preface the Docker commands with “sudo” for them to work properly.

Next, you’ll need to rebuild the Docker instance of smartthings-mqtt-bridge. The default build does not seem to work. I didn’t really dig into it, but I’m guessing it’s due to architecture requirements.

docker build -t smartthings-mqtt-bridge -f Dockerfile-rpi https://github.com/stjohnjohnson/smartthings-mqtt-bridge.git

docker run -d --name="mqtt-bridge" -v /opt/mqtt-bridge:/config -p 8080:8080 smartthings-mqtt-bridge

You’ll then need to edit the file /opt/mqtt-bridge/config.yml. Change the value Host to include to IP address of your Pi, i.e. the IP on wlan0. I had issues trying to use localhost, but the actual IP worked every single time. For example: Host 10.10.10.10 in the config.

At this point you need to restart the Docker instance:

docker restart mqtt-bridge

Step 2: Install the MQTT broker.
I use Mosquitto as the broker since I was having problems with the native HA broker. It’s in the Raspbian repo and works with minimal configuration.

sudo apt install mosquitto

You then need to change a few settings. I did this by with sudo vi /etc/mosquitto/conf.d/99-local.conf and adding the following entries to the file:

allow_anonymous true
persistence true

Please note that you’d want authentication if this is exposed to the Internet. Yes, anonymous isn’t the best security policy, but I’m not too concerned on my local network. You can then restart Mosquitto:

sudo service mosquitto restart

Step 3: Enable MQTT in HA.

There’s not much to this. Open up HA’s configuration.yaml file and add the following:

mqtt:
  broker: localhost
  discovery: true
  discovery_prefix: smartthings

Please note that it’s safe to use “localhost” here, unlike the mqtt-bridge code.

Step 4: Enable SmartThings Integration.

The instructions for this are pretty complete, minus the Pi compatibility issues that I saw in the first steps. You just need the to follow the sections “SMARTTHINGS DEVICE” and “SMARTTHINGS APP” in the guide. Once you have the app enabled, you should start seeing MQTT events in the HA log.

Hope this helps!

3 Likes

Thanks for this article. I was struggling by following instructions at https://home-assistant.io/blog/2016/02/09/Smarter-Smart-Things-with-MQTT-and-Home-Assistant/. Your article makes it easier.

One question, it seems I always have to run the following command every time I reboot my Pi.
sudo docker start mqtt-bridge

Is there any way to do this automatically on system reboot? I have HA and Mosquitto running as standalone and the bridge running in Docker.

Also, I have noticed quite a delay (about 3 to 5 secs) in any action from SmartThings or HA, like switching on/off a switch. Is this expected behavior?

Thanks!

Ok so I followed your instructions to the T 2 different times with a fresh install, but I can not get HA to receive ST devices/updates.

The only step i did not do is:
“I added the “homeassistant” user to the Docker group so I wouldn’t need to use sudo for Docker configuration. If you don’t add the user, just preface the Docker commands with “sudo” for them to work properly”
because i was unsure how to.

My config.yml contains:
mqtt:
host: mqtt://127.0.0.1
port: 8080
*tried both localhost and IP, with and without mqtt://

events.log
2017-06-14T20:08:11.065Z - info: Connecting to MQTT at mqtt://localhost
2017-06-14T20:10:39.101Z - info: Starting SmartThings MQTT Bridge - v1.3.3
2017-06-14T20:10:39.121Z - info: Loading configuration
2017-06-14T20:10:39.170Z - info: Loading previous state
2017-06-14T20:10:39.178Z - info: Perfoming configuration migration
2017-06-14T20:10:39.202Z - info: Saving current state
2017-06-14T20:10:39.221Z - info: Connecting to MQTT at mqtt://127.0.0.1
2017-06-14T20:13:16.312Z - info: Starting SmartThings MQTT Bridge - v1.3.3
2017-06-14T20:13:16.329Z - info: Loading configuration
2017-06-14T20:13:16.377Z - info: Loading previous state
2017-06-14T20:13:16.382Z - info: Perfoming configuration migration

When i start/stop/restart mqtt-bridge via: “sudo docker restart mqtt-bridge” my only response is “mqtt-bridge”, not sure if there should be more or not.

When I stop mqtt-bridge, i dont see anything in HA logs

When I restart mosquitto i do see
Jun 14 14:12:31 HomeAss hass[8491]: 2017-06-14 14:12:31 WARNING (Thread-11) [homeassistant.components.mqtt] Disconnected from MQTT (1). Trying to reconnect in 1 s
Jun 14 14:12:32 HomeAss hass[8491]: 2017-06-14 14:12:32 INFO (Thread-11) [homeassistant.components.mqtt] Successfully reconnected to the MQTT server

Smart things logs show the following when i change device state:
“MQTT Bridge sent deviceNotification command to MQ Bridge”
but i not sure if Pi is receiving it.

to the end of comfiguration.yaml I added:
mqtt:
broker: localhost
discovery: true
discovery_prefix: smartthings

I didn’t add devices yet but i assumed i would see something in the HA log showing its receiving data.

I ran
" mosquitto_pub -h 127.0.0.1 -t home-assistant/switch/1/on -m “Switch is ON”"
but i didn’t see anything in HA log

Any help would be greatly appreciated, not sure what to look at, where i went wrong.
Nathan

Does this eliminate ST cloud completely? or is it still used?

Unfortunately not. Even if all the devices run locally, the MQTT Bridge will always run in the ST Cloud.
So far there is no solution that i know off to make a custom smartapp / device handler run locally

Same issue, were you able to fix?

Sorry if this is already asked and answered.
Is this two ways?

Turn on a light in ST is refelcted in HA
Turn of the same light in HA and it switches of in ST?

Regards

For anyone getting a "cgroups: memory cgroup not supported on this system"with this like I was.

You need to add ‘cgroup_enable=memory cgroup_memory=1 swapaccount=1’ to the end of /boot/cmdline.txt and reboot.

see this github thread: https://github.com/moby/moby/issues/35587

I’ve some problem also here with mqtt bridge.
When i load this ```
docker run
-d
–name=“mqtt-bridge”
-v /opt/mqtt-bridge:/config
-p 8080:8080
stjohnjohnson/smartthings-mqtt-bridge


Iv'e a permission problem:
 docker run \
> --name="mqtt-bridge" \
>     -v /opt/mqtt-bridge:/config \
>     -p 8080:8080 \
>     stjohnjohnson/smartthings-mqtt-bridge
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/create?name=mqtt-bridge: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.


Any help?
Thanks in advance,


Alessandro

Maybe you are not using sudo? I’m not sure but maybe need more rights for that command
sudo docker run
unless you ran all this under sudo su
Hope that helps and not hurts :wink: