MQTT Device Tracker format

How does the MQTT data need to be formatted for the device tracker to pick up and plot on the map? I’ve finally got everything working right in my test so that it pulls the data down, but instead of plotting my phone on the map, or giving a location of Home on the dashboard, it just has this string and nothing on the map:

{" type":“location”,“tid”:“WP”,“acc”:<???>,“lat”:,“lon”:,“tst”:<???>,“batt”:66}

Right now I’m using OwnTracks as a test and I know there is a built in functionality for that, but my goal is to have my electron project send the location of our cars using MQTT and plot those on the map. I assume there’s a specific format or specific tags or something that are not present in the OwnTracks data.

Try the owntracks device tracker platform

I would, but I didn’t see anything on their site where I can do my own code to publish stuff through their service, only use the apps for Android/iOS and the only device I have with either of those platforms is my work phone :confused: My personal phone and my wife’s phone are both running Windows 10.

That’s why I figured I’d just track the cars using the electrons from particle.io and publish the data using MQTT. Maybe if that’s not possible, is there another way to update and manage location data? JSON or something.

Here is a few examples of owntracks reporting back to an MQTT server:

owntracks/homeassistant/andrew {"t":"p","tst":1461590348,"acc":0,"_type":"location","alt":0,"lon":<redacted>,"p":101.3717041015625,"lat":<redacted>,"batt":100,"tid":"AC"}
owntracks/homeassistant/wendy {"tst":1461584027,"acc":65,"_type":"location","alt":24,"lon":<redacted>,"vac":17,"p":101.0577392578125,"lat":<redacted>,"batt":95,"tid":"WC"}
owntracks/homeassistant/andrewipad {"t":"p","tst":1461537070,"acc":0,"_type":"location","alt":0,"lon":<redacted>,"p":101.3188552856445,"lat":<redacted>,"batt":84,"tid":"AC"}
1 Like

So you’re suggesting telling Home-Assistant that I’m using owntracks and sending in data formated like owntracks formats it’s data? What data specifically do I need? I noticed you have altitude and my phone doesn’t. Could I just send in type, latitude, and longitude? Do I need a tid or tst value for it to work?

Yes, OwnTracks has the richest functionality in terms of location reporting so I would suggest that if you don’t want to build your own module the OwnTracks module will be closest to what you want.

In your post you asked “How does the MQTT data need to be formatted for the device tracker to pick up and plot on the map?” - the answer is “it depends”. MQTT is just a lightweight messaging protocol, it does not enforce any particular format it just allows you to exchange strings of data on specific channels. In order for HA to understand and plot the locations, it needs a device_tracker module that understands what the tracker itself is sending and then to take that info and map it to tracker API calls - this is what the owntracks module does, as an example.

Since you are starting fresh, you would usually need to figure out a protocol for your device tracker to report the information you are interested in to MQTT, then build a module in HA to parse that and send it to the location API, however, if you use the existing owntracks module that part is done for you and you just need to implement your piece to look like owntracks.

To answer your specific questions, from my reading of the code, the author helpfully posted this link to explain the owntracks parameters:

I think it would be fine to report the altitude as zero, and I am not sure that the tid is used anywhere in HA. The MQTT path however is relevant - the channel name is munged up to become the name of the device tracker in HA - for instance, my device tracker ID is “device_tracker.homeassistant_andrew”. “homeassistant” is the login I am using on the MQTT server, “andrew” is the owntracks device id, so you would have to differentiate the cars by making sure they published to different topics in that format. The homeassistant part while mapped through could be anything, the keypart is that the channel starts with “owntracks” and that the 2nd and 3rd parts of the channel are mapped together to give the HA device ID.

Hope this helps - good luck.

2 Likes

Wow, yes, thanks :slight_smile: That helps a lot. I should be able to work with this.

I misunderstood and thought the MQTT Device Tracker module was it’s own thing that could pull the data if it was “formatted” just right.

Thank you :smiley:

Ok, here’s where I’m at.

owntracks/WorkPhone/WorkPhone {“_type”:“location”,“tid”:“WP”,“acc”:50,“lat”:20.452652,“lon”:-70.6954585,“tst”:1461720358,“batt”:55}
owntracks/tC/tC {“_type”:“location”,“tid”:“TC”,“acc”:53,“lat”:20.9739265,“lon”:-70.5449338,“tst”:1461720298,“batt”:68}

WorkPhone is my work phone which runs Android and I installed owntracks on it, that’s what it generated.
tC is my car using the Electron to publish.

The workphone works fine, I go into owntracks, force an update, HomeAssistant parses and is happy. When I do an update from the electron, I get this:

Apr 27 01:26:14 raspberrypi hass[24595]: INFO:homeassistant.core:Bus:Handling <Event mqtt_message_received[L]: qos=0, payload={“_type”:“location”,“tid”:“TC”,“acc”:53,“lat”:20.9739265,“lon”:-70.5449338,“tst”:1461720298,“batt”:68}
Apr 27 01:26:14 raspberrypi hass[24595]: ERROR:homeassistant.components.device_tracker.owntracks:Unable to parse payload as JSON: {“_type”:“location”,“tid”:“TC”,“acc”:53,“lat”:20.9739265,“lon”:-70.5449338,“tst”:1461720298,“batt”:68}

Any ideas why it isn’t parsing? They look identical to me :confused:

EDIT: Just wanted to add, Owntracks is actually picking up my entries for the car and working, I’m able to see it on the map in the Owntracks app and it updates. I’m also able to copy the payload from the error message above and paste it into python and it parses fine. Really lost on this one now.

payload = ‘{“_type”:“location”,“tid”:“TC”,“acc”:53,“lat”:20.9739265,“lon”:-70.5449338,“tst”:1461720298,“batt”:68}’
j = json.loads(payload)
print(j)
{‘tid’: ‘TC’, ‘lat’: 20.9739265, ‘tst’: 1461720298, ‘batt’: 68, ‘_type’: ‘location’, ‘acc’: 53, ‘lon’: -30.5449338}

Ok, I’m completely at a loss as to why HomeAssistant won’t parse the JSON? Everything else I try parses it fine, including the python libraries :confused:

Hi

Thanks for your comment/suggestion.

When I use the Owntracks device tracker platform I get my android on HASS after editing the known_devices.yaml where I put my MAC address but it shows I am away even if I am Home and it does not report any location i.e. latitude/longitude nor does it report the battery levels etc.

Can you please help?

Thanks.