@klogg @leSourCil
So I’ve been thinking about this whole “device timezone” topic some more and there’s a significant issue with adding an attribute that would contain the offset to the device’s timezone from HA’s configured timezone (or from UTC, for that matter.) The main problem is that that information could easily become wrong if either timezone changes DST between updates. Now I know that there usually isn’t much time between updates, and DST usually changes when people are sleeping and probably don’t care, and usually the two timezones would probably change DST at (roughly) the same time, so the relative offset wouldn’t change, and therefore, still be correct (or, at least, not be wrong for long), but… I think there are enough cases where this could be wrong that I’d rather not chance it.
Right now I’m thinking of keeping the same attributes, but instead of adding the times_as_local
config option, change it to times_as
with options of utc
, local
& device
, where utc
would be as things were before, local
would represent the (last_seen & at_loc_since) time attributes in the timezone of HA’s configuration, and device
would represent the time attributes in the timezone where the device’s GPS coordinates indicate.
If you want to know the current timezone offset to the device’s location, you could do that with this (assuming you used the times_as: device
option):
{% set n = now() %}
{{ (n.astimezone(state_attr('device_tracker.XXX', 'last_seen').tzinfo).utcoffset() -
n.utcoffset()).total_seconds()/3600 }}
That would show the offset from HA’s configured timezone to the timezone where the device is located (in floating point hours.) This works because, even if the device’s timezone has gone through a DST change since last_seen was updated, the tzinfo member is still valid, and can be used in the astimezone method to get the current timezone offset. To be clear, this would result in the current offset at the time the template is evaluated, regardless of how long ago the device_tracker was updated.
Now, the only remaining issue I see is, what if you want to display the (last_seen & at_loc_since) times in both the device’s timezone and HA’s configured timezone (i.e., the “local” timezone)? First, you’d have to use the times_as: device
option, and then the device_tracker’s “more info” display would only show the times in the device’s timezone. But you could still display the times in the local (HA) timezone in a template sensor with something like:
{{ state_attr('device_tracker.XXX', 'last_seen').astimezone(now().tzinfo) }}
Anyway, I think I’ll head in this direction unless I run into a snag, or someone has feedback to the contrary.