Unable to convert Devices[UnitiID].LastUpdate to datetime object

Python and python framework

Moderator: leecollings

Post Reply
akamming
Posts: 427
Joined: Friday 17 August 2018 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Unable to convert Devices[UnitiID].LastUpdate to datetime object

Post by akamming »

Hi,

I am trying to convert the Devices[UnitID].LastUpdate to a datetime object, so i can compare with another timestamp (which has the format of a unix timestamp). The timestamp has the string format "YYYY-MM-DD HH:MM", so i think i have to convert using the datetime.strptime function. but this one gives me " 'onStart' failed 'TypeError':''NoneType' object is not callable'."

i thought something something was wrong with the code i wrote, so to test i created a test.py script with a code example from the official python docs:

Code: Select all

import datetime
datetimeObj = datetime.datetime.strptime('2018-09-11 15:11:45', '%Y-%m-%d %H:%M:%S')
print(datetimeObj)
print(type(datetimeObj))
when i run this (python test.py or python3 test.py) outside domoticz plugin framework i get the correct output:

Code: Select all

2018-09-11 15:11:45
<type 'datetime.datetime'>
However when i copy the exact same code to my python plugin which is run as part of domoticz, i get the
" 'onStart' failed 'TypeError':''NoneType' object is not callable'." on the datetime.datetime.strptime line

if i change datetime.datetime.strptime() to datetime.datetime.fromtimestamp() or datetime.datetime.now() the error is gone again…

what am i missing here? and how can i get datetime.datetime.striptime() to work in a python plugin?


UPDATE:
I found the cause and a workaround.

The issue is not my lack of programming skills (pfjew…) , but a bug in python (https://bugs.python.org/issue27400). and i checked: This Py_Finalize is called a lot in the domoticz code, causing this bug to occur. Fortunately the workaround is also in that link:

instead of

Code: Select all

datetimeObj = datetime.datetime.strptime(string, format)
I am now using

Code: Select all

datetimeObj = datetime.datetime.fromtimestamp(time.mktime(time.strptime(string, format)))
not the neatest code (a few extra date/time format conversions), but it works...
Post Reply