[beta testers wanted!] Jython addon w/ helper libraries (requires OH 2.5.x)

Tags: #<Tag:0x00007f616f1f74a8> #<Tag:0x00007f616f1f72c8>

I really didn’t spend that much time analyzing the steps to deconflict the instructions like you have. I’m sure you are right, they weave together a little more tightly than the simple pattern I laid out.

Ok, thanks. But that already happens as a consequence of step 7 of the helper library instructions.

  1. …copy the contents of the /Core/ directory into your openHAB’s site configuration directory…

And, you can’t really skip step 7 because that creates the needed directory structure under conf.

Therefore, before starting openHAB, should I delete the automation/jsr223/python/community and automation/jsr223/python/personal directories that were created by step 7?

Yes, they do.

@5iver WDYT about creating a new set of instructions that describe how it do a first time install using the new scriptenginefactory? If you want people to test this, they need a relatively straightforward way to do it. I usually can follow directions pretty well (despite what my wife might say to the contrary), but I had a hard time with this.

2 Likes

I can imagine. You kind of need to read between the lines and merge some steps with XOR logic. :grinning:

But the instructions for copying the core directory and step 7 overlap. With the addon most directories are not needed anymore in conf/automation, just the one mentioned by @5iver , but it won’t hurt if they are still there.

Ok, thanks. I’ll hit the start button and see what happens. :crossed_fingers:

1 Like

Here are the steps that worked for me.

  1. Make sure you’re running OH 2.5.x (S1778) or newer
  2. The Next-Generation Rule Engine is still in development, so it is recommended to use the latest snapshot or milestone release of openHAB.
  3. Use the Karaf logging commands to enable debug logging for automation: log:set DEBUG org.openhab.core.automation . Leave this on for setup and testing, but you may want to set to WARN when everything is setup. For older openHAB builds before the ESH reintegration (older than snapshot 1566 and 2.5M2), you will need to change this to org.eclipse.smarthome.automation .
  4. Enable debug logging for jsr223: log:set DEBUG jsr223 . This is the default logger used in the examples and the helper libraries.
  5. Review the general openHAB Scripted Automation documentation and the language specific documentation found as links on that page.
  6. Install the Next-Generation Rule Engine add-on.
  7. Shut down openHAB and make a backup
  8. Download the contents of this repository. Using the openhab account, copy the contents of the /Core/ directory into your openHAB’s site configuration directory (Linux, Windows). This will create a directory structure as described in File Locations and will contain all of the Core files for each language, including the startup delay scripts that ensure openHAB has started completely before loading other scripts. If you do not plan to use all of the languages, you may want to remove the directories for them under /automation/jsr223/ and /automation/lib/ .
  9. There is a main configuration file for each scripting language’s helper libraries that must be renamed. For example, in /automation/lib/python/ , rename the file configuration.py.example to configuration.py . These files can be used to secure personal information, so that they are not stored in files that may be shared with the community, or to store variables that are accesed by multiple scripts and/or modules, e.g. LOG_PREFIX.
  10. The ‘’/automation/lib/python/personal/__init.py.example`` file must be renamed to __init__.py . If you modify it, take care not to overwrite the file during upgrades.
  11. Download the bundle that matches your OH version to OPENHAB_CONF/addons/.
  12. Copy the /Script Examples/Python/hello_world.py script to /automation/jsr223/python/personal/ .
  13. Start openHAB and watch the logs for errors and the entries from the Hello World script. This script will make a log entry every 10s and should be deleted after installation has been verified.
  14. Ask questions on the openHAB forum and tag posts with jsr223 . Report issues here, but please don’t hesitate to update the documentation and code. Just like openHAB, this repository is community driven!
  15. Check out the First Steps page for what to do next.
4 Likes

Yes, the instructions need an update. Now that it’s been tested, I will be updating the instructions in the HL repo to use the bundle.

I guess my #1 and #2 can be merged into one step.

PR? :wink:

What’s that? LOL

There are a few more changes coming, including splitting out the helper libraries (maybe). I’ll be setting up a skeleton so that others can easily put together library bundles too. But I’ll get the instructions updated. I’m due for a big push.

Right now, I’m working on getting the component scripts to launch from inside the bundle, so that there will be nothing left to copy other than the configuration.py, but I may put that in as configuration for the library bundle (no file).

3 Likes

I’ve come across the problem that I failed to import a new setting from configuration.py and found out that 'configuration$py.class' was not updated.
I think that’s the precompiled version and actually used rather than configuration.py itself but never understood how that is supposed to work and when compilation takes place.
Can I manually start (re-)generation ?

Had a script already imported configuration.py before you added the new setting? If so, you’ll need to reload the configuration module or restart OH. The same will happen whenever you update a module. This is not something that is specific to the Jython bundle (same for manual install).

Yes but that’s not the point. I had deleted 'configuration$py.class'and it did not get regenerated.
Inserting reload(configuration) gave
Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/python/personal/battery.py': NameError: name 'configuration' is not defined in <script> at line number 9
Only inserting (unconditional) import configuration finally did the trick.

No need for that. To reload a module, you need to import it first, before it can be reloaded…

import configuration
reload(configuration)
from configuration import my_module

Is this not clear in the docs? If not I’ll try to do better explaining how to reload a module.

Yes it’s clear in the docs, no need to change that. Albeit it’s pretty unintuitive that you need to use import twice, I had removed the first import which is what ultimately caused my issue.

1 Like

Not sure if it is addon related or just my config but I tried to use the items.<itemname> shortcut - instead of `ir.getItem(“itemname”).state - but it fails:

[INFO ] [me.core.service.AbstractWatchService] - Loading script 'python/personal/test.py'
[ERROR] [org.openhab.automation.jython       ] - Traceback (most recent call last):
  File "/openhab/userdata/cache/org.eclipse.osgi/203/1/bundleFile/Lib/core/log.py", line 51, in wrapper
    return function(*args, **kwargs)
  File "/openhab/userdata/cache/org.eclipse.osgi/203/1/bundleFile/Lib/core/rules.py", line 108, in execute
    self.callback(inputs.get('event'))
  File "<script>", line 18, in testRule
AttributeError: 'org.openhab.core.automation.module.script.internal' object has no attribute 'Test_Item'

For this test rule:

from core.rules import rule
from core.triggers import when
from core.log import logging
import configuration
reload(configuration)
from configuration import LOG_PREFIX

@rule("Test Rule")
@when("System started")
def testRule(event):
    testRule.log = logging.getLogger("{}.testRule".format(LOG_PREFIX))
    if items.Test_Item == ON:
        testRule.log.info("Test_Item is ON")        
    else:
        testRule.log.info("Test_Item is not ON (probably OFF)")

and this item:

Switch  Test_Item

Using your code, I got the same error. I than swapped out the Item for one that exists on my system and it worked fine. There’s some cleanup you can do in this code too. Since you are using the rule decorator, the function will have a log attribute added to it already. I also added another way of getting an Items state, so if it errors there, you need to check your Item definition.

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

@rule("Test Rule")
@when("System started")
def testRule(event):
    if items["Test_Item"] == ON:
        testRule.log.info("Test_Item is ON")        
    else:
        testRule.log.info("Test_Item is not ON (probably OFF)")
    if items.Test_Item == ON:
        testRule.log.info("Test_Item is ON")        
    else:
        testRule.log.info("Test_Item is not ON (probably OFF)")

Thanks Scott, for your quick reply.

This works:

    if items["Test_Item"] == ON:

This doesn’t:

    if items.Test_Item == ON:

I’ll turn on debug logging to see what’s going on.

That’s odd. How did you create the Item?