From openhab2 to openhab4

Hi,
I started with nearly the first available openhab version (0.x) in 2012/2013. The rules was written in Java. With changing to the openhab version 2 I was very happy, because of using python many thinks was really easy… functions could be imported and used in different rules, variable names could be changed dynamically.

But now I wanted to change to version 4 and it seems that python isn’t supported any more.
I hope that is a misunderstanding from my side. Please, please, correct me if I’m wrong.

If not, what is the preferred, powerful language to use, if I don’t want to change for the next openhab versions?

All my python files, have 10677 lines of code. I would be happy to reuse them.

Thanks for every kind of feedback,
Seb

That’s definitely not the case. Please follow this topic

1 Like

Hi Hans-Jörg,

thanks a lot for your feedback. The hope is rising again :slight_smile:

I added to my addons.cfg

automation =  jythonscripting

And after restart I can see that it’s installed
grafik

But then I got the error message

Error during evaluation of script ‘/etc/openhab/automation/jython/python/personal/wetter.py’: ImportError: No module named core in /etc/openhab/automation/jython/python/personal/wetter.py at line number 1

My script starts like this

from core.rules import rule
from core.triggers import when
import math
from datetime import datetime
from org.joda.time import DateTime
from org.joda.time import Days
from core.actions import PersistenceExtensions
from core.date import to_python_datetime    #https://community.openhab.org/t/jython-elapsed-time-millis-and-joda-time-functions/81570
from org.eclipse.smarthome.model.script.actions.Exec import executeCommandLine

from core.actions import HTTP
import json
import subprocess

# für Test folgende 2 Zeilen aktivieren
#import personal.mylib
#reload(personal.mylib)
from personal.mylib import fncZeicheTabelle

Do I need to replace the core or did I missed something?

Thanks,
Seb

I did a mistake when copy the lib folder.
Instead of

python libs: $OPENHAB_CONF/automation/jython/lib

I copied the files to

python libs: $OPENHAB_CONF/automation/jython/lib/python

But now I run in following error

2024-07-25 00:44:54.492 [INFO ] [ort.loader.AbstractScriptFileWatcher] - (Re-)Loading script ‘/etc/openhab/automation/jython/python/personal/wetter.py’
2024-07-25 00:44:55.256 [WARN ] [.influxdb.InfluxDBPersistenceService] - Re-queuing 9 elements, failed to write batch.
2024-07-25 00:44:55.800 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script ‘/etc/openhab/automation/jython/python/personal/wetter.py’: java.lang.NullPointerException: java.lang.NullPointerException: Cannot invoke “Object.equals(Object)” because “o” is null in /etc/openhab/automation/jython/python/personal/wetter.py at line number 2
2024-07-25 00:44:55.801 [WARN ] [ort.loader.AbstractScriptFileWatcher] - Script loading error, ignoring file ‘/etc/openhab/automation/jython/python/personal/wetter.py’

Also check out HABApp
it is python

one addition to the NullPointerException:
When starting openhab service it looks like

2024-07-25 01:22:02.653 [INFO ] [ort.loader.AbstractScriptFileWatcher] - (Re-)Loading script '/etc/openhab/automation/jython/python/core/000_Startup.py'
2024-07-25 01:22:03.624 [WARN ] [jython.startup                      ] - 

*******************************************************************************
Jython version:             2.7.3.final
Operating system:           Linux
OS Version:                 6.6.20+rpt-rpi-2712
Java vendor:                Debian
Java VM name:               OpenJDK 64-Bit Server VM
Java runtime name:          OpenJDK Runtime Environment
Java runtime version:       17.0.11+9-Debian-1deb12u1
configuration.py installed: True
sys.path:                   /etc/openhab/automation/jython/lib
                            /var/lib/openhab/cache/org.eclipse.osgi/302/0/bundleFile/Lib
                            __classpath__
                            __pyclasspath__/
*******************************************************************************

2024-07-25 01:22:03.625 [INFO ] [ort.loader.AbstractScriptFileWatcher] - (Re-)Loading script '/etc/openhab/automation/jython/python/core/components/100_DirectoryTrigger.py'
2024-07-25 01:22:04.265 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script '/etc/openhab/automation/jython/python/core/components/100_DirectoryTrigger.py': java.lang.NullPointerException: java.lang.NullPointerException: Cannot invoke "Object.equals(Object)" because "o" is null in /etc/openhab/automation/jython/python/core/components/100_DirectoryTrigger.py at line number 8
2024-07-25 01:22:04.266 [WARN ] [ort.loader.AbstractScriptFileWatcher] - Script loading error, ignoring file '/etc/openhab/automation/jython/python/core/components/100_DirectoryTrigger.py'
2024-07-25 01:22:04.268 [INFO ] [ort.loader.AbstractScriptFileWatcher] - (Re-)Loading script '/etc/openhab/automation/jython/python/core/components/100_StartupTrigger.py'
2024-07-25 01:22:05.279 [INFO ] [ort.loader.AbstractScriptFileWatcher] - (Re-)Loading script '/etc/openhab/automation/jython/python/personal/aircondition.py'
2024-07-25 01:22:07.997 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script '/etc/openhab/automation/jython/python/personal/aircondition.py': ImportError: No module named joda in /etc/openhab/automation/jython/python/personal/aircondition.py at line number 2
2024-07-25 01:22:07.998 [WARN ] [ort.loader.AbstractScriptFileWatcher] - Script loading error, ignoring file '/etc/openhab/automation/jython/python/personal/aircondition.py'

hate to ping but help from @holger_hees maybe
volunteer maintainer at this time thank you Holger!

1 Like

Hi,

I guess you are using the helper libraries which are not working anymore, because the openhab api changed a lot in the meantime and there is no one who maintains this library anymore.

I took over the maintainer role of python scripting binding and I will rewrite/create a new helper library based on my own learning during the last years. It will be based on my own helper script, but cleaned and simpler.

For now, you can use only the openhab/python core api without the helper library.

Hi Holger,

thanks for jumping in.
What does that mean for my openhab4 installation? Can I use rules like…

@rule("Fenster wird geschlossen")
@when("Item EG_Kind3_FensterKontakt_FensterOffen        changed to CLOSED")
@when("Item EG_Garderobe_FensterKontakt_FensterOffen    changed to CLOSED")
@when("Item EG_Wohn_FensterKontakt_TuerTerrasseOffen    changed to CLOSED")
@when("Item EG_Kueche_FensterKontakt_TuerReOffen        changed to CLOSED")
@when("Item EG_Kueche_FensterKontakt_TuerLiOffen        changed to CLOSED")
@when("Item EG_WC_FensterKontakt_FensterOffen           changed to CLOSED")
@when("Item OG_Bad_FensterKontakt_FensterOffen          changed to CLOSED")
@when("Item OG_Kind1re_FensterKontakt_FensterEckOffen   changed to CLOSED")
@when("Item OG_Kind1re_FensterKontakt_BalkonTuerOffen   changed to CLOSED")
@when("Item OG_Kind2li_FensterKontakt_FensterEckOffen   changed to CLOSED")
@when("Item OG_Kind2li_FensterKontakt_BalkonTuerOffen   changed to CLOSED")
@when("Item OG_Buero_FensterKontakt_FensterOffen        changed to CLOSED")
@when("Item OG_Schlaf_FensterKontakt_FensterLangReOffen changed to CLOSED")
@when("Item OG_Schlaf_FensterKontakt_FensterHochLiOffen changed to CLOSED")
@when("Item OG_AnkleideFensterKontakt_FensterOffen      changed to CLOSED")
def rFensterWirdGeschlossen(event):
  rFensterWirdGeschlossen.log.warn(u"Das {} wurde geschlossen".format(event.itemName))
  for rollladen in lutRolJalMitFenster:
    # welches Fensterkontakt gehört zum Fenster
    if lutRolJalMitFenster[rollladen][lutRolJalMitFensterKontakt] == event.itemName:
      # gibt es etwas zu tun?
      # holen den letzten Treffer in der Liste
      # loesche gespeicherte Werte
      restorePosIfPossible(rFensterWirdGeschlossen.log, rollladen, rmAll=True)
      try:
        tFensterOffen[event.itemName].cancel()
        del tFensterOffen[event.itemName]
        rFensterWirdGeschlossen.log.error(u"Timer fuer Fensterkonakt {} wurde geloescht".format(event.itemName))
      except:
        pass
      # Treffer es kann abgebrochen werden
      break

If yes, how can I use/install your helper scripts to my openhab4 installation?

Thank you!

for now, I do not official support my helper libraries. They are just for people who like to explore and provide technical feedback.

During the next months (maybe September/October) I will provide a first official preview of my rewritten helper libraries in an external git project. Preview means the api is not final and complete and the target group are early adapters who wants to influence the final version.

When the new helper libraries are becoming stable, I will merge and include them in the official openhab release.

If you want to explore my helper libs, just clone and copy the files from github to $OPENHAB_CONF/automation/jython/lib/shared

and import them with

from shared.helper import rule, getItemState, sendCommand
from shared.triggers import CronTrigger, ItemStateChangeTrigger

but keep in mind the final version will be different.

and complete rule example can be found here

btw. the main motivation for rewriting the helper libraries is to remove the complexity. I will only implement features to cover ~95% of use cases. This will reduce the code size a lot and will decrease the maintenance effort for the future. The rest has to be done by accessing the openhab api directly, which is still possible, because every openhab api is available in jython.

i think this approach is enough for most of us.

Ultimately you are going to have to rewrite your rules to work in OH 4. Not to be glib, but that’s the cost that comes with not upgrading more frequently.

If you want to keep using Python you have two choices.

  1. Use HABApp which runs separately from and in parallel to openHAB, interacting with OH through the REST API. It’s very powerful, offers Python 3 (Jython currently only offers the long since end of life Python 2 which still leaves me uncomfortable) and has a following. But it’s an independent implementation of rules so your existing rules will largely need to be rewritten.
  2. Use the Jython automation which I would label as experimental at this point given it doesn’t have a completed helper library yet. And since the helper library is a rewrite, you’ll have to rewrite your rules accordingly.

Outside of Python you have a number of choices. Since you have to largely rewrite anyway they are worth a review.

  • Blockly: great if you are not already a programmer, maintainers are very active
  • JS Scripting: probably the most popular language right now, Blockly “compiles” into JS Scripting, the maintainers are very active
  • jRuby: very powerful and complete with a vibrant community of users, maintainers are very active
  • Rules DSL: I don’t recommend new development in Rules DSL; all the other options are better, support for new (and even some old) features is lagging behind
  • Groovy: has a following but lacks good documentation, I think it uses the raw Java APIs so it doesn’t really need much active development.
1 Like

Thanks Holger,
I will try if after the summer break.
Then I will try your helper libs from github, knowing that it’s still on work.
Lot’s of thanks for your help and project work!