How to create a custom device tracker (from GPS coordinates)?

maybe this can help? https://www.home-assistant.io/integrations/mqtt_json/
your custom component needs to publish the coordinates with mqtt in JSON t format I guess.

You can use the device_tracker.see service.

1 Like

Unfortunately not, I already investigated this option. " The device_tracker.see service can be used to manually update the state of a device tracker"

… and manually updating the state of a device tracker that does not yet exist will create it. I know this will work. I’ve done it myself.

1 Like
automation:
  - trigger:
      platform: state
      entity_id: sensor.kia_position_lat, sensor.kia_position_long
    action:
      service: device_tracker.see
      data_template:
        dev_id: kia
        gps:
          - "{{ states('sensor.kia_position_lat') }}"
          - "{{ states('sensor.kia_position_long') }}"
10 Likes

Excellent this works, I had a similar automation based on time and I couldn’t get to work. Thanks!

1 Like

Any chance of you please sharing this custom integration sensor? I’m interested in this feature too. :slight_smile:

I tried the solution but it doesn’t seem to work. No tracker created.
Also, when trying to use the service manually I encounter a host of issues, basically the format for gps doesn’t seem to be valid for device_tracker.see call.
Any input?
This is what I am trying to do:
Sensors for coordinates extracted from MQTT by my Leaf OVMS:

   - platform: mqtt
     name: leaf position lat
     state_topic: "ovms/nissanleaf/xxx/metric/v/p/latitude"
     qos: 1
   - platform: mqtt
     name: leaf position lon
     state_topic: "ovms/nissanleaf/xxx/metric/v/p/longitude"
     qos: 1

Automation to update the position/create the tracker:

- id: '1603005245535'
  alias: 'Update Leaf position'
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.leaf_position_lat
  - platform: state
    entity_id: sensor.leaf_position_lon
  condition: []
  action:
  - service: device_tracker.see
    data:
      data_template:
        dev_id: Leaf
        gps:
         - "{{ states('sensor.leaf_position_lat') }}"
         - "{{ states('sensor.leaf_position_lon') }}"
  mode: single

This is actually triggered (I’ve checked) but no tracker is ever created.
I tried triggering the method manually using the developer tools but it complains about the data_template not being a valid keyword or that I am missing dev_id (depending on how I build up the yaml).

1 Like

I don’t know if you still want this but it does work, your YAML is just wrong. Heres mine:

  - service: device_tracker.see
    data_template:
      dev_id: work_phone
      location_name: Work
      gps:
        - '{{trigger.json.latitude}}'
        - '{{trigger.json.longitude}}'

Hi,

I created an automation on my iPhones being triggered when the phone looses the connection with CarPlay.
The automation is based on the decvice_tracker.see service, which has correctly created a new device tracker.
Yesterday the service created the new device tracker with the information (GPS coordinates) provided from my phone, but today I noticed that the new entity lost all the data.
Do you know if there is a way to keep that information in the system?

EDIT: I noticed that the system doesn’t keep the value if I reboot the system.
Any solutions are welcomed

1 Like

I have the problem that an integration is not able to create a device tracker.

So I used the info from this thread to manually test it (modified the GPS coordinates to post them here):

service: device_tracker.see
data:
  dev_id: automower_none_170805572_164914249
  location_name: home
  gps:
    - 51.509802
    - -0.086692

executed via developer tools - services. No error in the log, but no device tracker created.
@tamet83 found your issue and commented it. you have the problem of updating, I have the problem of creating.

1 Like

Hi @ChrLipp I’m sorry but I don’t know how to help you.
I use this on my iPhone Commands App using this template:


{
  "dev_id": "Golf", 
  "gps_accuracy": 20, 
  "gps": [
    "Latitudine", 
    "Longitudine"
  ]
}

Where latitude and longiture are variable extracted from my phone GPS info.
I’m still having the problem when i reboot home assistant: i loose entity data provided from the automation

Could you run the Automation at HA-startup to update it ?

I just tried pasting your config

service: device_tracker.see
data:
  dev_id: automower_none_170805572_164914249
  location_name: home
  gps:
    - 51.509802
    - -0.086692

and it created a device_tracker:

I use the automation directly from my iPhone, it starts each time the phone loose the connection with CarPlay on my car. I use it to save my car parking position.
I think won’t be useful using it when home assistant starts.

OK, thought that was your issue here

@Holdestmade Thanks for trying. When I execute this, the device_tracker is not created, so strange.
I am using

version core-2021.4.6
installation_type Home Assistant Container
dev false
hassio false
docker true
virtualenv false
python_version 3.8.7
os_name Linux
os_version 4.4.59+
arch x86_64
timezone Europe/Vienna

Do you get any errors in the log when you run the service in Dev tools ?
Did you check in dev tools for the device_tracker ?

Log says

2021-04-28 16:45:15 INFO (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Running websocket_api script
2021-04-28 16:45:15 INFO (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Executing step call service

Under dev tools - entities the device tracker doesn’t show up.

Very odd, can’t help then I’m afraid. I cant get rid of the test one now, assume after a restart it will be gone