[SOLVED] Numbers, ints and Joda Time

Hi, I’m struggeling with a Number<->int<->joda conversion for days and can’t figure out why.

I have a Number type set up
Number PresenceSimulation_DaysAway “Dauer (Tage) der Abwesenheit” (Home)

This Number type is having a value, according to the logs
2019-07-05 14:03:10.002 [INFO ] [del.script.presence_simulation.rules] - object supplied PresenceSimulation_DaysAway (Type=NumberItem, State=2, Label=Dauer (Tage) der Abwesenheit, Category=null, Groups=[Home])

I can convert this Number to an int
var int days = PresenceSimulation_DaysAway.state

As expected, this works according to the log
2019-07-05 14:04:00.116 [INFO ] [del.script.presence_simulation.rules] - days supplied 2

However, when running
var date = now.minusDays(days)

(or any permutation of this) I receive an error in this rule
2019-07-05 14:13:10.111 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘PresenceSimulation’: An error occurred during the script execution: Could not invoke method: org.joda.time.DateTime.minusDays(int) on instance: 2019-07-05T14:13:10.110+02:00

Why is that? When calling
var date = now.minusDays(2)
this works without any issue.

I have a programming background and I favor strongly typed languages, hence I expected
to be the same as
var int days = PresenceSimulation_DaysAway.state
What’s the difference between 2, my var int days and Number.state?

myItem.state never returns an int
It returns a Number, which can indeed look like an int in a string. But it isn’t an int.

now.plusXxxx(n) expects an int, as you know. So we must make that number into an int

(myItem.state as Number).intValue

Many examples of this in this forum.

Yep, that did the trick. Thank you very much!

Many examples of this in this forum.

If only the programming documentation would be more concise. The bits and pieces are scattered across the forums and partially outdated as well. That doesn’t make it easier to find the relevant bit of information.

Hi Marc, welcome to the OpenHAB community!!!
this is true but we have a vibrant community and new questions are answered very quickly, just ask and we will get you going!