Smartthings MQTT Bridge

Could it be your indentation? I don’t know if your config is setup the same as it is in this post, but this won’t work.

I tried using mosquitto but faced weird barriers and errors so I decided to go with the embedded broker instead.

As for my indentation, I think it just got messed up during the copy/paste to this thread, as I’m using what was already templated for me in the file.

Are you sure your MQTT broker is working properly? If you used the AIO installer mosquitto is also installed by default and is not too bad to get going. I had a permission problem with my pwfile but got it sorted. Here are my notes for setting it up from a fresh install:

Edit mosquitto config
sudo nano /etc/mosquitto/mosquitto.conf

Mosquitto Config

delete all text in the file and replace with this:

allow_anonymous false password_file /etc/mosquitto/pwfile listener 1883 listener 9001 protocol websockets user mosquitto persistence true persistence_file mosquitto.db persistence_location /var/lib/mosquitto/ pid_file /var/run/mosquitto.pid

Create password file (user pi), this destroys any existing file:
sudo mosquitto_passwd -c /etc/mosquitto/pwfile pi

If you want to add users to existing password file:
sudo mosquitto_passwd /etc/mosquitto/pwfile username

Test mosquitto config for errors:
mosquitto -c /etc/mosquitto/mosquitto.conf

I had an error where I didn’t have permission to access the pwfile. I had to killall mosquitto then delete the pwfile in samba, then create a new one for this to work. Once you pass the test above, reboot and test that the systemd instance is loading and running properly:

Test mosquitto systemctl process for errors
sudo systemctl status mosquitto.service

Test mosquitto- subscribe & publish: open two terminal windows and issue these commands in different windows:
mosquitto_sub -d -u pi -P raspberry -t dev/test
mosquitto_pub -d -u pi -P raspberry -t dev/test -m "Hello world"

To view all messages coming through the broker, subscribe to all messages:
mosquitto_sub -u pi -P raspberry -v -t '#'

To see even more details, stop the systemd instance and run just a verbose instance (note, this will not load the preference file)

mosquitto -v

1 Like

I just remembered I had this exact same issue when I migrated from a Pi1 to a Pi3. I forgot to update the MAC address in the ST IDE MQTT-Bridge device config options. After I made the changes, it still wasn’t working. I had to delete the SmartApp instance and then setup a new one. Maybe that’s your problem.

1 Like

I was thinking that as well.

Here are the steps I took on my last build. Hopefully they’re useful to someone.

Build Image from latest raspian and update it

sudo apt-get update; sudo apt-get upgrade

Install All In One homeassistant installer

wget -Nnv https://raw.githubusercontent.com/home-assistant/fabric-home-assistant/master/hass_rpi_installer.sh && chown pi:pi hass_rpi_installer.sh && bash hass_rpi_installer.sh

Reset mosquitto password before we get started. For this example, use username pi and password mqttpass.

sudo mosquitto_passwd /etc/mosquitto/pwfile pi
sudo systemctl restart mosquitto.service

Setup smarthings-mqtt-bridge via npm

sudo apt-get install npm
sudo npm install -g smartthings-mqtt-bridge

setup pm2 to run the processes

sudo npm install pm2 -g

create and edit smartthings-mqtt-bridge config

sudo cp /usr/local/lib/node_modules/smartthings-mqtt-bridge/_config.yml ~/config.yml
sudo nano ~/config.yml

mqtt-bridge-configuration ~/config.yml

mqtt:
  # Specify your MQTT Broker's hostname or IP address here
  host: mqtt://localhost
  # Preface for the topics $PREFACE/$DEVICE_NAME/$PROPERTY
  preface: smartthings

  # Suffix for the state topics $PREFACE/$DEVICE_NAME/$PROPERTY/$STATE_SUFFIX
  # state_suffix: state
  # Suffix for the command topics $PREFACE/$DEVICE_NAME/$PROPERTY/$COMMAND_SUFFIX
  # command_suffix: cmd

  # Other optional settings from https://www.npmjs.com/package/mqtt#mqttclientstreambuilder-options
  username: pi
  password: mqttpass

  # Port number to listen on
  port: 8080

Configure the SmartThings Device handler, Device, and SmartApp using steps outlined in https://github.com/stjohnjohnson/smartthings-mqtt-bridge1

Device Settings
IP: raspberry pi IP
MAC: Raspberry pi MAC
Port: 8080

Add mqtt to HomeAssistant config configuration.yaml

mqtt:
  broker: localhost
  port: 1883
  client_id: home-assistant-1
  username: pi
  password: mqttpass

Add switches to their own file, or configuration.yaml

- platform: mqtt
  name: "MQTT Dining Room Light"
  state_topic: "smartthings/Dining Room Light/switch"
  command_topic: "smartthings/Dining Room Light/switch"
  payload_on: "on"
  payload_off: "off"
  retain: true

start the bridge

pm2 restart smartthings-mqtt-bridge

restart HASS

sudo systemctl restart home-assistant.service

That should do it. You may want to sudo reboot

6 Likes

I’ve been following this thread and @AceoStar instructions as above but I’ve hit a brick wall. I’m running Hassbian and all is set up as above, but running ‘pm2 logs smartthings-mqtt-bridge’ results in ‘Error: listen EADDRINUSE :::1883’. This ‘sudo lsof -i TCP:1883’ results in the following;

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mosquitto 490 mosquitto 3u IPv4 10345 0t0 TCP *:1883 (LISTEN)
mosquitto 490 mosquitto 4u IPv6 10346 0t0 TCP *:1883 (LISTEN)
mosquitto 490 mosquitto 5u IPv6 41596 0t0 TCP localhost:1883->localhost:56404 (ESTABLISHED)
hass 14259 homeassistant 7u IPv6 42713 0t0 TCP localhost:56404->localhost:1883 (ESTABLISHED)

My level of experience on this is fairly limited and if anyone could suggest how I can permanently remedy the problem, I’d appreciate the help. I can kill some or all of the PIDs but the problem returns upon reboot. If I had hair, it’d be torn out by now.

Problem resolved!
Funnily enough, when I said “all is set up as above” I was entirely wrong and had the wrong port in the config.yml
Thanks for all who contributed here. Much appreciated.

That’ll get ya every time! Glad its working out :slight_smile:

Hey this actually came at the perfect time because I have been meaning to do this for the longest time but have not been able to until now! I am following what @AceoStar has posted above and I am coming across a couple of problems though, so maybe you guys could help me with that.

When I try to run “pm2 logs smartthings-mqtt-bridge” I get an error saying that smartthings-mqtt-bridge was not found. I try running “sudo npm install -g smartthings-mqtt-bridge” again and I am also able to edit and view the config file that is made from it. Any ideas on what was wrong? Also, how can I ensure that mosquitto is running properly? I am a complete newbie, so any and all ideas and steps are much appreciated :slight_smile:

@AceoStar: This is a great tutorial. I want to add my small contribution, as it isn’t something I’ve seen written anywhere.

Make PM2 start the MQTT-bridge on boot
(only tested on the Pi AIO installer)

First make sure the bridge is running in PM2:
pm2 start smartthings-mqtt-bridge

Save the process to the dump file:
pm2 save

Set PM2 to start on boot:
pm2 startup -u pi

I received this message:
[PM2] Init System found: systemd [PM2] You have to run this command as root. Execute the following command: sudo env PATH=$PATH:/usr/local/bin pm2 startup systemd -u pi --hp /home/pi

So execute the command:
sudo env PATH=$PATH:/usr/local/bin pm2 startup systemd -u pi --hp /home/pi

I hope this works for you guys, let me know if there’s a better way!

1 Like

@hoffsta Thanks for your post! Would that help the situation I am in as well? I’m not with my pi to try it at the moment

Before running pm2 logs smartthings-mqtt-bridge are you running pm2 start smartthings-mqtt-bridge?

First thing I’d do is look in /usr/local/bin and see if pm2 and smartthings-mqtt-bridge are installed there. I had an issue with my version of NodeJS not installing packages to that location. I had to remove and reinstall node and npm from a different method.

As for mosquitto, you can do a quick test. Open two ssh windows, in one, type:
mosquitto_sub -d -u username -P password -t dev/test

In the other type:
mosquitto_pub -d -u username -P password -t dev/test -m "Hello world"

Does “Hello World” pop up in the first window?
Of course this depends on you having created a password file in mosquitto. If you haven’t, omit the -u and -P arguments.

@hoffsta oh wow I never even knew I had to start running smartthings-mqtt-bridge. I was following @AceoStar step for step with everything that he wrote, when I get home with the pi I will try starting it and let you know how it goes.

Also another quick question: since I’m installing everything on my pi, when it says
` mqtt:

Specify your MQTT Broker’s hostname or IP address here

host: mqtt://localhost`

do I replace all instances of “localhost” with the IP address of my pi?

You shouldn’t need to if you’re on the same Pi. “localhost” ‘127.0.0.1’ or the actual IP address should all work.

@hoffsta Thank you so much for your help! I got it working! Your post about having it run on reboot actually came in very handy today, so thank you very much. How am I able to add a slider so that I could change the brightness of a lightbulb?

Awesome, glad I could help!

light:
  platform: mqtt
  name: "Bedroom Light Switch"
  state_topic: "smartthings/Bedroom Light Switch/switch"
  command_topic: "smartthings/Bedroom Light Switch/switch"
  brightness_state_topic: 'smartthings/Bedroom Light Switch/level'
  brightness_command_topic: 'smartthings/Bedroom Light Switch/level'
  brightness_scale: 100
  qos: 0
  payload_on: "on"
  payload_off: "off"
  optimistic: false

You won’t see the brightness slider on the front end unless you click into the card for the light. Look into building custom input sliders if you want to see that instead of the on/off switch. I haven’t worked on any of that yet.

Thank you! It works exactly how I always wanted it to. Thank you very much

1 Like

I have been trying to setup the bridge. I have followed @AceoStar steps. When I enter pm2 start smartthings-mqtt-bridge I get /usr/bin/env: node: No such file or directory would anyone understand why? Or what I am doing wrong?

sudo ln -s “$(which nodejs)” /usr/local/bin/node

2 Likes