Suggested Fix for device tracker location
Audreth opened this issue · 9 comments
Solved the device tracker issue:
Taking the information from HA-MQTT.md, I used the script to get the car's location (Car - Location) as well as the Automation that went with it.
I added the below code to my configuration.yaml and was able to get the location set properly for home, away (not_home), as well as other zones configured in home assistant
configuration.yaml
mqtt:
device_tracker:
- name: "Location"
state_topic: "homeassistant/device_tracker/ONSTAR_VIN/getlocation/state"
value_template: "{% set ns = namespace(loc=[]) %}{% if (distance(value_json.latitude | float, value_json.longitude | float)*1000) <= state_attr('zone.home','radius') %}{% set ns.loc = ns.loc + [ 'home' ] %}{% else %}{% for zone in states.zone %}{% if distance(value_json.latitude | float, value_json.longitude | float, zone) * 1000 <= state_attr(zone.entity_id,'radius') %}{% set ns.loc = ns.loc + [ zone.name ] %}{% break %}{% endif %}{% endfor %}{% endif %}{% if ns.loc | length > 0 %}{{- ns.loc[0] }}{%else%}not_home{% endif %}"
json_attributes_topic: "homeassistant/device_tracker/ONSTAR_VIN/getlocation/state"
json_attributes_template: "{{ {'latitude': value_json.latitude | float, 'longitude': value_json.longitude | float } |tojson }}"
device:
identifiers:
- ONSTAR_VIN
manufacturer: *****
model: '****'
name: *****
unique_id: ONSTAR_VIN-location
source_type: gps
payload_home: "home"
payload_not_home: "not_home"
@Audreth , all I have in my HA configuration is what is in the Location section of HA-MQTT.md and HA has always shown both my cars as either Home or Away or when applicable, the custom zone name. Therefore, I'm not sure why you had to do the above to get the same functionality. What was it showing before you did the above?
Sorry, neither were issues for me or anyone using the original https://github.com/michaelwoods/onstar2mqtt that I'm aware of.
I think the issue might be because you added quotes around the state topic.
From your post:
state_topic: "homeassistant/device_tracker/ONSTAR_VIN/getlocation/state"
From the example in the Location section of HA-MQTT.md:
your_car_name: homeassistant/device_tracker/YOUR_CAR_VIN/getlocation/state
I tried adding quotes to the state_topic and even then, the vehicle status works without any issues just using the default configuration that is shown in the HA-MQTT.md file. I’m guessing something is amiss on what is being read back to HA via MQTT for your location topic, so would need to see the raw data to troubleshoot the issue which I know you are not interested in because you have your workaround.