Setting the lat and lon attributes to a tracking_device

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