Setting up OwnTracks with integrated MQTT Broker

Not sure where to put this and the instructions are a bit disjointed, so I’ll document it here for future reference, resolving some of the confusion I had along the way.

I’m working from the assumption here you are already tracking devices using some form of In-Home only presence monitoring like UniFi or ping, but if not it’s no big deal (you just won’t have an existing device to merge in the known_devices.yaml step.

Step 1 - Enable MQTT and OwnTracks in Home Assistant

Firstly add the following line to configuration.yaml

mqtt:

The device_tracker: section needs to be changed to support the multiple platforms

device_tracker:
  - platform: owntracks
  - platform: unifi
    host: 127.0.0.1
    username: hass
    password: pass
    verify_ssl: false
  - platform: bluetooth_tracker

or alternatively if you are not already locally tracking presence simply add

device_tracker:
  - platform: owntracks

to configuration.yaml

Now check your configuration, and restart Home Assistant.

Step 2. Configure Port Forwarding in your Router

You will need to configure port forwarding on your router so that port 1883 on your Public IP address hits port 1883 on your Home Assistant Box (i.e. your MQTT Broker). How you achieve this will depend on your router.

On the UniFi Controller the configuration will be similar to the following (with the correct IP Address of your Home Assistant Box entered) :-

If you do not already have a static IP Address or Dynamic DNS service setup you will need to do so now - either in Home Assistant (e.g. DuckDNS) or in your Router (e.g. I’m using DNSOMatic on my UniFi USG Router)

Step 3. Install Own Tracks

Install OwnTracks from the Google Play Store

Step 4. Configure Own Tracks to use MQTT Version 4

You will now need to configure OwnTracks to talk in the correct version of MQTT - this is a step that threw me for a while but eventually I found the comments at https://www.home-assistant.io/docs/mqtt/broker#owntracks pointed me in the right direction

  • Open OwnTracks
  • Run through the setup wizard selecting “Private MQTT” when prompted and authorising Location permissions when prompted
  • Inside OwnTracks open Configuration Management
  • Tap the triple dot menu and tap Editor
  • In Key start typing mqttProtocolLevel and tap it when it appears in the list
  • In Value enter 4
  • Restart OwnTracks

NB You must restart Own Tracks - the easiest way is to reboot your phone, but you can also Force Stop in Android Settings > Applications (exact location varies depending on phone manufacturer / Android version)

Step 5. Configure OwnTracks to talk to Home Assistant

If you have not already done so configure your api_password in the http component

  • Launch OwnTracks again
  • Hamburger Menu
  • Preferences
  • Connection
  • Host
    • Host = Your Public IP address or Domain Name - e.g. homeassistant.example.com
    • Port = 1883
  • Identification
    • Username = homeassistant (NB this MUST be homeassistant)
    • Password = your api_password
    • Device ID = kevs_pixel2 (NB this is your friendly name of the device and will be used as part of the entity ID so make it memorable)
    • Tracker ID = p2 (This is displayed on the OwnTracks Map as the icon text, but is limited to two letters - your initials may be a good choice here)
  • Security
    • TLS = Off

Tap the Tick at the top of the configuration screen and with any luck the “OwnTracks” notification will now show your address.

Step 6. Configure known_devices.yaml

You will probably now have two devices in known_devices.yaml - your existing home presence detection and a newly created one from OwnTracks called homeassistant

000000ffffff:
    hide_if_away: false 
    icon:
    mac: 00:00:00:FF:FF:FF 
    name: Kev's Nexus 7 
    picture:  
    track: false 
    vendor: ASUSTek COMPUTER INC.
    
homeassistant_nexus7:
  hide_if_away: false
  icon:
  mac:
  name: homeassistant
  picture:
  track: true
  vendor:

You need to merge these into a single entry.
Easiest way is to copy the key line (i.e. homeassistant_nexus7:) and paste it over the key line of the already configured devices 000000ffffff: in this example. The delete the rest of the now untitled block.

This should leave you with something similar to the following:-

homeassistant_nexus7:
    hide_if_away: false 
    icon:
    mac: 00:00:00:FF:FF:FF 
    name: Kev's Nexus 7 
    picture:  
    track: false 
    vendor: ASUSTek COMPUTER INC.

NB If you simply copy the mac address to the homeassistant_nexus7: as I did originally your original entry will now always be “not_home” and ignored, while only the homeassistant_nexus7: gets correctly updated.

Step 7. Configure some zones

zone:
  - name: University
    latitude: 52.9572876
    longitude: -1.1544187
    radius: 300
    icon: mdi:school
  - name: Wayne Manor
    latitude: 52.9478537
    longitude: -1.2096445
    radius: 150
    icon: mdi:home

Check the config once again and restart Home Assistant.

Your device will now have multiple states to report

  • Home (Detected when you are connected to your home network, e.g. via the UniFi Controller)
  • University (When you are within 300 meters of the latitude / longitude entered for this zone i.e. Nottingham Trent University City Campus)
  • Wayne Manor (When you are within 150 of the latitude / longitude entered for this zone i.e. Wollaton Hall - Wayne Manor in The Dark Knight Rises FWIW )
  • Not Home (Anywhere else)

et voila, OwnTrack + Integrated MQTT in HASS!

NB - at the time of writing the Integrated MQTT broker is a little buggy and OwnTracks may get stuck “Connecting” - when it does so you need to restart your Home Assistant - the home-assistant.log file will be filled with multiple lines of Client homeassistant_nexus7 is reconnecting too quickly, make it wait

2 Likes