Simplified Jython rule definition (similar to Rules DSL) using a universal decorator

Nevermind… got it working and reproduced the issue! Same for Javascript. I’m also wondering if this may be due to a change put in for the script load order…

1 Like
# requires rule and when imports
ruleInst1 = rule("Group Test Rule")(
            when("Time cron 10 0/1 * * * ?")
                (GroupLister("ruleInst1")))

How are multiple when clause coded here?

I can’t test this currently, but I’m pretty sure it is:

ruleInst1 = rule("Group Test Rule")(
            when("Time cron 10 0/1 * * * ?")(
            when("Member of gLights changed")
                (GroupLister("ruleInst1"))))
1 Like

Yes it is (magic)
Thanks

It’s worth reading rules.py and when.py. That is how I puzzled this out for myself.

Have fun!

Just to inform you:
Since I’ve restarted OH two days ago, I could not reproduce the issue again.

1 Like

Hi Scott,

Have found anything what could cause this problem?
Is there anything Ican test?

I’ve been working on other things, but will get to this soon. Could you please open an issue in openhab-core? Maybe someone else will get to it before I can.

@5iver let me know if you’d rather me start a new thread for this.

Now that https://github.com/openhab-scripters/openhab-helper-libraries/issues/141 has been fixed I’ve started to convert more rules over but ran into an issue where my existing DSL rules had used UoM e.g. var threshold = 20|lx.

Can something like this be done already or is there another way of doing it?

What are you planning to do with it? I don’t have anything that reports in lx, but in Jython, you can create a QuantityType like this…

test = QuantityType(u"5 W/m²")

You wouldn’t need to use unicode for lx.

Perfect, that works nicely!

I have coded some thresholds values into my rules which I use to compare to item states to determine whether an alert or action needs to be taken.

Was there any update on this?
I have exactly the same issue (Windows, python scripts fired when touched, but not on startup).

Unfortunately, no. The issue is still open…

I’ve looked into it, but haven’t figured it out.

@niebochod, with the help of your debugging, I was able to fix the issue on Windows where scripts were not starting when OH starts. Here is the PR…

This PR will end up in OH 3.0, but I’ve made a test jar that is built to work with OH 2.5.x. If you’d like to try it (or anyone using scripted automation with Windows!), use your console to uninstall this bundle…

bundle:uninstall org.openhab.core.automation.module.script.rulesupport

Then shutdown OH, download this jar, and copy the jar into your /addons/ directory. To test, restart OH and your scripts should start on their own.

If/when you clear the OH cache, like when updating OH, you will need to repeat these steps since the old bundle will be reinstalled and there will be errors due to also loading the test bundle. To remove the test bundle, delete the jar and clear the cache.

6 Likes

Works great! Thanks!

1 Like

Thanks. Works also for 2.5.3.

1 Like

For OH3 will there also be an option to have true Python 3 and support for all libraries and modules? A combination of hab app and jython with its direct access to all binding functions etc would be a dream come true.

It’s possible, but unlikely. Development of Jython 3 is supposedly being started up again, since the release of Jython 2.7.2. So, it is possible that there could be some kind of release of Jython 3 before OH 3.0. If it is in a usable state before a release, I could do a build and setup an add-on for it. Just be patient… we’ll get there!

GraalVM is a possibility, but it looks to be a bit of a dud and the adoption of it is low. Well, except for companies/apps migrating from Nashorn, since the focus has been on graal-js due to Nashorn being removed in JDK15. graal-python is still in a very immature state, but javax.script does not look like it is going anywhere anytime soon. Whatever the case, there are many more important things that should be worked on for OH automation.

If you absolutely have to use Python 3 libraries with scripted automation, you can do so using executeCommandLine to call a Python script. It’s not optimal, but it works and you don’t lose the functionality of scripted automation or the helper libraries.

Hi @5iver,

I have a question. Not knowing if this is the best place for it or if I should start a new topic for it.

I started migration my DSL rules to Jython rules. Now I am very much interested in using automation Conditions in my rules. How is that possible. Do you have an example or a link for me?

It would be possible, but I have not implemented Conditions in the helper libraries and may never get to it, since the functionality is the same as using conditional statements in the script. Keep in mind, the Triggers still trigger, even if the Conditions are not met, so you’re really not saving anything.