Jython / problem with type checks on event states when used in imported files

I have troubles with type checks for event types :frowning: - hopefully someone can help me out.
I have simplified the problem down to a few lines in two files:


isinstance(33, (int, PercentType, OnOffType))

Saving the above (which automatically runs the script) gives me no errors. I double checked with testing a bogus type FooType instead of int - this does result in an error message.

What I want to be able to do, is type-checking in a helper script, which I import:


import personal._type_test_import2
isinstance(33, (int, PercentType, OnOffType))


isinstance(33, (int, PercentType, OnOffType))

This results in an error message:

2020-04-28 12:17:26.084 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/openhab/conf/automation/jsr223/python/personal/_type-test2.py': NameError: name 'PercentType' is not defined in <script> at line number 1

What is going on here? Why is “PercentType” known as a type in a script, but not in an imported script?

I found a (the?) solution: I can import the missing types:

from core.jsr223.scope import events, items, NULL, UNDEF, ON, OFF, OPEN, CLOSED, PercentType, OnOffType

Can someone please explain, if this is the right way to go, and also why are the types already present in the script in the jsr233 folder, but not in the lib?

Aaaaand I think I also found something autoriatative with regards to the “why”. I am adding it here for anybody who might hit on the same problem.


One of the challenges of scripted automation with Jython is that modules
imported into scripts do not have direct access to the JSR223 scope types and
objects. This module allows imported modules to access that data.

You got it. Modules are not scripts, and they have a different scope than scripts do. Scripts all have a default set of objects injected into the scope through ScriptExtension presets. To access those objects within a module, you will need to use the core.jsr223 module to import them.

This is in the documentation, but an updated version is coming soon…


I’ve mentioned this a few times in the forum too :slightly_smiling_face:


1 Like

@5iver - thank you for your explanation!
I never really doubted that an explanation to my problem would be somewhere in the documentation or in this great forum. I just failed to find the right terms to search for and after trying searching and reading through bits and pieces of the reference for a while, I decided to reach out :slight_smile:

I hope I didn’t come across as though that was a bad thing! Ask anything!