Device Tracker, Best echange for Life360

Phil, thank you for the prompt response. Unfortunately, it brings questions about locationsharinglib. Trying to be proactive in all of this, I can google the library and install directions, however, none are relevant to HAOS. Trying to follow the original instructions doesn’t list this lib anywhere. I really am trying to not be the script-kiddie here, however, I’m getting lost in the sauce.

So, what I understand is I need to install the locationsharinglib before any of the subsequent steps will work. Sound about right?

Cheers!!

Looked over the issue you created. I agree setting up this integration is more difficult than one would hope. But most, if not all, of the points you raise I believe are already covered in the existing documentation, albeit maybe a bit cryptically.

E.g., it does explain why two accounts should be used. (That explanation didn’t exist when I first started using it.)

As far as the cookie file name goes, the email address should be “slugified”. Yeah, that isn’t really clear, I would think, for most people. And the file name should not have an “extension”, like .txt, which might be hard for some people who are less adept at this sort of thing.

Now that the Life360 integration seems to be really dead, I might find some time to make the Google Maps integration a bit easier to use, but no promises.

1 Like

The locationsharinglib is just a package that the HA Google Maps integration uses. It does all the “low level” interaction with Google. It is automatically installed by HA. You do not need to worry about it.

The email I created for home assistant is:

My working cookie filename is:

.google_maps_location_sharing.cookies.word1_word2_word3_gmail_com

(Dot at the beginning and on either side of the word “cookies” only.)

What the google maps integration really needs is GUI setup, and the ability to not just fetch but more importantly renew the cookie. Kind of like the Alexa media player integration does

Agreed. But fetching & renewing the cookie, and the actual pulling/scraping of the data, is outside my wheelhouse.

On my todo list would be:

  • Convert from “legacy tracker” (i.e., that uses known_devices.yaml) to the newer, entity-based implementation.
  • Add config flow, including both import (i.e., YAML) & UI.
  • Add device info.
  • Add translations.
4 Likes

So far after following the above procedure by phil everything Google Maps seems to be working and has worked for over 24 hours.

So far so good.

:+1:

I’ve looked at none of this, but if its just web scraping, I’d be happy to help. It’s about the only thing I can do with python.

Check out:

costastf/locationsharinglib: A library to retrieve coordinates from an google account that has been shared locations of other accounts. (github.com)

That’s the package HA’s Google Maps integration uses. It does all that stuff.

Thanks to the excellent support of Phil (pnbruckner), Google Maps location sharing in HAOS now “works” for me. Works in that HA can sense when folks are and are not in each zone, however, there is a 15-20 delay in updates; thus, irrelevant to use as a sensor for automations.

Is there anything I can configure to diminish, if not completely eliminate, the flash-to-bang delay in location updates?

I’ll admit that I was SPOILED with Life360’s responsiveness to location updates and strive to achieve that level of fidelity.

Again, Phil, thank you, sir, for the help getting this to work.

Cheers!!

1 Like

I don’t generally find that to be the case. Yes, sometimes updates from Google can take a while, but that seems to be mostly 1) when the device isn’t really moving, which is fine, 2) in a bad cell/network coverage area, or 3) when the source is an iPhone (as opposed to Android. Not sure why this is.)

E.g., here are the 8 phones I’m tracking via Google Maps:

image

That is the last four hours of “time between updates” for each of the phones. You can see there is one outlier (yes, an iPhone), and sometimes the updates can take upwards of 10, 20 or even 30 minutes. But, again, that tends to be when the phones aren’t moving, so it doesn’t matter. If you look closely at the bottom, you can see the majority of the time, the updates are only a few minutes apart or less.

One question I have that I can’t seem to definitively find an answer to is if I share another google maps account with the same B account then do I have to reset the cookie or will the new account automagically populate to the original A account?

Or is there other stuff I need to do?

I haven’t tried it yet. Just wondering before I do.

The Google Maps integration will automagically see any other account that is shared with the account you point it at (and provide a cookie file for.) The first time it sees an account, it will be added to known_devices.yaml and a device_tracker entity will be created. That can be at any time, not just at startup. Then on next startup, “restored” device_tracker entities will be created for everything in known_devices.yaml, and the state of those entities will be updated as their corresponding accounts are “seen”.

But to try to answer your question more directly, you can “point” the integration at as many accounts as you like. For each account you’ll need a cookie file. In theory, you should only have to create each of those cookie files once, independently of what other accounts are shared with them, or when they get shared. The cookie files will eventually expire, but in my experience, that is after about a year.

There are two reasons, I would say, for configuring more than one account. The first reason is that the device_tracker entity created for the account itself will be lacking two attributes, battery_level & entity_picture. (Actually, it will have battery_level, but its value will be null.) So, to get all the attributes for that account, you need a different account to be configured that the first account has been shared with.

So, concrete example. Account A is your “normal” account, and accounts C through F have been shared with it. This will create entities for A & C through F, but the entity for A is lacking attributes. To get all those attributes, you configure account B that A has been shared with. Now B creates an entity for itself (lacking attributes) and one for A (with all its attributes.)

But wait, there are two entities for A. Isn’t that a conflict? Well, turns out, no. Normally, the entity IDs are device_tracker.google_maps_NNNN, where NNNN is a number unique to that account. But the entity that A creates for itself has a different entity ID, more like device_tracker.google_maps_SLUGIFIED_EMAIL_ADDRESS. The one you want to use for A is the one from account B, which will have the usual entity ID format and all the attributes for A.

Clear as mud?

EDIT:

Oh, the second reason would be to see more accounts and create more entities. E.g., A can see C through F, and G can see I through K. You might then configure A & G to get C through F & I through K. (And then you might want to create B to see all of A and H to see all of G. :rofl: )

1 Like

I am getting the following error

Logger: homeassistant.components.device_tracker
Source: components/device_tracker/legacy.py:311
Integration: Device Tracker (documentation, issues)
First occurred: 15:31:39 (1 occurrences)
Last logged: 15:31:39

Error setting up platform legacy google_maps
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/device_tracker/legacy.py", line 311, in async_setup_legacy
    setup = await hass.async_add_executor_job(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/google_maps/device_tracker.py", line 57, in setup_scanner
    scanner = GoogleMapsScanner(hass, config, see)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/google_maps/device_tracker.py", line 76, in __init__
    self.service = Service(credfile, self.username)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/locationsharinglib/locationsharinglib.py", line 98, in __init__
    self._session = self._validate_cookie(cookies_file or '')
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/locationsharinglib/locationsharinglib.py", line 153, in _validate_cookie
    session = self._get_authenticated_session(cookies_file)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/locationsharinglib/locationsharinglib.py", line 149, in _get_authenticated_session
    raise InvalidCookieFile(message) from None
locationsharinglib.locationsharinglibexceptions.InvalidCookieFile: Could not open cookies file, either file does not exist or no read access.

Any thoughts?

I am getting this:

The cookie file provided does not provide a valid session. Please create another one and try again

With nothing else provided in the log. Guess I need to turn on debug and reboot.
@pnbruckner The email I put in device tracker, is that the sluggified or the regular email ? Cookies is the underscore version but not sure about the device tracker configuration file. I currently have this in device_tracker.yaml (which is referenced in configuration.yaml)

  
    - platform: google_maps
    username: "[email protected]"
    max_gps_accuracy: 200
    # scan_interval: 60

@Markb1 Make sure the url is sluggified only the beginning needs a dot and cookies before and after .cookies. the rest use underscores.

It is as per the documentation.

.google_maps_location_sharing.cookies.XXXXXXX_gmail_com

Out of ideas…

What you have is correct. If it wasn’t, i.e., if the username or cookie file name weren’t right, you’d get a different error about not being able to find the cookie file.

From what I’ve read (especially what I’ve read in the locationsharinglib project) you have to make sure the cookie doesn’t represent a session that has been logged out. I think the main way to avoid that is to 1) use an “incognito” / “inPrivate” web browser to get the cookies, and 2) do NOT log out from that browser, just close it.

There is still the possibility that the session in the cookie file may become invalid if you log out of that Google account somewhere else. This isn’t clear, but it might be the case.

EDIT:

Because of this last point the author of locationsharinglib strongly suggests using a different account than any that is normally used, i.e., one you create just for this purpose. Then have everyone you care about tracking share their location with that HA-only account. Then, when you grab cookies, do NOT log out. Just close the browser. I think I’d also suggest that when grabbing the cookies, during the sign in step, make sure to check the box that says something like do not require this on this computer, stay logged in, or something similar, if it presents something like that. (Sorry, can’t remember the details.)

FWIW, I’ve only ever used my “normal” account and have not had issues (other than the missing attributes for my own tracker, which is why I’ve recently used another account that just my account shares its location with, in addition to my normal account. I mean, I configure the integration with both accounts, my normal one and this other one.)

Did you “slugify” that part correctly? E.g., [email protected] would become xx_yyy_zzz_gmail_com. Basically, everything lower case and all special characters (anything other than a number or a letter) gets replaced with an underscore.

Yes, without giving to much away the x are lowercase

.59KxxxxxxRxxx_gmail_com

That might be your problem. Shouldn’t it be .59kxxxxxxrxxx_gmail_com, ALL lower case? HA typically runs on Linux, and in Linux, filenames are case sensitive.