Create mqtt device_tracker based on individual mqtt lat/lon sensors

using an automation:

  - alias: Boss Presence Update from Owntracks
    id: Boss Presence Update from Owntracks
    trigger:
      platform: state
      entity_id:
        - sensor.ot_boss_latitude
        - sensor.ot_boss_longitude
        - sensor.ot_boss_battery_level
        - sensor.ot_boss_accuracy
    condition:
      >
        {{trigger.to_state is not none and
          trigger.from_state is not none}}
    action:
      service: mqtt.publish
      data:
        topic: ha_rpi4/location/theboss
        retain: true
        payload: >-
          {
            "latitude": "{{states('sensor.ot_boss_latitude')}}",
            "longitude": "{{states('sensor.ot_boss_longitude')}}",
            "battery_level": {{states('sensor.ot_boss_battery_level')|int}},
            "gps_accuracy": {{states('sensor.ot_boss_accuracy')|int}}
          }

and an Mqtt_json device_tracker:

device_tracker:

  - platform: mqtt_json
    devices:
      owntracks_theboss_mqtt: ha_rpi4/location/theboss

a correct device_tracker is created.

However, this setup seems a bit convoluted, and I wonder if there really is no more direct way to inject the lat/lon into a device_tracker, possibly without the automation.

I did have a glimpse at the regular platform: mqtt device_tracker, because that allows me to set the source_type: gps too, but then the payload is not correct, which is of course the reason for the json tracker in the first place…
I set the source_type with customize now.


please have a look? thanks!

do you need a device tracker? If not just make a sensor with lat/lon, it’ll still show up on a map if thats what your after.

thanks, though I guess I do need a device_tracker.
it should be either home/not_home/zone and thus can trigger automations.
been staring too long at these mqtt configs now.

Maybe check

But according to the device_tracker.see, “location_name” is mandatory, so not sure who’s right.

Phil is always right :wink: Although this is some time ago of course, and things might have changed

I guess I could do this then:

automation:
  - trigger:
      platform: state
      entity_id:
        - sensor.ot_boss_latitude
        - sensor.ot_boss_longitude
        - sensor.ot_boss_battery_level
        - sensor.ot_boss_accuracy
    action:
      service: device_tracker.see
      data:
        dev_id: owntracks_theboss_see
        gps:
          - "{{states('sensor.ot_boss_latitude') }}"
          - "{{states('sensor.ot_boss_longitude') }}"
        battery: "{{states('sensor.ot_boss_battery_level')|int}}"
        gps_accuracy: "{{states('sensor.ot_boss_accuracy')|int}}"

and that would allow me to dispense with the mqtt_json config.
let me try. thanks

bingo:


Nice :wink:

could probably take out the 2 bottom triggers, as the device_tracker will be updated with these anyways, if and when the lat/lon changes, and I don’t care about those to trigger.

not anymore, corrected that :wink: see: Device tracker - Home Assistant

1 Like

Even if it’s not mandatory per-se, you should check if the location changes based on gps coordinates.
My fear is that, by using the service, location wouldn’t be updated.

Hi petro, could you tell me how to do this, please? I have lon/lat in my node-red installation getting it from an mqtt-device, but i don’t know how to use it for showing it on a map in my dashboard.
Thanks a lot!