How to make a hex/binary transform within item definition?

How do I transform a hex strings first 7 bit to a number within an item definition?
An regex returns a string of hex values that contains the information, e.g.: (hex=BA00… > bin=1011101000000000… > first7bit=1011101 > dec=93). The goal is to have the 93 as value in the number item.

Can I use a transform script in the item definition, but how to combine regex and transform?

If you can code the extraction of the 1’s and 0’s from the String and convert them to a number in JavaScript then you can use the JavaScript transform. Otherwise, you cannot do it within the item definition and will have to do the above in a Rule.

Thank you. As a rule is more flexible then a transform I decided to write a rule. A short minimal working example looks like this:

rule "MyDecodeRule"
  Item MessageString changed 
  // MessageString = "bla bla MessageData=ba0000; bla bla"
  var MessageData = transform("REGEX", ".*MessageData=([0-9a-f.]+)?;.*", MessageString.state.toString())
  // MessageData = "ba0000"
  var StringBuilder MessageDataBin = new StringBuilder
  for (char ch: MessageData.toCharArray()) {
    var i = Integer.parseInt(ch.toString(), 16)
    var s = String.format("%4s", Integer.toBinaryString(i)).replace(' ', '0')
  // MessageDataBin = "101110100000000000000000"
  logInfo("MyDecodeRule", "MessageData = "+MessageData+" > "+MessageDataBin)
  var MyDecodedValue = Integer.parseInt(MessageDataBin.substring(0,10), 2);
  // MyDecodedValue = "93"
  logInfo("MyDecodeRule", "MyDecodedValue = "+MyDecodedValue)
1 Like