AirTag "integration" (user friendly + device tracker)

As i said, the coordinates are not rounded int the code. It is just the way we get the information :blush: Can we get more information? Maybe, it is something to dig in

“I have been also playing triying to focus on every device with gestures, but i didn’t find the right way to do it. How are you triying to achieve this?”

–rep:
Unfortunately, this is just a solution I’ve come up with, and I’ve been looking for this solution, but I haven’t solved it yet.
iOS seems to have some limitations on such operations, and I’m not very familiar with the iOS implementation of this, so if there are iOS engineers who know more about this, maybe they have a better way.
If you have a better solution, we can discuss it together.

I am happy to share more information if I would know what :slight_smile:
What I see is, that the json response from geoapify returns:

{
    "results": [
        {
            "datasource": {
                "sourcename": "openstreetmap",
                "attribution": "© OpenStreetMap contributors",
                "license": "Open Database License",
                "url": "https://www.openstreetmap.org/copyright"
            },
            "country": "Germany",
            "country_code": "de",
            "city": "Hamburg",
            "postcode": "xxxxx",
            "suburb": "xxxxx",
            "street": "xxxxx",
            "housenumber": "xxxxx",
            "lon": x.xxxxxxx,
            "lat": yy.yyyyyyy,
            "result_type": "building",
            "formatted": "xxx, xxx, Hamburg, Germany",
            "address_line1": "xxx xxx",
            "address_line2": "xxx Hamburg, Germany",
            "timezone": {
                "name": "Europe/Berlin",
                "offset_STD": "+01:00",
                "offset_STD_seconds": 3600,
                "offset_DST": "+02:00",
                "offset_DST_seconds": 7200,
                "abbreviation_STD": "CET",
                "abbreviation_DST": "CEST"
            },...

I wonder why geoapify is needed at all if openstreemap is used.
Back in my openHAB days, I remember that I retrieved the address from coordinates using the openstreetmap API request to openstreetmap.
I need to check if I have still the code.

Anyway, the rest entity below reads lat / lon from this json response.
So there must be somthing going on under the hood of the rest part of HA!?

# New Muli Airtag Tracker
- platform: rest
  name: "Airtag Muli (Geo)"
  unique_id: airtag_muli_geo
  value_template: "{{ value_json.results[0].formatted }}"
  json_attributes_path: "$.results[0]"
  json_attributes:
    - lon
    - lat

Thank you and sorry.
I missed your comment on the decimals above somehow.
I will look into the link you’ve sent first thing tomorrow morning.

sensor:
  - platform: rest
    name: airtag_ota_geo
    value_template: "{{ value_json.results[0].formatted }}"
    json_attributes_path: "$.results[0]"
    json_attributes:
      - lon
      - lat
    verify_ssl: true
    scan_interval: 900 # every 15 min
    force_update: true
    resource_template: >
      {% set address = states('input_text.airtag').split('Ota')[1].split('•')[0] %}
      {% if address == "Home" %}
        {% set home = "Straža Pri Raki 11" %}
        {{
          "https://api.geoapify.com/v1/geocode/search?text="
          + home | urlencode
          + "&apiKey=xxxxxxxxxxxxxxxxxxxxxxxx&format=json"
        }}
      {% else %}
        {% set fix = address + " Slovenia" %}
        {{
          "https://api.geoapify.com/v1/geocode/search?text="
          + fix | urlencode
          + "&apiKey=xxxxxxxxxxxxxxxxxxxxxxx&format=json"
        }}
      {% endif %}
  - platform: template
    sensors:
      airtag_ota:
        friendly_name: "ota"
        value_template: >
          {% set home = "Straža Pri Raki 11" %} 
          {% if ":" in states('input_text.airtag').split('Ota')[1].split('•')[0] %} 
          {{ states('input_text.airtag').split('Ota')[1].split('•')[0].split(',')[0] }}
          {% elif home in states('input_text.airtag').split('Ota')[1].split('•')[0] %}Home
          {% else %}
          {{ states('sensor.airtag_ota_geo') }}
          {% endif %}
        attribute_templates:
          latitude: >
            {% if ":" in states('input_text.airtag').split('Ota')[1].split('•')[0] %}null
            {% else %}{{ state_attr('sensor.airtag_ota_geo','lat') }}
            {% endif %}
          longitude: >
            {% if ":" in states('input_text.airtag').split('Ota')[1].split('•')[0] %}null
            {% else %}{{ state_attr('sensor.airtag_ota_geo','lon') }}
            {% endif %}
          status: >
            {% if ":" in states('input_text.airtag').split('Ota')[1].split('•')[0] %}unavailable
            {% else %}available
            {% endif %}
          last_update: "{{ as_timestamp(states.input_text.airtag.last_updated)|timestamp_custom ('%d/%m/%Y %H:%Mh') }}"
        icon_template: mdi:dog

Any body knows why there are no coordinats? in sensor?

Try a Quick reload:

image

I have the same issue at boot. The problem is that some of the variables are not loaded or something. I now have an automation that will reload the configuration 1 minute after boot. This solved this issue for me.

1 Like

Thanks m8! its working now. Can you share the automation?

1 Like

Any body got a problem with geocoding?

Sure here it is. I actually also have a 2nd automation that does a reload every hour, just in case :wink:

alias: >-
  System - Reload Configuration 2minutes after boot to make the Airtag Rest call
  work
description: ""
trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: homeassistant.reload_all
    data: {}
mode: single
1 Like

Can you explain a bit more. Bit difficult to understand why something is correct or wrong. Is the text where you write wrong a street name? What is wrong about it?

The latitude and longitude are correct but the address is not. If i try coordinates in geoapify the give me the right address.

It has to do with this part of the code. But tbh, I dont understand this code. I dont understand the check on “:” as I never see that on my iphone. It also doesn’t work for me. Whenever there was “Home” displayed on my iphone, the solution will not work. I ended up making my home address on the iphone someplace far away so that it always shows the address in Find My and never “home”

@TiToTB might be able to explain what this code should do.

          {% set home = "Streetname" %} 
          {% if ":" in states('input_text.airtag').split('backpack')[1].split('•')[0] %} 
          {{ states('input_text.airtag').split('backpack')[1].split('•')[0].split(',')[0] }}
          {% elif home in states('input_text.airtag').split('backpack')[1].split('•')[0] %}Home
          {% else %}
          {{ states('sensor.airtag_backpack_geo') }}
          {% endif %}

You can see “:” when the status is “Last seen:…”. That is the reason of that part of the code.

Also, “home” is a variable. It converts your Home address in “Home” value, if you have set if before (“streetname”)

1 Like

Hi everyone,

I have a problem with my German iPhone.
In Find my… App the state is Privat if at home (not Zuhause as expected.

We have a street in Hamburg called Privatweg.

This part of the template seems to find the location of my items at Privatweg.

resource_template: >
    {% set address = states('input_text.airtag').split('Ranzen Mads')[1].split('•')[0] %}
    {% if address == "Privat" %}

Shouldn’t this only work if it’s Privat and not Privatweg or something like Privat...?

EDIT:
Never mind - I asked ChatGPT and I will try that

 {% if address == "Privat" and address != "Privatweg" %}
1 Like

How did you change the home address on the iphone?

As everything in iOS (I really don’t like the Apple ecosystem and use Android for everything else besides these airtags) it is in a very weird location:

Add or change your home address in Maps on your iPhone or iPad - Apple Support

2 Likes

+1 for that! i only have an old iPhone for this haha. Thanks m8.

2 Likes

I do see that we can probably switch to Google for the tag integration soon.

Find My Device Trackers on Android are Coming! (youtube.com)

My phone is connected today i saw. Wont be long before the Tags are being produced. I already see Chipolo Point finders for Google’s Find My Device app - Chipolo and Google Find My Device – Pebblebee

Hopefully the integration with Home Assistant will be a bit smoother. The work that @TiToTB has done is truly amazing and creative, but I would prefer to get the coordinates straight from an Google API. Fingers crossed.

2 Likes

I agree! I cant wait to have this solution in android.

However, it might take some time until we can find more affordable devices (hopefully i am wrong)

2 Likes

I appear to not be receiving lat/lon coordinates: any ideas?

latitude: null
longitude: null
status: available
last_update: 28/05/2024 15:34h
icon: mdi:bag-personal
friendly_name: mybag