[SOLVED] Convert number to string from Exec Binding and comare it in rules

I have a termo-sensor LM75, and I get temperature by python script (using special addon Exec Binding). It works fine, BUT type of result of script is a STRING. And i can’t use it in my rules.

    String Temp_1f  "real temp 1 floor [%s]" {channel="exec:command:c5d60dc7:output" }

and I try this ways:

  1. in rules I write
var teest = Temp_1f as Number

, but in log appears error: Error during the execution of rule ‘test’: Could not cast Temp_1f (Type=StringItem, State=25.12, Label=real temp on 1 floor, Category=null) to java.lang.Number

  1. After it i found a post with a same problem. I create special item
Number Temp_1f_numb "Temp is [%.1f °C]"

and make rule for converting

Temp_1f_numb.postUpdate(Float::parseFloat(String::format("%s",Temp_1f.state).replace(' ','')))

And it works, BUT a can’t compare getted value in my rule:

 var set1temp = Reg_1f.state as Number  
 Temp_1f_numb.postUpdate(Float::parseFloat(String::format("%s",Temp_1f.state).replace(' ','')))
 var cur1temp = Temp_1f_numb

    if (cur1temp < set1temp)  logInfo("info", "O, Yeees!)
    else  logInfo("info", "Omg, NO")

In log instead my text appeared Error during the execution of rule ‘test’: Unknown variable or command '<'

My questions are: What wrong in my second way? And, maybe, there is more easier, right solution for compare results?

1 Like

Temp_1f_numb is an Item.
So now cur1temp is a reference to an Item.

You can’t compare an Item with a number variable. Items are objects with name, label, etc.

Usually you would be interested in the item.state for this kind of comparison.

But be careful, when you postUpdate something to an Item it gets done asynchronously. the rule doesn’t stop and wait for that to happen. If you immediately read the Item state it will probably still have the old value.
You don’t need to do that anyway - you already know what value you just sent to postUpdate.

Thank you, it works!
And what about more easier way to work with Exec Binding results? Do you know?

Not sure what you mean.
It is a limitation of exec binding that results are always strings.

There is an open issue requesting enhancement to handle Switch type output (not Number)

but personally I think it’d be more useful to have a more general type-parsing profile that we could add to channels.