I compare two time objects, and if I put all the code in one script, it works.
When I move the function ‘getTime’ to a module in lib, I get an exception, at the line:
if aTime > now:
Here’s to code in automation\jsr223\python\personal\test.py
> from datetime import datetime
> from core.rules import rule
> from core.triggers import when
> import personal.utilities
> reload(personal.utilities)
> from personal.utilities import getTime
> @rule("TestNGE")
> @when("Item Test changed")
> def testNGE(event):
> aTime = getTime(items.TimeDuskLightsOff)
> now = datetime.now().time()
> if aTime > now:
> # do stuff
> pass
and the code in automation\lib\python\personal\utilities.py
> from datetime import time
> def getTime(timeRep):
> '''Takes a DecimalType as e.g. 1234 and returns a datetime.time object set to 12:34'''
> hours = timeRep.intValue() / 100
> minutes = timeRep.intValue() % 100
> return time(hours, minutes)
and here is the exception:
2020-01-27 16:46:25.204 [ERROR] [jsr223.jython.TestNGE ] - Traceback (most recent call last):
File "/etc/openhab2/automation/lib/python/core/log.py", line 51, in wrapper
return fn(*args, **kwargs)
File "<script>", line 45, in testNGE
File "/etc/openhab2/automation/jython/jython-standalone-2.7.1.jar/Lib/datetime.py", line 1284, in __gt__
_cmperror(self, other)
File "/etc/openhab2/automation/jython/jython-standalone-2.7.1.jar/Lib/datetime.py", line 390, in _cmperror
raise TypeError("can't compare '%s' to '%s'" % (
TypeError: can't compare 'time' to 'time'
2020-01-27 16:46:25.226 [WARN ] [e.automation.internal.RuleEngineImpl] - Fail to execute action: 1
org.python.core.PyException: null
If I add logging to check the datatypes, they are both the same: datetime.time
If I change the function to return a datetime.datetime, and also adjust the script, then it works ok.
Doing this on RPI3+ with openhabian and openHAB 2.5.1-2 (Release Build)
using Jython 2.7.1