AirTag "integration" (user friendly + device tracker)

Interesting approach! I just wonder, how would you manage It for example if your are travelling and one of your airtags is in your luggage and the others are at home? Would It give you false matches? Thanks!

I ll keep It in mind for next version!

Thank you!
Does this mean the lat / lon are rounded?

Not by me, this is how we get It by default. But maybe we can get more decimals some how

1 Like

I did a bit of a fix that works well for me. For the devices that are normally with me (car and keys) I use the following line in the sensor configuration.

{% set fix = address + " " + states.sensor.pocof3miui14_geocoded_location.attributes.locality + " " + states.sensor.pocof3miui14_geocoded_location.attributes.country %}

What this does is that it takes the City (locality) and the Country of my phone in the call to Geoapify. This works quite nice as the city and country are more often correct now as my keys and car are mostly in the same city as I am.

The device that is normally around home I just use the original line.

1 Like

Hi, TitoTB,
Your question is a very good one, in fact, this solution I provided is still not perfect and can’t fully cover all the usage scenarios.
Since I don’t have frequent business trips or overseas travel plans these days, I haven’t done a more comprehensive test yet.
But I still think there are a lot of optimizations to be made, such as
1> You can group multiple devices, for example, which devices are often left at home, which devices are often active and mobile. Then based on the grouping, make some logical conditional restrictions in the python code.
2> There is another possibility to do some optimization for iOS automation process. For example, do a focus action on each device before OCR recognition on iOS devices, and do OCR text extraction for each device individually, but this automated action may be more complicated. I’m still trying to do this for now.

If you have a better solution, feel free to discuss it together, in short. I think this is a good project.

1 Like

Means, in HA the REST implementation cuts off the other decimal values after 2 digits?
That does make sense for temperatures, but not for GPS cooordinates.

Is this something we could change / define in the REST entity definition?

EDIT:
Sorry - my bad:
I did not recognize, that the real number of digits after the decimal point is correct in developer tools

1 Like

The rest implementation uses the full coords. I have 7 decimals in both sensor.airtag_backpack and sensor.airtag_backpack_geo

I think the issue of 2 decimals is unrelated to this solution, perhaps this is somthing:
Latitude and longitude have 2 decimal signs precision - Configuration - Home Assistant Community (home-assistant.io)

1 Like

This is really interesting, can you share the code? I would like to see if I can then combine it with the other solution i added ( AirTag "integration" (user friendly + device tracker) - #126 by john2014 )

Another issue I have is, that I have 6 Airtags and just see three in the object screens.
If you follow the tipp in the documention and swipe the list upwards, the shortcut will capture all 6 Airtags in a screenshot, but the OCR fails.
Means, the input_test.airtag is Unknown

Anyone successfully uses the swiped up view?

I had an issue with OCR and added a CROP step to only get the part of the screenshot with the items. This might also work for your usecase. It is a bit of trial and error to do, but if you crop the screenshot to just the text with the items, it works very well.

The way I did it:

  1. Create a screenshot and download it to a pc
  2. With a graphics program, see the starting coordinate and width and height of the image you want.
  3. Add this CROP step to the shortcut (make sure it is taking the screenshot as input)

In HA I would then check the value of input_test.airtag to see if it was working. Afaik you can not see what is actually cropped.

Update: After the CROP you can also use Save to recents to store the cropped image in Recents. That way you can see what is cropped.

2 Likes

Yep, i agree with you. I think this is the base, but it could/should be modified with your personal circumstances.

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?

Thanks!

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?