Track people's( devices) presence per country?

Hi,

Might be a silly question, but would it be possible to track devices and show in which country they are. Can’t do that with a Zone, since that would need lat/lon settings.
Since my family is often abroad for professional and study purposes, Id like to show lets say the flag of the country they’re in.

Thanks for having a look.

Yes, but it’s probably not trivial.

You’d need to use, say, Google Maps travel time, and extract the country from the attributes, then use that as a lookup key somehow. I found however that if it can’t work out directions, you don’t always get the start address.

After I posted this, I realized I have the timezone of these devices, so I would need a way to calculate the country based on this known timezone.

Found this How to convert timezone to country code in Python? - Stack Overflow but as I put it in the Life360 thread (I wrote after creating this topic, sorry for the confusion or crosspost) am lost as to how I could realize that in either Python or plain Jinja…

fyi, all my life360 trackers show the current timezone they’re in. Seems very simple for us humans to immediately know the someone is in Europe/Rome the country is Italy, but how to extract that from a computer…

List of timezones https://gist.github.com/pamelafox/986163

yes, thanks!

so id need to go from the given timezone, to the ‘name’ in this list.
I need some help doing so though …

At a high level, it’s either going to be a crazy long list of elif statements, or you can name the flags as the timezone:

data_template:
  entity_picture: >-
      /local/flags/{{ state_attr('device_tracker.whatever','timezone_attribute_here') }}.jpg

that’s a start yes, thanks!

still this wouldn’t lead me to the countries would it?
I’d now have to create a crazy long list of flag_pictures :wink:

say I am here:

  "time_zone": "Europe/Vatican",

and wanted to use the Italian flag, I would need to convert all these italian timezones to the same flag… and that for each countries existing timezones. or am I missing something.

the link I provided apparently enables the timezone-country conversion, which would allow for a sensor based on the timezone, and then allow me to use your suggestion, based on that sensor.

a bit like I do now:

      - type: custom:button-card
        template: button_picture_family
        entity: device_tracker.life360_name
        entity_picture_template: >
          return '/local/tiles/family/name_' + entity.state.toLowerCase().replace(' ','_') + '.png';

but then with an extra timezone condition.

Yes, you’d need a long set of files, named for the timezone. It’s that or a long, long, long, long set of elif statements.

Naming files for the timezone means it’s visually sane too. You’ll have a folder for Europe/ and then a bunch of files inside. You can even use symbolic links to create the duplicates. So you could create an image in Europe/ called Italy.png and then ln -s Italy.png Vatican.png; ln -s Italy.jpg Rome.png (and so on).

you’d have to help me here, Ive never done symbolic links in Hassio templates…could you give me 1 example how that should be written please?

secondly, and please indulge me, is there truly no way available of iterating over this list Python list of country codes, names, continents, capitals, and pytz timezones · GitHub , look for the attribute time_zone of a given device_tracker and simple report the country_code or name that goes with it. Seems to be completely feasible, and the list isn’t that horrifically long is it? The list seems made for these kind of searches?

Many Ui-lovelace.yaml files are a tenfold of it :wink:

it would be a second best way, only topped by an internal python function, which I woud have hoped this:

At any rate, this is how you can identify the countries that use a given timezone, like this:

{country for country, timezones in country_timezones.items()
if timezone in timezones}

If you need to do lots of these lookups, you can of course build your own dict to make it faster and simpler:

timezone_countries = {}
for country, timezones in country_timezones.items():
for timezone in timezones:
timezone_countries.setdefault(timezone, set()).add(country)

And now it’s just:

timezone_countries[timezone]

could have been…please let me carefully ask @pnbruckner and @petro to join in and have a look?

Well, the link wouldn’t be in the template, what I posted there is how you create the files themselves. All of that is entirely out of Home Assistant.

You could certainly do what you’re proposing in HA, I just don’t know how - and for me it’s easier and faster to handle it in the OS. That too makes everything faster, there’s no fancy lookup.

Please, go read number 16 in this post

sure, I did before, and thought I complied completely. And carefully selected my fellow members, because there’s a frequent exchange of ideas , advice, testing and helping out going on. Not a careless tagging at all.

just to keep you up to date, @ludeeus just made this possible in AppDeamon, see all about it on How to iterate over a timezone list in (python) to find country(code)