Problems with the Rule Language

I really hate the used language. The Documentation is really bad i think and a lot of thinks are not running or well described.

For example i just try to check if the time is between 12pm and 2pm in other langues i would think to write it like that

if(now.getHour() >= 12 and now.getHour() <= 13)

  1. I can not find that “and/or(&&/|| or different)” operators are allowed
  2. I can not find if there is something like getHour (i saw getDay and getYear in a sample)
  3. i got errors trying to use the >= and <= operator

Can someone explain me how to handle this with that language?

1 Like
if (now.getHourOfDay() >= 20 && Gate_Show_OC.state==2 && !sentGarageNote || now.getHourOfDay() <= 5 && Gate_Show_OC.state==2  && !sentGarageNote )

Checks if Gate is open between 8PM and 5:59 AM …

At the risk of hijacking the thread…I think part of his question (and mine) is “how can we find for ourselves the syntax of the language and the API of built in functions?”. The only thing I have found so far is a comment on the website that the language is Java-like. Examples can only take one so far.

John

The rules and scripting language is a Domain Specific Language (DSL) written on the Xbase programming language. Xbase is itself built on the Java Runtime Environment and therefore has access to Java libraries and classes, but Xbase is not any more Java like then Clojure is Java like.

The documentation for Xtend which is also built on Xbase is here and it is where the wiki directs users to look. However, be aware that openHAB uses a DSL that is based on Xbase. Therefore there are certain things you can do in Xtend that you cannot do within your rules or scripts, such as create your own classes or use the shorthand for creating lists and maps.

The easiest way I’ve found to figure out what you can and can’t do in rules is to use Designer. Designer is based on Eclipse and the wonderful <ctrl>-<space> key combo to see what can complete what you are typing is the most helpful.

In the original example, I would type now.get<ctrl>-<space> and a menu will pop up showing all of the methods on now that start with “get”. You can even just type now.<ctrl><space> and see all the methods in now. This also works for completing Item and Group names.

I agree that the documentation of the DSL is lacking and perhaps someday I’ll have time to start a page to document all the things that are different between it and Xtend. In the mean time, the Xtend documentation is the place to look.

And for the record, both the Scripts wiki page and the Rules wiki page give a high level outline of the syntax as well as pointing you to the Xtend documentation.

2 Likes

Same problems here, as a .Net Developer it’s pretty hard to find solutions based on the problem I have.
Big thanks to Rich, spreading out alot of knowledge and solutions.
It’s really not that easy without a good IDE and well documented programming language.
But it works! at most of the times :wink:

VSCode with the openHAB extension is as good as any ide I’ve ever used. It had syntax checking code completion and code snippets. And since it supports LSP, the exact same parser that OH uses to lead the files is what VSCode uses to check the syntax.

Keep in mind that the above was posted over three years ago. A lot has changed since then.

I see these pages for Scripting and Rules:
Scripting: https://www.openhab.org/docs/configuration/jsr223.html
Rules https://www.openhab.org/docs/configuration/rules-dsl.html
Are these the proper pages which are referred to above? Thanks!

If I’m talking about Wiki pages this is something posted years ago in reference to OH 1.x.

At that time when I posted the above, Scripts were (and are) a way to write a little chunk of callable Rules DSL code in another file. It still uses Rules DSL syntax. So your Rules link applies to both Rules and Scripts.

JSR223 are an alternative to Rules DSL that lets you write your Rules in other programming languages like Jython, JavaScript, or Groovy. Your “Scripting” link is to the docs for this alternative.

And in OH 2, we now have the Experimental Next Gen Rules Engine which will let you write Rules in PaperUI using any of the JSR223 languages (hopefully). But take note of the “experimental” in the name. It’s not yet ready for prime time. Documentation doesn’t really exist yet but I’ve started work on them here: Experimental Next-Gen Rules Engine Documentation 1 of : Introduction