Examples of variable declaration in rules file

I have some trouble with syntax of variable declaration. Basically, I would like to declare a bytes array that will be used for encryption. The Java syntax would be like :

byte[] keyBytes = new byte[] { 's','o','m','e','b','y','t','e','s' };

But it would be really appreciated to have a lot more examples.

Is there any guide of items/scripts files programming/coding ?

The Rules DSL does not support arrays directly and is admittedly somewhat awkward in how it does support ArrayLists and HashMaps.

You would need to do the following instead:

val List<Byte> keyBytes = newArrayList(new Byte('s'), new Byte('o'), ...)

The Rules DSL is documented on the Rules wiki page and to some degree on the Xtend Language Docs, the language to which the Rules DSL bares the most resemblance. You can also learn a lot from looking at the examples on the lower right corner on the wiki page.

If you are going to need to do a lot of this sort of programming which, honestly, does not come up much in a typical OH deployment, you might consider looking into the JSR233 Binding and code some or all of your rules in Jython as opposed to the Rules DSL.

I’m an advocate for the Rules DSL. I think it is a particularly well suited language for an event driven environment. But I do recognize that there are cases where other languages make more sense and this may be one of them, given the little info you have provided.

1 Like

I will do my researches about your solution and I might have other questions.
Thanks for your time