Create a device tracker from sensor template (GPS coordinates from REST API)

I have follow this tutorial to get my car location. Now sensor.car_latitude and sensor.car_longitude have the updated GPS coordinates of my car. How could I create a device_tracker from these sensors? I want to show in the integrated HA map and use zones, not only showing in Google Maps, like the tutorial.

Maybe could I use device_tracker.see like in https://community.home-assistant.io/t/composite-gps-based-device-tracker/67179/3

Another idea that seems more elegant because MQTT:

1 Like

Have you ever found a solution for this? I’m having this same challenge.

1 Like

I’ll reply here since this thread came up in my search when I was looking to solve this same thing, and this was left unanswered. I skipped over the part of creating template sensors based on the attributes for the REST component and instead just mapped the attributes in my service call, but this will work the same either way.

In my configuration, I’m retrieving GPS coordinates from a REST endpoint for a vehicle tracker (doesn’t matter what kind), and using it to update a device_tracker entity for the car.

Here’s my REST sensor:

- platform: rest
  name: my_rest_sensor
  resource: https://foo.bar/api/get?foo=bar&key=blah
  value_template: '{{ value_json.result }}'
  scan_interval: 120
  json_attributes_path: $.entries.[0]
  json_attributes:
    - lat
    - lng
    - altitude
    - course
    - speed

Next, I’m using an automation to update the device_tracker component, which needs to be present in known_devices.yaml already.

Entry from known_devices.yaml:

my_car:
  hide_if_away: false
  icon: mdi:car
  mac:
  name: My Car
  track: true

My automation for updating the device from the REST sensor:

alias: Update Car Tracker
trigger:
  - platform: event
    event_type: state_changed
    event_data:
      entity_id: sensor.my_rest_sensor
condition: []
action:
  - service: device_tracker.see
    data:
      dev_id: my_car
      gps:
        - "{{ state_attr('sensor.my_rest_sensor', 'lat') }}"
        - "{{ state_attr('sensor.my_rest_sensor', 'lng') }}"
mode: single

If you have individual sensors for lat/lon, you can change the template for the gps values in the automation to use the states function instead of state_attr.

Also the device_tracker.see service can take a location name parameter, but HA will resolve a zone based on the GPS coordinates automatically.

Does anyone know how to do this without using known_devices.yaml?

2 Likes