In the process of rewriting rules from DSL to JSR223, I failed to implement bitwise AND operations. I am not familiar in identifying packages, classes and methods that can be applied within JSR223. My present way is rather searching for similar code pieces, and if do not find any, I am on stuck like here. So if anybody has a best practice to do better, this would be great help.
Here is the current problem. The code in Rules DSL is:
val HvacMode = (hvacState as Number).intValue.bitwiseAnd(0x0F)
In my corresponding JSR223 rule, I have already working:
hvacState = items[event.itemName]
But now I fail to apply the bitwise AND operation onto hvacState.
Thank you very much for your attention and your hints.
You can use the ‘&’ operator:
items[event.itemName].state & something
I’m not sure though if you need to cast the state to an int first but I think this should work.
Thank you for confirming the use of the & operator for bitwise AND in JSR223 as in conventional Python.
The variable hvacState returns already a state of DecimalType, so that I assume there is no need to apply .state; items[event.itemName] should be sufficient.
As DecimalType does not allow bitwise operations, I need to cast it to integer. This is how I got it working, but my solution looks rather weird:
hvacMode = int(float(str(hvacState))) & 15
hvacState is DecimalType, so I cannot directly cast it to int or float. Therefore, I cast it to string first. Although my values are effectively integer values, there are in float representation so that casting from string to int does not work. Therefore I ended up with int ← float ← string ← decimal type.
Now my questions to the experts: How could I do it better?
DecimalType has an
intValue() method to get a int:
hvacMode = hvacState.intValue() & 15
should be sufficient.
Thank you very much for your hint to use .intValue() - works perfectly!
What would be the suggested way to find out what classes and methods are applicable? I know about the OOP concepts in general so that I would need to identify the class hierarchy and to identify suitable classes. But where/how to search?
I use VSCode with OpenHAB extension, and I do not see any suggestions/word completions.
When you are working with anything that represents something like an Item or a State or interacts with openHAB you are working with Java Objects. So you can look at openHAB’s JavaDocs: Overview (openHAB Core 3.1.0-SNAPSHOT API) or the Java 11 JavaDocs.
So, for example ZonedDateTime use used when interacting with OH (e.g. creating timers). So it must be a Java class and therefore you would look at the Java docs for Java 11.
A bitwise and operation is going to be core to what ever language you are using. Python doesn’t know anything about how to deal with a java.lang.Number. That’s a Java Object. Looking at the language docs for Python 2.7 shows it works with primitive int values. So you need to convert that Number to a primitive int by calling toInt.
Note, there are some classes built into Java I think that can help you process binary data. so you could keep the Java Objects but then you have to use the Java classes to do the operations.
@peter_tau you should look into my other repository containing Python Stubs for OH. You will find a link in the Ivan’s Helper Libraries post. Once you have them installed, VSCode will be able provide autocompletion hints.
Thank you Michael. The Python Stubs are now installed!
Hopefully soon I will have some time to get a simpler process made and be able to generate them more regularly. Though the only version published currently may seem outdated, most of OH Core is very stable so you probably won’t run into issue when writing rules.
Make sure you follow the instructions carefully or they may nit work. You will also find a couple of pages in the repo docs that will help you get the most out of them.