How to iterate over a timezone list in (python) to find country(code)

2019-06-24 21:56:19.459063 WARNING AppDaemon: Excessive time spent in utility loop: 4120.0ms
2019-06-24 21:56:23.488546 INFO AppDaemon: Removing module /config/appdaemon/apps/address/address_old.py
2019-06-24 21:56:23.489123 INFO AppDaemon: Removing module /config/appdaemon/apps/address/._address_old.py
2019-06-24 21:58:46.903842 INFO AppDaemon: Reading config
2019-06-24 21:58:46.947424 WARNING AppDaemon: ------------------------------------------------------------
2019-06-24 21:58:46.948134 WARNING AppDaemon: Unexpected error loading config file: /config/appdaemon/apps/._apps.yaml
2019-06-24 21:58:46.949007 WARNING AppDaemon: ------------------------------------------------------------
2019-06-24 21:58:46.950306 WARNING AppDaemon: Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/appdaemon/appdaemon.py", line 1677, in read_config_file
    config_file_contents = yamlfd.read()
  File "/usr/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 37: invalid start byte
2019-06-24 21:58:46.950952 WARNING AppDaemon: ------------------------------------------------------------
2019-06-24 21:58:46.951579 WARNING AppDaemon: File '/config/appdaemon/apps/._apps.yaml' invalid structure - ignoring
2019-06-24 21:58:46.952574 INFO AppDaemon: /config/appdaemon/apps/._apps.yaml added or modified

I did simply comment out the previous config in the apps.yaml. Isn’t that allowed?

I have no such file as /._apps.yaml … no invisible files to be found

gave the add-on a full restart:

[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] 00-banner.sh: executing... 
-----------------------------------------------------------
 Hass.io Add-on: AppDaemon
 Python Apps and HADashboard using AppDaemon 3.x for Home Assistant
-----------------------------------------------------------
 Add-on version: 4.0.0
 You are running the latest version of this add-on.
 System: HassOS 2.12  (armv7 / raspberrypi3)
 Home Assistant version: 0.94.4
 Supervisor version: 166
-----------------------------------------------------------
 Please, share the above information when looking for help
 or support in, e.g., GitHub, forums or the Discord chat.
-----------------------------------------------------------
[cont-init.d] 00-banner.sh: exited 0.
[cont-init.d] 01-log-level.sh: executing... 
[cont-init.d] 01-log-level.sh: exited 0.
[cont-init.d] appdaemon.sh: executing... 
[22:07:21] INFO: Updating Hass.io API token in AppDaemon with the current one...
Collecting geopy
  Downloading https://files.pythonhosted.org/packages/80/93/d384479da0ead712bdaf697a8399c13a9a89bd856ada5a27d462fb45e47b/geopy-1.20.0-py2.py3-none-any.whl (100kB)
Collecting geographiclib<2,>=1.49 (from geopy)
  Downloading https://files.pythonhosted.org/packages/5b/ac/4f348828091490d77899bc74e92238e2b55c59392f21948f296e94e50e2b/geographiclib-1.49.tar.gz
Installing collected packages: geographiclib, geopy
  Running setup.py install for geographiclib: started
    Running setup.py install for geographiclib: finished with status 'done'
Successfully installed geographiclib-1.49 geopy-1.20.0
You are using pip version 19.0.3, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[cont-init.d] appdaemon.sh: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.
[22:07:36] INFO: Starting AppDaemon...
2019-06-24 22:07:42.384530 INFO AppDaemon Version 3.0.5 starting
2019-06-24 22:07:42.385185 INFO Configuration read from: /config/appdaemon/appdaemon.yaml
2019-06-24 22:07:42.389182 INFO AppDaemon: Starting Apps
2019-06-24 22:07:42.403647 INFO AppDaemon: Loading Plugin HASS using class HassPlugin from module hassplugin
2019-06-24 22:07:43.322137 INFO AppDaemon: HASS: HASS Plugin Initializing
2019-06-24 22:07:43.323685 INFO AppDaemon: HASS: HASS Plugin initialization complete
2019-06-24 22:07:43.325355 INFO Starting Dashboards
2019-06-24 22:07:43.357551 INFO API is disabled
2019-06-24 22:07:43.376351 INFO AppDaemon: HASS: Connected to Home Assistant 0.94.4
2019-06-24 22:07:44.573445 INFO AppDaemon: Got initial state from namespace default
2019-06-24 22:07:45.711421 INFO AppDaemon: Reading config
2019-06-24 22:07:45.747203 WARNING AppDaemon: ------------------------------------------------------------
2019-06-24 22:07:45.747883 WARNING AppDaemon: Unexpected error loading config file: /config/appdaemon/apps/._apps.yaml
2019-06-24 22:07:45.748509 WARNING AppDaemon: ------------------------------------------------------------
2019-06-24 22:07:45.763168 WARNING AppDaemon: Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/appdaemon/appdaemon.py", line 1677, in read_config_file
    config_file_contents = yamlfd.read()
  File "/usr/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 37: invalid start byte
2019-06-24 22:07:45.763871 WARNING AppDaemon: ------------------------------------------------------------
2019-06-24 22:07:45.764655 WARNING AppDaemon: File '/config/appdaemon/apps/._apps.yaml' invalid structure - ignoring
2019-06-24 22:07:45.765656 INFO AppDaemon: /config/appdaemon/apps/apps.yaml added or modified
2019-06-24 22:07:45.766396 INFO AppDaemon: /config/appdaemon/apps/._apps.yaml added or modified
2019-06-24 22:07:45.767049 INFO AppDaemon: /config/appdaemon/apps/apps.yaml added or modified
2019-06-24 22:07:45.767651 INFO AppDaemon: /config/appdaemon/apps/._apps.yaml added or modified
2019-06-24 22:07:45.768254 INFO AppDaemon: App 'hello_world' added
2019-06-24 22:07:45.769066 INFO AppDaemon: App 'address' added
2019-06-24 22:07:45.769968 INFO AppDaemon: Adding /config/appdaemon/apps to module import path
2019-06-24 22:07:45.770967 INFO AppDaemon: Adding /config/appdaemon/apps/address to module import path
2019-06-24 22:07:45.773250 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/hello.py
2019-06-24 22:07:45.810108 WARNING AppDaemon: No app description found for: /config/appdaemon/apps/address/._address.py - ignoring
2019-06-24 22:07:45.810917 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/address/address.py
2019-06-24 22:07:45.816593 INFO AppDaemon: Initializing app hello_world using class HelloWorld from module hello
2019-06-24 22:07:46.142052 INFO hello_world: Hello from AppDaemon
2019-06-24 22:07:46.146462 INFO hello_world: You are now ready to run Apps!
2019-06-24 22:07:46.147249 INFO AppDaemon: Initializing app address using class Address from module address
2019-06-24 22:07:46.152469 INFO address: App started.
2019-06-24 22:07:46.157021 INFO address: State listener for ['device_tracker.life360_1', 'device_tracker.life360_2', 'device_tracker.life360_3', 'device_tracker.life360_4', 'device_tracker.life360_f5', 'device_tracker.life360_6'] started.
2019-06-24 22:07:46.159279 INFO AppDaemon: App initialization complete

It looks like you have a ° sign in your config it does not like.

And it does not loop though your list (indicating that you have an earlier version (before I added support for that)) :slight_smile:

deleted the #commented section, causes this to log:

2019-06-24 22:11:34.255593 WARNING AppDaemon: ------------------------------------------------------------
2019-06-24 22:11:34.256964 WARNING AppDaemon: File '/config/appdaemon/apps/._apps.yaml' invalid structure - ignoring
2019-06-24 22:11:34.271020 INFO AppDaemon: /config/appdaemon/apps/._apps.yaml added or modified
2019-06-24 22:12:16.636048 INFO AppDaemon: Reading config
2019-06-24 22:12:16.660325 INFO AppDaemon: /config/appdaemon/apps/._apps.yaml deleted
2019-06-24 22:12:16.660882 INFO AppDaemon: /config/appdaemon/apps/apps.yaml added or modified

so apparently the ._apps.yaml is now deleted :wink:

huh, dl’d the file after you told me to in the few posts above… will check again.

—update—

the new file is loaded now, it says listening to all my listed device_trackers.

What should I expect to see happening now? Should the raw attributes of the first version now be seen as attributes on the device_tracker.life360_name itself?

Yes, on the next state update of each of them it will be populated (like the image in the repo)
You can “force” an “update” from /dev-states

As for your original question:

TZ_INFO = [{'timezones': ['Europe/Amsterdam'], 'code': 'NL', 'continent': 'Europe', 'name': 'Kingdom of the Netherlands', 'capital': 'Amsterdam'}]
TZ_MATCH = 'Europe/Amsterdam'
for tz in TZ_INFO:
    if TZ_MATCH in tz['timezones']:
      # We found a match
      print(tz['code']) # Use the matched "code"
      break

its happening again!:

got to tag @pnbruckner here because we discussed adding this feature to the integration itself, and this is now possible. So cool, thank you so very much.

1 Like

let me compare that with 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]

which I posted earlier…

the TZ_INFO should in my original post be populated with the complete list of course, possibly loaded from file
TZ_MATCH would be the attribute.time_zone of the device_tracker listed in some config for the script

the print should the be replaced with has.states.set…

Yeah…

And that would not be that easy, hence my suggestion to use a custom_component/AppDaemon :slight_smile:

though I would still love to be able to do that, if only for educational purposes, this AD app using the geopy package is so much more precise, it is a bit scary really… many more details than the on the list, and dynamic at that.
Hope this is secure and no eavesdropping is possible.

Anything using a external webserver for any kind of data can theoretically be tracked.
This is the documentation for that lib https://geopy.readthedocs.io/en/latest/#

thanks! I will thoroughly read that :wink:

final question for now:
How can I comment in the configuration file? apparently AD doenst like the # I used, but since I do that all the time in my python scripts, I thought I could here also. Apparently not.

You can comment :thinking:

---
# Add stuff to device_tracker entities
address:
  module: address
  class: Address
  entity:
    - device_tracker.my_entity1
    - device_tracker.my_entity2

hi, getting back to the App please.
All of a sudden, I see incorrect time_zones popping up. May very well be they were incorrect before and I simply hadn’t noticed, but still.

On 2 of my devices, I see an incorrect time_zone, while the lat/lon are correct, and the address and country etc which is added by the App are all fine and perfect.

Does this App replace the time_zone? Or could it be the life360 has an incorrect time_zone.
it can’t be that when the lat/lon are correct, an incorrect time_zone is displayed (stating another city with a completely different lat/lon set…)

As far as I can tell that info is not added/replaced with that app.

update:
Have a look at the sample image in the repo, no TZ there :slight_smile:

thanks for confirming, Ill check life360 again for erroneous time_zones…
and indeed, my second system, still without the App has the life360 device with the identical erroneous time_zone. Never noticed that before!

so much the better we use the geopy component to check on lat/lon, and not have it do what I originally wanted, check for the time_zones… :wink:

update
to be in line with the discussion at the Life360 CC thread: above, read ‘time zone name’ where I wrote ‘time zone’…

@ludeeus
checking the sure code at Geocode would it be possible to also display a distance? its mentioned on the docs. Would be nice to have in the attributes.

seocndly, about the time_zone_names: would the timezone.py in the Geopy GitHub be any good in producing a correct time zone name. If so, could an attribute time_zone_name be added to the App (don’t want to overwrite the existing ‘time zone’ attribute of the device_tracker.

so the App could then add/append the currently added attributes for Address, and add Distance and Time zone name to that?

thanks for having a look!

quite, but it displays … indicating there’s more than displayed in the image… Tried to read through the code, but couldn’t find a list of attributes it creates for ‘address’

Here you can see a list of address info it returns https://nominatim.org/release-docs/develop/api/Output/#json

I don’t think TZ or distance calculations have a place in a app designed for address information.

Now would be a great time for you to take the next step in your AD venture, by creating two new apps, one to get TZ info, and one to calculate distance :+1:

Hi @ludeeus

I left an issue on the github, maybe you didn’t notice or it is offline:
would you please check whats up with the app address? I can’t get it to work after updating to Appdaemon 4.0.

discussed about that update here, though even that change from self.get_state(entity=entity) to self.get_state(entity_id=entity) or even self.get_state(entity) doesn’t solve the issue.

could it be the final set_state should be changed also?

thanks for having a look, if you can find a moment.

need to add a state. You’re setting the state attributes but not the actual state.

change

        attributes = self.get_state(entity_id=entity, attribute="all")["attributes"]

to

        stateobj = self.get_state(entity_id=entity, attribute="all")
        state = stateobj['state']
        attributes = stateobj["attributes"]

and change

        self.set_state(entity, attributes=attributes)

to


        self.set_state(entity, state=state, attributes=attributes)

thanks Petro, figured the bottom-line myself, and added the other change now too. I don’t see any errors logged anymore, but nothing I happening either…

could this then be important after all?

WARNING: You are using pip version 19.2.3, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

no, add log lines and debug it.