Migrating my rules to Jython

I have started to slowly migrate my rules to the NGRE.

However at first I have some problems and I couldn’t find an answer for these at anywhere (even at the helper libraries docs).

Have this rather simple rule:

from core import *

@rule("Everyone at Home", description="Updates the state of Everyone_Home item", tags=["presence"])
@when("Member of gPhoneHome changed")
def everyone_home(event):
    everyone_home.log.info("Updating Everyone_Home state...")
    if ir.getItem("Phone_Home").state == ON:
        events.postUpdate("Everyone_Home", "ON")

The warnings I got immediately:

  • ir is undefined. Where are these defined? What should be included? The docs only says the you can use ir.getItem("Item").
  • Also the ON state is also interpreted as an undefined variable. Are there any place here in the NGRE where are these constants are defined and be used in rules, so I don’t need to use it them as String?
  • events are also undefined.

Thanks for your help

This is unnecessary, but you’ll need…

from core.rules import rule
from core.triggers import when

ir is included in the default script scope (it’s in the ‘default’ preset)…

Same as above

This one too

How did you install Jython and what version of OH? I’ve never seen the default script scope not come through.

It’s a little more efficient to do this…

if items["Phone_Home"] == ON:
1 Like

I might know what my problem is here.
I have used Python in Visual Studio Code for other projects thus I have the Python extension (or what it is called) installed in VSCode. This only does static code checks and that’s why these warning appear here.
And I couldn’t see it working, because that Member of group didn’t received a changed yet…

Thanks this looks simpler too!

Oh! These are the linter warnings in VSC! Check out this post to clean those up. I’ll be making a commit soon to the HL repo for this. I’ve actually added some more to it, but this should get you by.

Thanks! I know that something like this existed!

What is the root of my workspace in this case? The whole openhab-conf SMB share or the automation folder or what?


Ps.: Saw your reply in the other thread about this… I will try!

Wherever the workspace file is located… probably at the root of openhab-conf.

Placing it in the root of openhab-conf doesn’t help. I will try to find out where it will work.

This is where mine is located…

I remember getting it setup at first was tricky. I think restarting VSC helped, but I also enabled/disabled the linter.

I managed to get it working. Placing in the conf folder is right.
However after a restart VSCODE said that there is a pylint file, should pylint be enabled or something like this. But after pressing Yes, the same happened.
Opening the command palette and selecting Python Linter as pylint helped.

However if you can specify what should be turned off… Can it be somehow enabled (I mean specifying these in the pylintrc file) to auto-complete these default variables? Like you said items, and ir, events, etc… are in the default scope so it will be always available, so only a static list of these variables would be enough and really could help in the migration process, especially in typos…

I have saved this rule in the personal folder, but when it should trigger nothing happens. I suspect that I least should see the logging in the openhab.log but not. Even saving the file doesn’t give any output. Should it give something similar to Rules DSL when saving a file?

Yes… I had spent some time refining this and have had a commit that I’ve been testing since early November that needs to be pushed… similar to what you see in the PR that Michael submitted.

Read through the setup instructions again. You probably did not set the logging level for jsr223. Out of curiousity, did you manually install jython or are you trying out the bundle?

I don’t think so. When restarting openHAB I can see that the initial scripts (“StartupTrigger.py”) is loading. I suspect the same should happen when I save a file but not. A restart is required when saving a new file?

Nope. What do you see when you execute this in the console?


Logger                                             │ Level
ROOT                                               │ WARN
javax.jmdns                                        │ ERROR
javax.mail                                         │ ERROR
org.apache.karaf.jaas.modules.audit                │ INFO
org.apache.karaf.kar.internal.KarServiceImpl       │ ERROR
org.apache.karaf.shell.ssh.SshUtils                │ ERROR
org.apache.karaf.shell.support                     │ OFF
org.eclipse.lsp4j                                  │ OFF
org.eclipse.smarthome                              │ INFO
org.jupnp                                          │ ERROR
org.openhab                                        │ INFO
org.openhab.ui.paper                               │ WARN
org.openhab.ui.paper.internal                      │ INFO
org.ops4j.pax.url.mvn.internal.AetherBasedResolver │ ERROR
org.ops4j.pax.web.pax-web-runtime                  │ OFF
smarthome.event                                    │ INFO
smarthome.event.InboxUpdatedEvent                  │ ERROR
smarthome.event.ItemAddedEvent                     │ ERROR
smarthome.event.ItemRemovedEvent                   │ ERROR
smarthome.event.ItemStateEvent                     │ ERROR
smarthome.event.ThingAddedEvent                    │ ERROR
smarthome.event.ThingRemovedEvent                  │ ERROR
smarthome.event.ThingStatusInfoEvent               │ ERROR

Ok, I know what the problem is now.
It worked before, but yesterday I have updated to 2.5 stable and changed to release through openhabian-config. That replaced the /etc/default/openhab2 file to the default one, so there is no extra Java opts there.

Your org.ops4j.pax.logging.cfg file was probably wiped during the upgrade. Set the logging level again for jsr223…

log:set DEBUG jsr223

If you do not have an EXTRA_JAVA_OPTS, Jython would not work. Unless you are using the Jython bundle…

Ok, thanks I will try!

I have added the extra JAVA_OPTS again, I will retry after a restart!

How can I specify the used Persistance service when calling the previousState?

I can only find this example in the docs:

PersistenceExtensions.previousState(ir.getItem(“Weather_SolarRadiation”), True).state

I want to transfer this to Python:

ArmCurrState.previousState(false, “mapdb”) != NULL



from core.actions import PersistenceExtensions

if PersistenceExtensions.previousState(ir.getItem("ArmCurrState"), False, "mapdb").state != NULL:

But this would never be true, since NULL is never persisted.

But I need the previousState not the time when changed.
And I need to use mapDB and mapDB is not the default Persistance service for me, that’s why I have to specify there to use that one.
What is the “True” in the previousState call?