Hi @TiToTB as anticipated via chat I played a little bit with device_tracker.see to create this device class instead of sensor class to share lat and long.
This is what I achieved so far.
- the rest sensor is more or less the same that you already shared. I only added some condition to avoid mismatches (for example when geoapify picks up the correct street but in a different town:
sensor:
- platform: rest
name: airtag_herbie
value_template: "{{ value_json.results[0].formatted }}"
json_attributes_path: "$.results[0]"
json_attributes:
- lon
- lat
- district
- city
- postcode
- street
- housenumber
- state
- country
- country_code
verify_ssl: true
scan_interval: 900 # every 15 min
resource_template: >
{% set address = states('input_text.airtag').split('Herbie')[1].split('•')[0] %}
{% set home = "Via xxxxxxxxxxxxxxxxxxxxxxxxxxxx Italy" %}
{% set work = "Via xxxxxxxxxxxxxxxxxxxxxxxxxxxx Italy" %}
{% if "Casa" in address %}
{{
"https://api.geoapify.com/v1/geocode/search?text="
+ home | urlencode
+ "&apiKey=xxxxxxx&format=json"
}}
{% elif "Via xxxxxxx" in address %}
{{
"https://api.geoapify.com/v1/geocode/search?text="
+ work | urlencode
+ "&apiKey=xxxxxxx&format=json"
}}
{% elif "Via xxxxxxx" in address %}
{{
"https://api.geoapify.com/v1/geocode/search?text="
+ work | urlencode
+ "&apiKey=xxxxxxx&format=json"
}}
{% else %}
{{
"https://api.geoapify.com/v1/geocode/search?text="
+ address | urlencode
+ "&apiKey=xxxxxxx&format=json"
}}
{% endif %}
the only missing piece is the following: when airtag is not recently updated in Find My I see a message “last update: xx:xx”. Going inside the device details address is colored red, maybe because Apple want to tell us that it may be not so reliable. I’m thinking how to solve this…there’s a way to allow input_text to exclude text with that string?
Below the device tracker automation to create new entitity:
automation:
- alias: Set Herbie Location da Airtag
trigger:
- platform: state
entity_id: sensor.airtag_herbie
action:
- delay:
hours: 0
minutes: 0
seconds: 5
milliseconds: 0
- service: device_tracker.see
data:
dev_id: airtag_herbie
# location_name: "{{ states('sensor.airtag_zaino_tigrotto') }}"
gps:
- "{{ state_attr('sensor.airtag_herbie', 'lat') }}"
- "{{ state_attr('sensor.airtag_herbie', 'lon') }}"
mode: single
another piece that I would like to share is that rest sensor (in my config) il loaded before input_text at HA restart so it happens that sensor fail and all is broken.
As a workaround I wrote this:
- alias: Reload REST integration after restart
trigger:
- platform: homeassistant
event: start
id: HA restart
condition: []
action:
# - delay:
# hours: 0
# minutes: 5
# seconds: 0
# milliseconds: 0
# - service: notify.mobile_app_iphone_di_marco
# data:
# message: Dove sono gli AirTag?
# data:
# shortcut:
# name: AirTag to HA
- delay:
hours: 0
minutes: 5
seconds: 0
milliseconds: 0
- service: rest.reload
mode: single
as you can see I left commented the part when asking manual updated at boot as well, maybe is a bit overkill.
I also created this for test purpose to avoid waiting 15min during developement:
- alias: Reload REST integration after shortcut refresh
trigger:
- platform: state
entity_id: input_text.airtag
action:
- delay:
hours: 0
minutes: 1
seconds: 0
milliseconds: 0
- service: rest.reload
Any feedback is welcome and appreciated