rkrisi
(Kristof Rado)
September 8, 2018, 5:25pm
1
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!
hr3
(Harry)
September 8, 2018, 5:28pm
2
try
var tmp = (miNetLife.state as Number)
or
var Number tmp = miNetLife.state
if miNettLife is a number-item
vzorglub
(Vincent Regaud)
September 8, 2018, 5:29pm
3
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)
rkrisi
(Kristof Rado)
January 23, 2019, 3:13pm
5
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.
vzorglub
(Vincent Regaud)
January 23, 2019, 3:38pm
6
Have you tried with no spaces?
val sec = totalSecs%60
rkrisi
(Kristof Rado)
January 23, 2019, 3:41pm
7
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…
vzorglub
(Vincent Regaud)
January 23, 2019, 3:46pm
8
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
rkrisi
(Kristof Rado)
January 23, 2019, 3:49pm
9
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
vzorglub
(Vincent Regaud)
January 23, 2019, 3:56pm
10
var int totalMillis = (triggeringItem.state as Number).intValue
val int totalSecs = totalMillis / 1000
val int sec = totalSecs % 60
4 Likes
rkrisi
(Kristof Rado)
January 23, 2019, 4:08pm
11
Thanks, this did the trick!