nathan-gs / ha-map-card

A Map Card for Home Assistant
https://community.home-assistant.io/t/map-card-a-slightly-improved-map-card/693088
MIT License
15 stars 4 forks source link

Use configured device_trackers for person type entities. #60

Closed thybag closed 3 weeks ago

thybag commented 3 weeks ago

Fixes https://github.com/nathan-gs/ha-map-card/issues/27

Person entities allow you to define a set of device_trackers, each of which acts as a source of location data for the person. This PR adds the ability for the map card to detect configured device_trackers and pull both lat/lng and history data for them.

Key changes

ildar170975 commented 3 weeks ago

Sorry for interfering, would like to get more details about a nature of https://github.com/nathan-gs/ha-map-card/issues/27 and this PR. Consider some person entity. It has some device_tracker entity(-ies) associated. Assume it has 3 trackers - 1 router-based & 2 gps-based (like Life360 or Traccar). Coords from these trackers (except router-based one) are "passed" to the person entity (as described here). Only when a person is at home - then the router-based tracker is considered as the "latest" one and hence the person entity "looses" its coords (and hence not displayed on a stock map card - which is considered as a bad implementation by many users). So, coords of assoc. device_tracker entities are passed to a person entity. A "trajectory" on a map is a combination of "trajectories" of each gps-based trackers (most recent data are used). And I wonder what is a purpose of analysing assoc. device_trackers provided by this PR? Could you give some short explanation?

nathan-gs commented 3 weeks ago

@ildar170975 unfortunately my experience with the description of person only matches with the stock map, not with this map.

ildar170975 commented 3 weeks ago

@nathan-gs I believe that a stock map card takes data for a person entity from DB. The card does not know how these data were prepared (based on which trackers, on which algorithm, ...). So I believe that the custom:ha-map-card also uses the DB, yes?

An example from state_attributes table for some person entity: {"editable":false,"id":"ildar","latitude":55.xxxxxxxxxxxx,"longitude":37.xxxxxxxxxx,"gps_accuracy":200,"source":"device_tracker.iphone_5s_accum","user_id":"4c7b8d24fced4a6e863eda0e693b9ed3","friendly_name":"Ильдар","entity_picture":"/local/images/persons/ildar.png"

thybag commented 3 weeks ago

Hi @ildar170975,

So when using a person entity (ie. any entity that defines device_trackers), the changes i've made do two things 1) When a person is loaded, if lat/lng data are available on the entity itself, those are used 2) If lt/lng information is not currently available on the person entity (ie. its your home network which lacks lat/lng data), it will then traverse your trackers to find the latest lat/lang information available in order to allow the map to plot your marker.

In terms of history, no location data appears to be held against the person themselves, the only events i see are a few home/not_home presence flags on my own person entity for example. The historical location info appears to instead be held on the underlying trackers (I assume to avoid duplicating this in the recorder).

In this case my change will subscribe to events from all trackers linked to the person entity and plot any found in the date range regardless of the source tracker. Based on the original feature request, i understand this matches the functionality of the built in HA map card.

The marker location is all read from the live states information HA makes available, while the history is read using the websocket API to subscribe to the relevant event data.

thybag commented 3 weeks ago

As a TLDR;

From a history point of view https://github.com/nathan-gs/ha-map-card/issues/28#issuecomment-2135720502 probably adds better background on what I was aiming to solve.

From a marker point of view, the change is mainly this will now continue to plot person markers (and not throw warnings) even when your home and lng/lat data is no longer available on the person entity itself.

ildar170975 commented 3 weeks ago

If lt/lng information is not currently available on the person entity (ie. its your home network which lacks lat/lng data), it will then traverse your trackers to find the latest lat/lang information available in order to allow the map to plot your marker.

Very good. As I said earlier, many users find confusing when a person is not shown on a map if it is home.

In terms of history, no location data appears to be held against the person themselves, the only events i see are a few home/not_home presence flags on my own person entity for example. The historical location info appears to instead be held on the underlying trackers (I assume to avoid duplicating this in the recorder).

Sorry, I do not understand. DB seems to contain coords for a person entity, check here. You can see it yourself if open DB by smth like DBbrowser for SQLite. You may exclude device_tracker entities from recorder - and still see a path on a map for a person (honestly, never did it myself). Assume a person has 2 gps-based device_tracker entities associated. DB contains records for these device_tracker entities (incl. coords) as well as for person entity (also incl. coords). Coords for a person are set based on the latest update from any device_tracker.

From a history point of view #28 (comment) probably adds better background on what I was aiming to solve.

This case is rather strange for me. I will try to check this with a more attention.

thybag commented 3 weeks ago

Hmm, I've not actually had a look in the database to check tbh, I've mostly just been looking at the info the ha websocket returns for the entity. It may be there is somthing additional needed to return the relevent meta data.

From the websocket return data i assumed the sources (the currently selected device tracker) lng/lat and other attrs was merged in to the person at run time, hence why the attributes available change when the source does.

I've mostly tested this on myself so my marker is inherently always in the home state when I've looked so quite possible I've missed something.

ildar170975 commented 3 weeks ago

I excluded device_tracker domain from a Recorder on some test VM and then compare stock maps with a VM w/o that exclusion.

Update: here is my test. There is a gps-based device tracker. It is an only source for a person entity. Also, there is a template sensor which keeps latitude & longitude of that device_tracker.

изображение

Now let's compare 2 map cards: for this person and for this sensor - on 2 HA same setups with only difference: on setup A a whole device_tracker domain is excluded from Recorder. And db-files are deleted (before starting HA) for both setups. On both setups I see same picture (changes within ~1 hour): изображение

Since device_tracker data are missing in DB on setup A - then probably only person data are used.

Just in case - asked in Community (https://community.home-assistant.io/t/person-on-a-map-card-data-source/739682).

thybag commented 3 weeks ago

So, having a further dig in the db. There is state date for the person trackers (It seems to be due to significant_changes_only being set as true rather then false this is filtered in the api subscription). That said, its a lot less detailed than what i can pull from the combination of trackers.

Looking at my travel yesterday i have 30 datapoints under the person.carl, but over 100 under my gmaps device tracker, and 49 under my own tracks one. Combing them gives a lot more detail into my actual movements - especially those near my house which are missing entirely on the person state history (I guess cuz its swapped to router), which means my initial inbound/outbounds all appear to be missing.

I suspect the main map works around this by just connecting the history to the current icon, given they only deal with days ago, not any richer ability to jump around.

It's possibly worth getting a few other people to test using just their person.name entity vs a specific device tracker and see if they have similar results? For my own dataset, drilling into the trackers directly def seems to be the better option. I will flip the significant_changes_only flag tho, as i don't think that has the desired effect (and is explicitly off in the main card https://github.com/home-assistant/frontend/blob/dev/src/panels/lovelace/cards/hui-map-card.ts#L268 )

nathan-gs commented 3 weeks ago

It's OK to merge for me, WDYT @ildar170975 and @thybag ?

thybag commented 3 weeks ago

The implementation works quite nicely for my own Home Assistant setup, as prior to the change the person.* entities seemed to cause errors when I tried to use them on the map. So my default would be its g2g.

If @ildar170975 has concerns of a potential downsides to having the feature as standard I could maybe add a setting to disable it, although I'm not 100% on what that downside would be.

ildar170975 commented 3 weeks ago

Guys, I am awfully sorry, starting from today I do not have an access to any normal PC; cannot continue analysis. Will come back in a week or later. Let’s merge it. I will start testing and continue analysis when will be able. My best wishes!

Elu43 commented 2 weeks ago

Hello everyone, thank you for your work.

@thybag I saw you mentioned my comment about the missing points on the map. This release allow the card to show more points, thank you very much for this.

But now quite the opposite happens: there is too much points 😄 . It seems the person points and the tracker points are now overlaid :

Points are created when the person changes its zone (home -> not_home, only 2 points are created with a straight line) and when the device tracker updates its coordinates (a lot of points are created). Is there a way to hide the person points and keep only the tracker ?

Best regards

ildar170975 commented 1 week ago

Continued testing & comparing ha-map & stock map for a person entity. Imho fixing significant_changes_only is the RIGHT way. See no differences between ha-map & stock map within 24 hrs. Just in case - device_tracker domain was excluded from Recorder - although I still do not think that these entities are analysed for a stock map card (and do not see such a code in "hui-map-card.ts"). As I said, I saw no differences with an older version earlier as well - but that was just a brief & quick test. Thanks a lot, guys.