The pythonscripting binding for openHAB is the long term successor of the jython binding.
It is testet in openhab 5.0.0 and 4.3.x and provides the following features.
Based on graalpy which provides python3.11 support
Included openhab helper library. The main focus is to make the openHAB core API accessible without encapsulating everything again. (Keep it simple and easy to maintain.)
Dependency tracker to reload your rule if an imported module (lib) is updated.
Caching of compiled *.pyc files (to improve startup performance)
Jython emulation (We may remove the feature to reduce the number of error sources - you can still use the jython binding)
Encapsulated datetime <=> ZonedDateTime handling. Everything is now a native python datetime object.
@when and @onlyif decorator support
Easy to use code profiler just by â@rule(profile=1)â
Python and old jython binding can run in parallel for a seamless migration of old scripts
I made also my own custom âtoolboxâ lib on top of it, where Iâm not sure if some functionalities should be part of the openHAB helper library. (particularly getGroupMemberTrigger and getFilteredGroupMember).
Performance
I ported all my ~9000 lines of jython based python2 code to python3 which is now running with the new python binding. Everything works, but it looks that graalpy is 2-5 times slower then jython. If you run a pure python benchmark, it should be up to 4 times faster according to the graalvm devs. In openhab, however, most of it depends on the Python=>Java bridge, because there is a lot of interaction with java objects. This seems to be the reason for the âbadâ performance. For comparison, I reimplemented some rules with the openhab-js binding and got similarly slow results.
Still, I think itâs acceptable because itâs more future-proof. It offers Python 3.11. It offers more features than the Jython binding (e.g. dependency tracker, object wrapping for datetime objects, helper lib). And finally, I think it doesnât matter if a script runs 0.1ms or 0.8ms
Most of my scripts run between 0.5ms and 8ms. More complex scripts have increased their runtime from 30ms to 60-70ms.
Credits
A lot of thanks goes to @jsjames . He helped, especially in the beginning, to deploy graalpy dependencies correctly. Without this help we would not be where we are now.
I also forgot to mention support from graalpy developers with a lot of helpful hints!
Iâm not sure this will work as a market place posting. Youâve deviated from the template in a number of important ways. Have you tested it by installing the add-on from MainUI-> Add-on Store â Bindings or possibly Automation.
You may need to enable âshow unpublishedâ in Settings â Community Marketplace if you havenât already.
Typically only one resource is allowed per post so you would usually need separate postings for the OH 4 and OH 5 versions and use a support versions string in the title (e.g. Debounce [4.0.0.0;4.9.9.9]). The second link needs to be to the license and there sooner be nothing after the links.
You should not see from the template because OH has to parse it. Itâs not just read by humans.
This performance is probably also slower because we donât have the Graal compiler available for both Python and JS as we run in a stock JDK.
Have you benchmarked on GraalVM?
Hello Holger,
thank you for your efforts to bringing python3 in OH.
I have a quick question:
Iâm using some .py files that are based on datetime. So far, I havenât been able to determine a local time value; I always get GMT.
Is there a standard method to get datetime with the time zone set in OH?
I looked deeper in my timezone issue, but was not successful finding the root cause.
What I found out, that I also have this issue in Python 2.7 Scripting.
I created a workaround for me.
@tpnrw maybe a little bit more details would help.
can you show us the output of you default timezone
from openhab import logger
import time
logger.info(str(time.tzname))
additionally I like to know
how are you deploying openhab. Is it running as a container?
if yes, is the environment variable, I mentioned, configured?
is the runtime configuration âorg.openhab.i18n:timezoneâ correct
what version of openhab are you running
which os are you using
what java version
if you running on linux, the output of âdate +%Zâ would be helpful too. If you run a container, the previous mentioned command should be run inside the container.
how are you deploying openhab. Is it running as a container?
directly in linux
root@its:~# lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 12 (bookworm)
Release: 12
Codename: bookworm
if yes, is the environment variable, I mentioned, configured?
no container, but set EXTRA_JAVA_OPTS="-Duser.timezone=Europe/Berlin"
in /etc/openhab/linux.parameters
is the runtime configuration âorg.openhab.i18n:timezoneâ correct
set org.openhab.i18n:timezone=Europe/Berlin in /etc/openhab/services/runtime.cfg
what version of openhab are you running openhab-addons/stable,now 4.3.2-1 all [installiert]
which os are you using
see above
what java version
root@its:~# java --version
java 17.0.11 2024-04-16 LTS
Java(TM) SE Runtime Environment (build 17.0.11+7-LTS-207)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.11+7-LTS-207, mixed mode, sharing)
my workaround:
because no timezones are availble; I use zoneinfo.reset_tzpath(("/usr/share/zoneinfo",)) to relaod all available timezones from OS and create default_timezone = zoneinfo.ZoneInfo("Europe/Berlin") which I use in every call for calls i.e.
Timezone can come from one of three places: the OS, MainUI, and arguments passed to the JVM. Itâs the last one that usually causes problems. To address this one needs to pass the timezone as an argument to the JVM when starting OH by adding it to EXTRA_JAVA_OPTS in /etc/default/openhab. E.g.
-Duser.timezone=US/Mountain
There is an option in openhabian-config that does this for you but if you are not running openHABian you need to do this yourself.
Iâm not sure we really know why this is sometimes required but usually not required.