MQTT Device Tracker from sensor

Using HASS.Agent and a VPN, I’m exposing a location sensor work my work laptop that holds GPS coordinates. I’d like to create a MQTT Device Tracker from that to use in automations, but I can’t seem to figure out for the life of me what I am doing wrong.

The sensor, sensor.latitude_e5470_geolocation, has the following state: 51.908242362241235,5.534193299322271,0

so I think thought publishing to a MQTT topic like this should do it:

service: mqtt.publish
data:
  topic: homeassistant/location/work_laptop
  retain: true
  payload: |-
    {
      "latitude": "{{ states('sensor.latitude_e5470_geolocation').split(',')[0] |replace("'", '')|replace("[", '')}}",
      "longitude": "{{ states('sensor.latitude_e5470_geolocation').split(',')[1] |replace("'", '')|replace("[", '')}}",
      "battery_level": {{states('sensor.latitude_e5470_battery_charge_remaining_percentage')|int}},
      "gps_accuracy": {{1}}
    }

and creating a configuration.yaml entry like this would sort it:

mqtt:
  device_tracker:
    - name: "laptop_tracker"
      state_topic: "homeassistant/location/work_laptop"
      json_attributes_template: >
        {"other_data":"{{ states('sensor.latitude_e5470_geolocation').split(',')[0] |replace("'", '')|replace("[", '')}}","other_data2":"{{ states('sensor.latitude_e5470_geolocation').split(',')[1] |replace("'", '')|replace("[", '')}}"}
      payload_home: "home"
      payload_not_home: "not_home"

I am doing something really wrong here, but can’t figure out what I should change. Does anyone have any suggestions? I don’t seem to be able to put the GPS attributes in any correct place…

The MQTT device tracker basically allows to tell if a device is “home” or not, through payload_home and payload_not_home.
Nothing more, nothing less…

You can add additional attributes, indeed, and your template should generate 2 attributes, “other_data” and “other_data2”.

But the “homeassistant/location/work_laptop” topic is neither “home” or “not_home”, so I guess the tracker itself is “unknown”.

I seem not to be able to add the GPS coordinates as attributes too:

Doesn’t HA determine based on the coordinates if the device is 'home' or 'not_home' ?

Right. I followed blindly your code, but it doesn’t work that way at all.

Just this should work, if I read the doc properly.

mqtt:
  device_tracker:
    - name: "laptop_tracker"
      json_attributes_topic: "homeassistant/location/work_laptop"

Created a second MQTT in the configuration.yaml:

mqtt:
  device_tracker:
    - name: "laptop_tracker"
      state_topic: "homeassistant/location/work_laptop"
      json_attributes_template: >
        {"other_data":"{{ states('sensor.latitude_e5470_geolocation').split(',')[0] |replace("'", '')|replace("[", '')}}","other_data2":"{{ states('sensor.latitude_e5470_geolocation').split(',')[1] |replace("'", '')|replace("[", '')}}"}
      payload_home: "home"
      payload_not_home: "not_home"
  device_tracker:      
    - name: "laptop_tracker2"
      json_attributes_topic: "homeassistant/location/work_laptop"

Empty device_tracker and no attributes:

It puzzles me. :smiley:

Lat/Lon must be numbers, not strings, as your log probably tells you.

device_tracker:      
  - name: "laptop_tracker2"
    json_attributes_topic: "test-ha/location/work_laptop"

image

Well, that was a very steep learning curve…

…it wasn’t as much as the numbers but to commas in the right place:

service: mqtt.publish
data:
  topic: homeassistant/location/work_laptop
  retain: true
  payload: |-
    {
      "latitude": {{ states('sensor.latitude_e5470_geolocation').split(',')[0] |replace("'", '')|replace("[", '')}},
      "longitude": {{ states('sensor.latitude_e5470_geolocation').split(',')[1] |replace("'", '')|replace("[", '')}},
      "battery_level": {{states('sensor.latitude_e5470_battery_charge_remaining_percentage')|int}},
      "gps_accuracy": {{100}}
    }

Comparing the MQTT payload with your example finally managed to fix it for me! Thanks!

Oh, it definitely doesn’t work if you keep longitude and latitude as strings…
I don’t usually care about this, but I’d suggest to mark my last answer as the solution, as that one might actually help other users, while yours is completely specific to your case.

1 Like

you’re right! Thanks again!

1 Like

Hi:) I have ran into exact the same situation like you - I have a device tracker, which shows a state of latitude, longitude. I also have 2 sensors shows either lat and long values. These values are given to me of a car integration (pulling XPENG data from Enode). I want to show where the car is on a map, but those sensors and device tracker does not have attributes, so it does not work . Thats why I think I need to make a MQTT device tracker, but I can’t figure out setting it up. Do you have a working config, you want to share?