Setting the lat and lon attributes to a tracking_device

I use owntracks and the iOS app to track my location,
I use this for automations (e.g. when I’m home or not) but also to track my location on a map as per this example

I found that there are times when one of the trackers is not accurate enough and gives me an incorrect zone location.

To try and improve the reliability of my overall location I created a simple automation that gets the latest tracker status and posts it to an MQTT topic.

- alias: location_lolo
  trigger:
      - platform: state
        entity_id: 'device_tracker.lolos_iphone'
      - platform: state
        entity_id: 'device_tracker.ha_lolos_iphone'
  action:
    service: mqtt.publish
    data_template:
      topic: 'location/lolo'
      payload_template: '{{ trigger.to_state.state }}'
      retain: true

This works great and my home/not home location is so much better.
Only issue is that the tracker in location/lolo does not have any coordinates, only a status (home/away/zone name).
How would I go about copying the lat and lon from the trigger.to_state info and posting it so that HA recognises this as coordinates so I can display it on a map card?
this is my camera card:

- platform: generic
  name: 'Lolo'
  still_image_url: https://maps.googleapis.com/maps/api/staticmap?center={{ states.device_tracker.lolo.attributes.latitude }},{{ states.device_tracker.lolo.attributes.longitude }}&zoom=15&size=500x500&maptype=roadmap&markers=color:blue%7Clabel:L%7C{{ states.device_tracker.lolo.attributes.latitude }},{{ states.device_tracker.lolo.attributes.longitude }}
  limit_refetch_to_url_change: true

I think you’d need to make a custom component that looks at zone names and populates the lat and longitude. I don’t think there is some simple “templating way” to complete this. I could be wrong though.

The custom component would be a copy of the generic platform with the extra zone searching & populating bs in the python code.

I just did something kind of similar and thought I’d share in case it sparks any thoughts for you.

I have a few different types of device trackers in place, but for the sake of simplicity I have them all report status via mqtt to a mqtt_json device tracker which I then use for all automations. My recent addition was the Google Maps Location Sharing device tracker, which holds latitude and longitude in the attributes. I created template sensors (not shown on front end) for each of these attributes, and then whenever one of these changes, I pull the current values from the Google Maps tracker attributes and publish to my mqtt_json tracker.

Certainly open to suggestions on better ways of doing this if anyone has them! :slight_smile:

sensor:
  - platform: template
    sensors:
      brett_pixel_latitude:
        value_template: '{{ states.device_tracker.google_maps.attributes.latitude }}'
      brett_pixel_longitude:
        value_template: '{{ states.device_tracker.google_maps.attributes.longitude }}'
device_tracker:
  - platform: mqtt_json
    devices:
      brett_pixel: location/brett/pixel
  - platform: google_maps
    username: !secret gmail_username
    password: !secret gmail_password
automation:
  - alias: 'Brett Google Location Update'
    trigger:
      - platform: state
        entity_id: sensor.brett_pixel_latitude
      - platform: state
        entity_id: sensor.brett_pixel_longitude
    action:
      service: mqtt.publish
      data_template:
        topic: location/brett/pixel
        payload_template: '{"longitude": "{{ states.device_tracker.google_maps.attributes.longitude | float }}","latitude": "{{ states.device_tracker.google_maps.attributes.latitude | float }}","gps_accuracy": "{{ states.device_tracker.google_maps.attributes.gps_accuracy | int }}"}'
        retain: true

Thnaks @petro but that seems a bit overkill no?
I’m actually thinking now of posting to the existing owntracks topic and fake the source as if it came from owntracks.
All I’d need to do is find a way to post a json message with the same data as owntrack’s one? Would that work?

Thanks, your solution combined with my reply to petro might end up being what I need to do.

Yah, that should work because it’s an mqtt topic. It’s just that owntracks will override it at times. You could always create a third mqtt device tracker and do the same thing.