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.