Converting a Hex String to a Number using parseInt

Tags: #<Tag:0x00007f616fcd90b0> #<Tag:0x00007f616fcd8ed0> #<Tag:0x00007f616fcd87f0>

Hi,

I’m trying to convert the hex number output of a battery pack to a number item using parseInt inside a rule. So far my attempts have been giving me persistent errors.

Heres the rule:

rule "Battery Level"
when Time cron "0/15 * * * * ?" //run every 15 seconds
then
   var string = executeCommandLine("i2cget -y 1 0x62 0x4 b",10000) 
   logInfo("Hex",string)

   var int value = Integer::parseInt(string) 
   logInfo("Test",value)
end

Log Output:

2020-10-17 19:22:15.067 [INFO ] [g.eclipse.smarthome.model.script.Hex] - 0x61

2020-10-17 19:22:15.072 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘Battery Level’: For input string: “0x61”

I’ve also tried giving parseInt a string directly parseInt(‘0x61’) however it still results in the above error, same with parseInt(‘0x61’, 16)

Any help would be appreciated I’ve been stuck on this for hours now :sleepy:

var int value = Integer::parseInt(string, 16)

Still gives me the same error sadly

2020-10-17 19:44:00.073 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘Battery Level’: For input string: “0x61”

Put this at the top of rule file
import java.lang.*

and change it to
var value = Integer::parseInt(string, 16)

Gives me the following output:

2020-10-17 20:28:25.282 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model ‘battery.rules’, using it anyway:

The use of wildcard imports is deprecated.

2020-10-17 20:28:25.371 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model ‘battery.rules’

2020-10-17 20:28:30.224 [INFO ] [g.eclipse.smarthome.model.script.Hex] - 0x60

2020-10-17 20:28:30.230 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘Battery Level’: For input string: “0x60”

Why is it not working I going bald, I have tried also

  //convert hex_code to Number type
   var MyNumber = Integer.parseInt(hex_code, 16) as Number
  //use the following for large_hex_code
  var MyNumber1 = Long.parseLong(hex_code, 16) as Number
   // coverting hex_code into DecimalType
  var DecimalType parsedResult = DecimalType.valueOf(Long.parseLong(hex_code, 16).toString);
   val itemvalue = new java.math.BigDecimal(Integer::parseInt(hex_code, 16))
 

I guess I don’t know or its not working or is broken.

Oh well try another approach
Install JS Transformation Pattern

Create File in transformation folder
HEXtoDEC.JS

(function(i) {
  if (i == 'NULL') { return i; }
  if ((i == '-') || (i == 'UNDEF')) { return 'Undefined'; }
  return (parseInt(i,16));
})(input)

Then use a transform in your rule

rule "Battery Level"
when Time cron "0/15 * * * * ?" //run every 15 seconds
then
   var hex_code = executeCommandLine("i2cget -y 1 0x62 0x4 b",10000) 
   var value = transform("JS", "HEXtoDEC.js", hex_code)
   logInfo("Hex",hex_code)
   logInfo("Test",value)
end

It’s working, cheers!!

The only little niggly issue remaining is that the value variable still appears to be a string so I’m unable to perform any mathematical operations on it.

I’ve tried using var int value and var Number value but no luck.

Transformations always return strings - but now it’s a “decimal” string and you can parse that.

Ok so I think it was my item definition causing issues, the rule now returns a NumberItem.

Here’s the rule:

rule "Battery Level"
when Time cron "0/30 * * * * ?" //run every 30 seconds
then
    var hex_code = executeCommandLine("i2cget -y 1 0x62 0x4 b",10000) 
    var value = transform("JS", "HEXtoDEC.JS", hex_code)

    logInfo("Hex",hex_code)
    logInfo("Test",value)

    Battery.postUpdate(value)
    logInfo("Battery", Battery.toString)
end

Item:

Number Battery "Battery Percentage [%s %%]" <batterylevel>

Thanks for the help i’ll mark this as solved :upside_down_face:

1 Like