OH3 UI Python rules - how to obtain the Event object/argument

I’m struggling to create even a very simple Python rule using the OpenHAB3 Rules UI.
I have multiple JavaScript rules triggered by various events and all of them works fine. Now I need far more complex logic, so I decided to use Python. Recent hours I spent browsing the web, but without almost any result.
Here is the very very simple Python rule created with OH3 UI:

from org.slf4j import LoggerFactory
log = LoggerFactory.getLogger("org.openhab.rule.demo")
log.info("Hello World!")
log.info("event=" + event)

Running this example gives me the following output:
2023-06-02 20:34:17.469 [INFO ] [org.openhab.rule.demo ] - Hello, World!
2023-06-02 20:34:17.472 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘5ba2f2675f’ failed: NameError: name ‘event’ is not defined in at line number 4

I.e., it can be executed (great), but the code doesn’t knows anything about the event object/argument (which JavaScript rules has). So, how can I get the event?

When possible, please don’t offer to install another 3rd party solution like HabApp. Or just tell me that in opposite to JS UI rules, Python ones doesn’t supports the event object neither the OpenHab items registry, action, etc.

An additional question. If there is a positive answer to my question, can I declare and instantiate Python classes at the OH3 Python UI-created rule? Maybe you can provide a tiny 5-lines example how to do this?

I recommend against starting anything new using Jython. It doesn’t really have anyone in OH supporting it any more (hasn’t been since just before the release of OH 3) and even upstream is barely being supported. It’s still Python 2.7 which is years past it’s end of life at this point. And it’s never been optimized for use in UI rules nor even been updated to support OH 3 UI rules. In fact this is the first post I’ve seen of anyone even trying to create a UI rule using it and OH 3 has been release for more than two years now.

If you really want to use Jython you have to write rules in files, not the UI. If you really just hate JavaScript, look at the jRuby add-on, or Groovy add-on. Otherwise I recommend sticking with JavaScript, but use the new GraalVM JS Scripting add-on, not the old Nashorn JS.

:person_shrugging: It should be there but, like I said, I know of no one using Jython in UI rules. Maybe something needs to change in the add-on to make event available in UI rules. Unfortunately no one has done it and I don’t see anyone stepping up to support it. But if it’s not there, there’s nothing you’ll be able to do about it from your end. It’s going to require changes to the add-on.

Sorry, it is what it is. It does appear that in the UI something is broken about Jython and event isn’t available. I could lie to you if you prefer. But if you insist on Python, your two options are file based Jython or HABApp.

You are pretty much on your own here.

:person_shrugging: I see no reason why that wouldn’t be supported. But I also see no reason why event isn’t there so who knows. And since event isn’t there the whole issue is moot. The last time the Jython add-on was touched was prior to OH 3.0 release and a lot has changed in the past couple years with UI rules which the add-on has not kept up with.

Thank you for the great clarification. And twice thanks for your Sensor Reporter project.

So I tried to find today something that doesn’t exists. :joy:
Well, I not hateJS. I’m just trying to integrate a NxPanel for which the nspanel-lovelace-ui project IMHO is the better firmware and controller. HomeAssistant unfortunately. In overall it’s not a rocket science to me to integrate this firmware in OpenHAB. But there is a lot of Python code, the project contains thousands lines. Great job BTW.
Ok, I’ll evaluate other approaches. Thanks again for the great answer.

@westward Have you managed the NSPanel Lovelace integration for openHAB?