Parsing an MQTT message in rules


val checkKthBit = [ Integer num, Integer bit |
    val n = num.intValue
    val k = bit.intValue

    if ((n & (1 << (k - 1))) == 1) True else False // shift to the kth bit and check if it's 1
]

if(checkKthBit.apply(MyItem.state, 0)) // device from first bit is ON
if(checkKthBit.apply(MyItem.state, 1)) // device from second bit is ON
...

I am not at all certain that the above will work. I think Xtend will let us use the Java binary shift operators. If not I’m not sure how to do this.

If you use Scripted Automation you can use Python (specifically Jython 2.7), JavaScript (specifically Nashorn), or Groovy. See [beta testers wanted!] Jython addon w/ helper libraries (requires OH 2.5.x) to get started with Python.

1 Like