HOWTO: Using OwnTracks (with auth) and the MQTT-Bridge for SmartThings

Setting up OwnTracks with 2 MQTT (mosquitto) brokers bridged for use with the MQTT bridge for SmartThings

I successfully tied in OwnTracks to HASS and SmartThings while using authentication for external access.

The MQTT bridge doesn’t support authentication so I set up 2 MQTT instances.

Here are the steps I performed… Hopefully it saves someone else some time.

It seems to be working pretty well but if anyone sees something incorrect in my config, please let me know.
This is my first real interaction with MQTT so if anyone seems something incorrect in my config, please let me know.

Here’s a summary of my setup

2 Docker instances of MQTT
– 1 for internal use (the MQTT bridge for SmartThings - no authentication)
– 1 for external use ( with authentication - for OwnTracks)

All Docker config files are on my NAS so the docker can be destroyed with affecting my actual config files.

Docker setup for the mosquitto internal instance… no auth… for use with the mqtt bridge

docker run -ti -p 1883:1883  \
-v /volume1/data/mosquitto-int/config:/mqtt/config:ro \
-v /volume1/data/mosquitto-int/log:/mqtt/log \
-v /volume1/data/mosquitto-int/data/:/mqtt/data/ \
--name mosquitto-int -d toke/mosquitto

Docker setup for the mosquitto external instance… with auth… for use with the owntracks

docker run -ti -p 1884:1883  \
-v /volume1/data/mosquitto-ext/config:/mqtt/config:ro \
-v /volume1/data/mosquitto-ext/log:/mqtt/log \
-v /volume1/data/mosquitto-ext/data/:/mqtt/data/ \
-v /volume1/data/mosquitto-ext/etc:/etc/mosquitto \
--name mosquitto-ext -d toke/mosquitto

Here are the config files:

/volume1/data/mosquitto-int/config/mosquitto.conf

connection mosquitto-ext
persistence_file mosquitto.db
try_private true
address 10.0.0.20:1884
start_type automatic
username test
password test
notifications true
topic owntracks/# in
log_type all                                              
log_dest file /mqtt/log/mqtt.log 
log_facility 5

/volume1/data/mosquitto-ext/config/mosquitto.conf

connection mosquitto-int
persistence_file mosquitto.db
try_private true
address 10.0.0.20:1883
start_type automatic
username test
password test
notifications true
topic owntracks/# out
log_type all                                              
log_dest file /mqtt/log/mqtt.log
log_facility 5
allow_anonymous false
password_file /etc/mosquitto/pwfile

create a password for mosquitto-ext

docker exec -it mosquitto-ext /bin/bash

cd /etc/mosquitto/

mosquitto_passwd -c /etc/mosquitto/pwfile <userID>

Config in HASS

from:   https://home-assistant.io/components/device_tracker.owntracks/

# Example configuration.yaml entry
device_tracker:
  platform: owntracks
  max_gps_accuracy: 200
# Example in known_devices.yaml
mqttuserid_deviceid    <---- <part of EntityID as seen in <hostname/dev-state>:   ie:  device_tracker.<this part>
  name: <Name to appear on map>
  mac: <MAC address>
  picture:
  track: yes
  hide_if_away: no

OwnTracks settings - for Android

Preferences / Connection / Mode - Private MQTT

Preferences / Connection / Host

Fill out the following:
Host
Identification
Security: TLS disabled

Preferences / Connection / Identification

Authentication (enable)
Username (username entered with mosquitto_passwd command)
Password (password entered with mosquitto_passwd command
Device ID (will be used in the Entity ID in HASS)
Tracker ID (will be used for object tracking in MQTT)

Confirm messages are making it to your server

mosquitto_sub -h <hostname> -p <port> -v -t "owntracks/#" -u <username> -P <password>

Output should appears as follows:

owntracks/<username>/<Device ID> {"_type":"location","acc":22,"batt":60,"lat":<lat>,"lon":<lon>,"tid":"<Tracker ID>","tst":1472491902}

Using Tasker to monitor and restart OwnTracks when it crashes

I’ve found that OwnTracks will periodically crash so I’ve used Tasker to monitor and restart when this happens

Profile screen

Enter an interval to check for the OwnTracks process

Task screen

Task - Step 1

Task - Step 2

Task - Step 3

Task - Step 4 - (Optional but used to track when Tasker restarts OwnTracks)

Task - Step 5
Add the End If

Be sure to save you changes by backing out to Exit and Selecting “Save First”

Note: Location images do not show up on the map when the entities are home

6 Likes

That’s a lot of hard work… Thanks for taking the time to document and share it with the community!

1 Like

I’ve updated the instructions to include additional configuration info as well as how to confirm messages are making it to your MQTT broker

1 Like

WOW this is really detailed. I’m going to tag @fabaff on this one in case he might consider it for a blog posting. Nice work, man! :+1:

He helped me get it added to the Examples page but I don’t have edit access so I need to update the fork and get it submitted. It’s easier to update this post directly.

Here’s the example (minus my latest updates)

1 Like

The Pull request for this is available at https://home-assistant.io/cookbook/owntracks_two_mqtt_broker/ .

It would be good to only maintain one version of this guide. Do you want me to remove the Cookbook entry?

Where would you prefer I keep updated instructions?

Information seems to be split between the main page with Examples and the forums.

I think a wiki type of system would be more open to contributions which would help people find info and help others.

I’ve found that OwnTracks will periodically crash so I’ve used Tasker to monitor and restart when this happens

I’ve added screenshots for the Tasker setup

Thank you for the very good example. I tried to set up the Mosquitto to host Owntracks and control MQTT ESP8266 Sonoffs by following your instruction on Raspberry PI 3 without NAS. I had a problem from the start of the example when I type docker run -ti -p 1884:1883 … from the terminal, in spite of changing the path for config files directly to PI3. But the command stops from the beginning as shown in the attached picture. Are there any procedures that I have to do before I could follow the first line of your instructions?. I have read something about Docker, still I don’t understand. If you have any advice or any comment I would highly appreciate. Thanks a lot.

I run docker on my Synology but I did do the following to install it on my Pi2 for a quick test.

I can’t confirm it works but it does install and returns the version.

Let me know if this works and I’ll add this to my first post.

It is very kind of you, surge919. I succeeded installing Docker version 1.10.3 as you guided above. Now I would follow your instructions on OwnTracks With Two MQTT Brokers. It does not matter if it does not work. At least I could learn something very useful from you. Thanks a lot.

After I successfully installed Docker, Then I have tried to setup config files as shown below but I received the responded message that “Cannot connect to the Docker daemon”. Even though I ran with sudo docker, It still reported the same message. May I ask you a question, After docker installation, what commands should I call to keep Docker daemon running so that I could continue to follow surge919’ s instructions? Please help again, Many thanks.

pi@raspberrypi:~ $ docker --version
Docker version 1.10.3, build 20f81dd
pi@raspberrypi:~ $ docker run -ti -p 1883:1883 \

-v /etc/mosquitto/mosquitto-int/config:/mqtt/config:ro \
-v /etc/mosquitto/mosquitto-int/log:/mqtt/log \
-v /etc/mosquitto/mosquitto-int/data/:/mqtt/data/ \
--name mosquitto-int -d toke/mosquitto

docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See ‘docker run --help’.
pi@raspberrypi:~ $

Sorry to drag this back to the top, but I just got this set up and I can see the data coming in from OwnTracks, but it immediately shuts down HASS’ front end. Meanwhile, there is A LOT of data flowing in from OwnTracks.

Anyone else have this issue after getting set up? Quick glance seems to be that there’s no database for the data to be stored in, since my data directory is empty. Should a database or something be in there to give Mosquitto a place to store all the info and HASS to read from?

First off kudos to @surge919 for this How-To. I am working to port over from Smartthings and in the middle of the HASS setup work right now.

So for this, I am running everything in seperate containers but one thing I couldnt’ get working was to get the mosquitto bridge running and see if HASS woudl automatically created a known_device.yaml file.

After pulling out my hair for a few hours I stumbled on this site: http://www.steves-internet-guide.com/mosquitto-bridge-configuration/

There I found out for what I want, I only need to have the bridge from mosquitto-ext to mosquito-int so the easiest way to do that is the following two mosquitto.conf file:

Note: I am running this in docker running on OS X.

mosquitto-ext/config/mosquitto.conf
connection mosquitto-int address <host ip address>:1883 topic # out 0 "" "" allow_anonymous false password_file /etc/mosquitto/pwfile

mosquitto-int/config/mosquitto.conf
I left this one just a blank file (I found that mosquitto won’t launch with the toke/mosquitto docker image without a file there.

After that I ran a quick test and BAM! I see my Owntracks updates hitting the mosquitto-ext MQTT and then passing that into my mosquitto-int MQTT instance. And the https://home-assistant.io/blog/2016/02/09/Smarter-Smart-Things-with-MQTT-and-Home-Assistant/ is still working great to bridge.

I went with the blank config file for the mosquitto-int image as recommended in the post above and everything seems to be working. I did have one issue, I had to remove these lines from my mosquitto-ext configuration file in order for Docker to start the container.

log_type all                                              
log_dest file /mqtt/log/mqtt.log
log_facility 5

I have an internal MQTT broker. It is working well and is currently running with no username/password or SSL.

I would like to connect an external facing MQTT broker and have a bridge setup between them and have the external facing MQTT republish any topics that it receives back to the internal MQTT server.

The external facing broker is the HASSIO add on and is setup and I can connect to it with MQTT.fx no problems using its external domain name, port 8883 and username password, with SSL/TLS enables on MQTT.fx. I alos have Owntracks publishing to it sucessfully.

I have added the following to my internal MQTT (mosquitto.conf file)

#connection ha
connection bridge-01
address XXX.XXXXXXX.XXX:8883
username user01
password pw01
try_private false
topic # out 0
topic # in 0

where XXX.XXXXX.XX is my FQDN.

However, we I start the internal MQTT broker the following appears in the log file

1513491425: Connecting bridge bridge-01 (XXXX.XXXXXX.XXX:8883)
1513491425: Socket error on client MQTT-PI.bridge-01, disconnecting.

And nothing is re-published. I am at a loss as to why it won’t connect.

The internal MQTT is mosquitto running on rp3 and the external facing broker is mosquitto running on Home Assistant HASSIO.

the settings on HASSIO are

  "plain": false,
  "ssl": true,
  "anonymous": true,
  "logins": [
    {
      "username": "user",
      "password": "pw001"
    }
  ],
  "customize": {
    "active": false,
    "folder": "mosquitto"
  },
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem"
}