Integer to binary

Hello…

PuuH. I have to admit: the rule engine based on extend drives me a bit crazy. I have to spent hours on finding the right syntax/ expressions and most I get from java examples just do not work.

I hope someone of you can give me a hint for this:

I wnat to simply convert an integer to binary.
I have found: “Integer.toBinaryString(intvalue)”

Using this in a rule throws an error:

  • Couldn’t resolve reference to JvmIdentifiableElement ‘toBinaryString’.
  • Couldn’t resolve reference to JvmIdentifiableElement ‘Integer’.

But why? Do I need to import more libraries?

This is what I import already:
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import java.lang.*

Thanx for any hint. Laso, if someone knows a ressource where to find xtend specific ways to do it. The online documentation does not cover a lot from my point of view…

cal

You can try java.lang.Integer.toBinaryString(intvalue). If that does not help, here is my working solution for using Bits:

rule "test operation mode"
// Items Heat_Opmode and Heat_Opset both are both of type Number
when
  Item Heat_Opmode received update
then
  var tmp = Heat_Opmode.state as DecimalType
  var state = tmp.toBigDecimal.toBigInteger
  logDebug("heizung.rules","Heat_Opmode:"+state.toString)

  if (state.testBit(0) ) {
        if (Heat_Opset.state!=1)
                Heat_Opset.postUpdate(1)
  }
  else if (state.testBit(1)) {
        if (Heat_Opset.state!=2)
            Heat_Opset.postUpdate(2)
  }
  else if (state.testBit(2)) {
        if (Heat_Opset.state!=3)
                Heat_Opset.postUpdate(3)
  }
  else if (state.testBit(3)) {
        if (Heat_Opset.state!=4)
                        Heat_Opset.postUpdate(4)
  }
  else {
        Heat_Opset.postUpdate(2)
  }
end

In fact, it isn’t that elegant, but at least it works.

In the Rule’s DSL, to reference static class methods you use “::” instead of “.”.

So it would be Integer::toBinary(intvalue)

Thank you very much…worked.

Hi, OK. Thank you.