sj3fk3
(Greg)
April 21, 2018, 6:52am
21
My brain is hurting
self.log("Plain: {}".format(self.datetime()))
self.log("utc: {}".format(self.datetime().replace(tzinfo=timezone.utc)))
self.log("last_changed: {}".format(self.get_state(entity = sensor,attribute="last_changed")))
2018-04-21 08:47:00.009016 INFO bathroom mls: Plain: 2018-04-21 08:47:00
2018-04-21 08:47:00.010194 INFO bathroom mls: utc: 2018-04-21 08:47:00+00:00
2018-04-21 08:47:00.010914 INFO bathroom mls: last_changed: 2018-04-21T06:36:56.108860+00:00
mine is always hurting.
as i see it then you didnt configure your device right.
the time from your device must be set to local time.
sj3fk3
(Greg)
April 21, 2018, 11:37am
23
I’m not sure what you mean. My linux box has timezone Europe/Amsterdam. I think it’s setup right? If I type ‘date’ I get the localtime not UTC.
Local time: Sat 2018-04-21 14:05:21 CEST
Universal time: Sat 2018-04-21 12:05:21 UTC
RTC time: Sat 2018-04-21 12:05:21
Time zone: Europe/Amsterdam (CEST, +0200)
Network time on: yes
NTP synchronized: yes
RTC in local TZ: no
sj3fk3
(Greg)
April 21, 2018, 12:43pm
24
{{utcnow() - states.binary_sensor.sensor_5.last_changed}}
{{now() - states.binary_sensor.sensor_5.last_changed}}
{{utcnow()}}
{{now()}}
outputs:
0:00:43.125920
0:00:43.126023
2018-04-21 12:41:02.689752+00:00
2018-04-21 14:41:02.689763+02:00
yeah sorry.
i always get confused also with those things.
i did some testing and this works for me: ( after i installed “pip3 install python-dateutil”)
import appdaemon.plugins.hass.hassapi as hass
import datetime
from dateutil import tz
class test(hass.Hass):
def initialize(self):
tzlocal = tz.tzoffset('UTC', 7200)
utcnow = datetime.datetime.utcnow().replace(tzinfo=tz.tzutc())
now = utcnow.astimezone(tzlocal)
lastchanged = self.convert_utc(self.get_state(entity = "sensor.rene",attribute="last_changed"))
lastchangedlocal = lastchanged.astimezone(tzlocal)
self.log("Plain: {}".format(self.datetime()))
self.log("now: {}".format(now))
self.log("utc: {}".format(utcnow))
self.log("last_changed: {}".format(self.get_state(entity = "sensor.rene",attribute="last_changed")))
self.log("last changed local: {}".format(lastchangedlocal))
sj3fk3
(Greg)
April 21, 2018, 12:46pm
26
I’m going to give that a try… Don’t you agree it’s a lot of boilerplate just to get the time-diff of a last_changed? I would very much prefer this to be a build in util in AppDaemon
the problem isnt AD but HA.
last changed and last updated where always buggy and troublesome.
but you can create your own general function like:
def make_local(self,datestring):
tzlocal = tz.tzoffset('UTC', 7200)
lastchanged = self.convert_utc(datestring)
lastchangedlocal = lastchanged.astimezone(tzlocal)
return lastchangedlocal
then you can use:
self.make_local((self.get_state(entity = “sensor.rene”,attribute=“last_changed”))
or a bit more specific:
def get_local_last_change(self,entity):
tzlocal = tz.tzoffset('UTC', 7200)
lastchanged = self.convert_utc(self.get_state(entity = entity,attribute="last_changed"))
lastchangedlocal = lastchanged.astimezone(tzlocal)
return lastchangedlocal