[SOLVED] Parse Number to Integer

Hi!

I want to parse my Number to Int to do some calculations with it.

I have tried:

	var tmp = (miNetLife.state as DecimalType)
	tmp = tmp / 1800
	sendCommand(miNetLife_Out, tmp)

But I get an error that it can’t convert BigDecimal to DecimalType.

What is the problem?

Thanks!

try

var tmp = (miNetLife.state as Number)

or

var Number tmp = miNetLife.state

if miNettLife is a number-item

As @hr3 said, use the Number
Also use the sendCommand method instead of the action

	var tmp = (miNetLife.state as Number)
	tmp = tmp / 1800
	miNetLife_Out.sendCommand(tmp)

Thanks for both of you!

I’m back with a new problem related to this.

I have an Number:Time item:

var totalMillis = (triggeringItem.state as Number)
val totalSecs = totalMillis / 1000
val sec = totalSecs % 60

There during execution I got an error that ‘%’ is an unknown command. Before I was able to use this to calculate the residue.

Have you tried with no spaces?

val sec = totalSecs%60

Yes, same error:

Rule 'Calculate current Track Position': Unknown variable or command '%'; line 15, column 15, length 12

Funny thing is that this rule worked before great (it calculated milliseconds to hour:minute:seconds format), but the binding changed and now the input is not provided as String but as a Number (which is better), but the rule won’t want to work like this…

Remove the brackets, I thing it may create a boolean
Force the type to Number

var Number totalMillis = triggeringItem.state as Number
val Number totalSecs = totalMillis / 1000
val Number sec = totalSecs % 60

Unfortunately the same error.

Are these commands supported on Number type (considering that this is an Object)?

Maybe it can help you, I don’t know anything else what can be wrong. This was the parsing from String to Integer:

val totalMillis = Integer::parseInt(String::format("%s", triggeringItem.state))
1 Like
var int totalMillis = (triggeringItem.state as Number).intValue
val int totalSecs = totalMillis / 1000
val int sec = totalSecs % 60
2 Likes

Thanks, this did the trick!