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

Then that configuration goes in apps.yaml

a ok, thanks.
About address: : should that really be called address, or can I see
address_me:
address_she:
address_his:
address_her:

etc

–edit–

sorry for the crossposts… got it thanks @petro and @ludeeus

now how does this create the sensors? do I have to automate that, or are the created automagically?

edit 2

automagically!! how cool this is
6 perfect sensors appearing the the dev-state…yes!

only thing is, the state is enormously long, could that be changed to only show eg the country, and the rest be created as attributes (which they are already as I suppose)?

https://github.com/ludeeus/ad-address/blob/4a0c02f04805b55b5ded52f22c02e4f05ae0682a/apps/address/address.py#L37
Cahnge that line to:

        address = data.raw["country"]
1 Like

thanks. changed it, but the sensors don’t adjust just yet. Does that only happen at restart? or can I manually force the new address/py to be reloaded

Sensor updates when device tracker updates.

For AD Apps it does a “live reload” when you save :slight_smile:

This I can confirm, it still does with the full address as state…

this doesn’t apply here, Ive saved, but as written above, the sensors still have their full address state as before.

shouldn’t this line be adapted too:

        self.set_state(self.args["name"], state=address, attributes=raw)

maybe like this:

       data = geo.reverse(lat_long)
        address = data.address
        raw = data.raw["address"]
        country = data.raw["country"]

        self.log("Updating state for {}".format(self.args["name"]))
        self.set_state(self.args["name"], state=country, attributes=raw)

Should be like this:

        data = geo.reverse(lat_long)
        address = data.raw["country"]
        raw = data.raw["address"]

        self.log("Updating state for {}".format(self.args["name"]))
        self.set_state(self.args["name"], state=country, attributes=raw)

Try the latest version instead https://github.com/ludeeus/ad-address

Instead of creating a new “useless” sensor, it appends the address data to the device_tracker entity.

To make it easier I made this possible to (as the config):

address:
  module: address
  class: Address
  entity:
    - device_tracker.my_entity
    - device_tracker.his_entity
    - device_tracker.her_entity
1 Like

ok so Ive changed my setup, but not sure what to look for… the address sensors are gone, and my device_trackers.life360_ which I setup to track, don’t have anything appended to them
tried a force update in the dev-service without change in attributes I am afraid.

That should have worked…
Any errors in the addon log?

yes as a matter of fact,

2019-06-24 21:47:55.334392 INFO AppDaemon: Reloading Module: /config/appdaemon/apps/address/._address.py
2019-06-24 21:47:55.335362 WARNING AppDaemon: No app description found for: ._address - ignoring
2019-06-24 21:48:09.459775 INFO AppDaemon: Reloading Module: /config/appdaemon/apps/address/._address.py
2019-06-24 21:48:09.462190 WARNING AppDaemon: No app description found for: ._address - ignoring

or is that the previous file I saved as address_old.py

Looks like it, try to remove all old files out of the /config/appdaemon/apps dir.

yes, I had already done that, even cleared the pycache…
maybe nobody moved, and the plane cuts of the gps signal…

You should still have a log like this:

2019-06-24 20:00:08.625343 INFO AppDaemon: Reloading Module: /config/appdaemon/apps/address/address.py
2019-06-24 20:00:08.628048 INFO AppDaemon: Initializing app address using class Address from module address
2019-06-24 20:00:08.629355 INFO address: App started.
2019-06-24 20:00:08.630124 INFO address: State listener for device_tracker.my_entity started.
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