[SOLVED] Variable Timer with “createTimer”

Hi all,

I followed the thread

to create a variable timer

My item and Setpoint are

Number TestTimer

Setpoint item=TestTimer label="TestTimer [%.0f sec]" icon="time" minValue=10 maxValue=30 step=10

My rule is

var variable_timer

val Integer TIMER_TIME = TestTimer.state as Number

rule "Alexa Test"
when
        Item MySmartphone changed from OFF to ON
then
                Echo_Livingroom_Speak.sendCommand("Hello")

                variable_timer = createTimer(now.plusMinutes(TIMER_TIME), [|
                Echo_Livingroom_Speak.sendCommand("Again Hello")
                ])
end

But when the rule starts I get the error message:

Rule ‘Alexa Test’: An error occurred during the script execution: Could not invoke method: org.joda.time.DateTime.plusMinutes(int)

What’s wrong? Do I have to load a library?

1 Like

Move the val to be inside the rule, opposed to being set as a global val.
Example:

var variable_timer

rule "Alexa Test"
when
        Item MySmartphone changed from OFF to ON
then
                Echo_Livingroom_Speak.sendCommand("Hello")
                val Integer TIMER_TIME = TestTimer.state as Number
                variable_timer = createTimer(now.plusMinutes(TIMER_TIME), [|
                Echo_Livingroom_Speak.sendCommand("Again Hello")
                ])
end

Since your not doing anything in the rule with the var you can omit that as well.

rule "Alexa Test"
when
        Item MySmartphone changed from OFF to ON
then
                Echo_Livingroom_Speak.sendCommand("Hello")
                val Integer TIMER_TIME = TestTimer.state as Number
                createTimer(now.plusMinutes(TIMER_TIME), [|
                Echo_Livingroom_Speak.sendCommand("Again Hello")
                ])
end

Neither of these variations is working.
The error message remains the same.

should this be var instead of val?
I’m guessing (new to rules writing)

var = variable (new values can be assigned)
val = value (can only be set once)

ok well your code matches Vincent and Udo’s versions of the code

maybe log value of TIMER_TIME before attempting to create the timer?
edit: maybe restart OH help clear old rule out?
edit2: value set in sitemap? make sure expected input sorry if did already

Or add persistence to the TestTimer item?

Ok I finnaly got it working with

val Integer TIMER_TIME = (TestTimer.state as DecimalType).intValue

in the body of the rule.

However

val Integer TIMER_TIME = TestTimer.state as Number

showed exactly the same log, but threw an error.