[SOLVED] How to use number item in createTimer(now.plusSeconds(number item))

I browsed around in a lot of posting, java doc, eclipse doc but did not found a solution for my problem

I have an item:
Number randmetimesettings

I want to use this item in the function

But whatever I use there is an error in the log that type convertion is wrong

Not working:
createTimer(now.plusSeconds(randmetimesettings.state as Number))
createTimer(now.plusSeconds(randmetimesettings.state as Integer))
createTimer(now.plusSeconds(randmetimesettings as Integer))

Any idea?

Any luck with:

createTimer(now.plusSeconds(randmetimesettings.state as int))

or, for completeness:

var Timer doorTimer = null

// [...]

val timing = randmetimesettings.state as int
doorTimer = createTimer(now.plusSeconds(timing)) [|
  // do something

It is the same error message

"Cannot cast from State to int"
Could not invoke method: org.joda.time.DateTime.plusSeconds(int)

I found it after searching for some hours. Maybe someone needs it

Number timesettings

doorTimer = createTimer(now.plusSeconds((timesettings.state as DecimalType).intValue))


I’m not sure where you found it but there is now a section in the docs that attempts to address this to some extent:


In this case, the problem was you needed to convert from a Number to a primitive int.

1 Like

Hi Rich,

yes I read this doc before. But there was nothing about converting Number item to integer. I found it in an old google group about openhab from 2014.

I think it makes sence to add this to the docs

Who can edit this doc?

Anyone. Go to https://github.com/openhab/openhab-docs, submit an issue, and you can edit the document and submit a PR right in your browser.

I waned to parametrize timer length by a dummy (persistent) Number value, which can be set using the app. I spent several hours, incl. reading Rules - openHAB 2 - Empowering the Smart Home, however all the time same error messages. This rules language type casting is really strange and I have not found any documentation, than just mentined examples. Although I just needed to convert Item type Number to rules argument type Long.
This casting example solved the problem, thank you.