Turn light off based on timer

Tags: #<Tag:0x00007f617ec0cd08> #<Tag:0x00007f617ec0cad8>

Ok, Rule fired but know I get this error:

An error occured during the script execution: Could not invoke method: org.joda.time.DateTime.plusMinutes(int) on instance: 2017-02-28T19:27:55.957+01:00

My import at the beginning of the rule file:

import org.joda.time.DateTime

What is missing.

Thank you for your help!

What does the loginfo say, that you added to see what was passed to the timer?

I tested the logInfo output of the timer only with a fixed value (e.g. 2) inside the now.plusMinutes expression. I think the problem is the format of the number I will give into this expression. I used the expression from Udo:

Number myHours "my hours [%d]"

but changed to minutes. Could this be this expression for the formating

[%d]

problem?

I have checked: The timer don’t start, so I can not check the logInfo output.

Your plusMinutes error message is hinting that it wants to be supplied with an integer value.
Try (myHours.state as Number).intValue

The [%d] in the Item label is only about how you choose to display the number, in this case as a decimal.

Thank you, tomorrow I will give it a try.

Good night

Still no luck, this is the output:

Rule 'Kaffeemaschine auto off': An error occured during the script execution: The name '<XFeatureCallImplCustom>.state' cannot be resolved to an item or type.

and the smart home Designer says:

Type mismatch: cannot convert from Number to int

When I turn the coffee maker off the following log appears in karaf:

Rule 'Kaffeemaschine auto off': cannot invoke method public abstract boolean org.eclipse.smarthome.model.script.actions.Timer.cancel() on null

Thank you for any hint!

Maybe you could cast like

 (myHours.state as DecimalType).intValue

Same problem:

[ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Kaffeemaschine auto off': An error occured during the script execution: The name '<XFeatureCallImplCustom>.state' cannot be resolved to an item or type.

Is it possible that I need more imports for that. I use only:

import org.joda.time.DateTime

Thanks for your support guys!

Do you use OH or OH2?
OH2 doesnt need any imports AFAIK

OH2. I removed the import but now the rules abort with this error:

Could not invoke method: org.joda.time.DateTime.plusMinutes(int) on instance:

I found the failure. I’m an idiot:-) I forgot one parenthesis after .intValue.

createTimer(now.plusMinutes((minutes_auto_off_kaffee.state as DecimalType).intValue))

Thank you for your time and help.

1 Like

A version with a fixed auto-off time using the Expire binding, but no timers or rules needed:

Switch Wallplug1_Switch "Coffee Machine" 
  { expire="30m,command=OFF", otherbinding="..." or channel="..." }

It’s me again:-). Sometimes I get the following error after switching the wall plug off, but it seems to work:

Rule 'Kaffeemaschine auto off': cannot invoke method public abstract boolean org.eclipse.smarthome.model.script.actions.Timer.cancel() on null

Can somebody explain to me whats the problem. I cannot find information about it.

You’re trying to .cancel a timer that has already expired.
Add another
myTimer_Kaffeemaschine = null
inside the body of the timer so that it nulls itself when finished.

You are talking about to add the expression after the off command for the item?

I will give up. Actually I have the problem that the timer switches the coffee maker after a random time off, but inside the time range of the set timer value. To reduce the failure possibilities I simplify the rule to:

rule "Kaffeemaschine auto off"
when
    Item Wallplug1_Switch changed
then
    if (Wallplug1_Switch.state==OFF) {
        myTimer_Kaffeemaschine.cancel
        myTimer_Kaffeemaschine = null
        logInfo("rules","Timer Kaffeemaschine cancelled")
    }
    else if (Wallplug1_Switch.state==ON) {
        	logInfo("rules","Timer Kaffeemaschine start and set to: "+minutes_auto_off_kaffee.state+" minutes")}
       		myTimer_Kaffeemaschine = createTimer(now.plusMinutes((minutes_auto_off_kaffee.state as DecimalType).intValue)) [|
            sendCommand(Wallplug1_Switch, OFF)
        ]
end

But the problem still exists. Sometimes it happens, that the log shows a state change, but the reale plug doesn’t change the state, but I don’ t have any connection issues with the z-wave plug. Every second or third try the timer operates correct.

Actually I have no imports inside my rules file.

Can somebody help me please!

Johannes, in the rule you just posted, you are missing a closing curly brace ‘}’ in the line before end

In the body of the if statement, you aren’t checking myTimer_Kaffeemaschine for the value null before calling its cancel method. You might try the following:

rule "Kaffeemaschine auto off"
when
    Item Wallplug1_Switch changed
then
    if (Wallplug1_Switch.state==OFF) {
        myTimer_Kaffeemaschine?.cancel
        myTimer_Kaffeemaschine = null
        logInfo("rules","Timer Kaffeemaschine cancelled")
    }
    else if (Wallplug1_Switch.state==ON) {
        	logInfo("rules","Timer Kaffeemaschine start and set to: "+minutes_auto_off_kaffee.state+" minutes")
       		myTimer_Kaffeemaschine = createTimer(now.plusMinutes((minutes_auto_off_kaffee.state as DecimalType).intValue)) [|
            sendCommand(Wallplug1_Switch, OFF)
        ]
    }
end

Note the ‘?’ following myTimer_Kaffeemaschine in the first line of the body of the if statement. The invocation of the cancel method is ignored if myTimer_Kaffeemaschine has the value null.

[Edit: I just noted that you did have a closing ‘}’ following the ‘)’ in the logInfo call. I removed it from my proposed version of your rule.]

Thank you for your hint with the “?”. The rule is running, but when I use .plusHours instead of .plus Minutes I get the following error after the timer (1 hour) wants to turn off the device:

13:12:49.405 [ERROR] [org.quartz.core.JobRunShell         ] - Job DEFAULT.2017-03-08T13:12:49.402+01:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
  <XFeatureCallImplCustom>.sendCommand(<XFeatureCallImplCustom>)
} ] threw an unhandled Exception: 
java.lang.RuntimeException: The name '<unkown>' cannot be resolved to an item or type.
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:112)[129:org.eclipse.smarthome.model.script:0.9.0.b4]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:901)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:864)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:223)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:759)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:219)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:446)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:227)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:189)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:46)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:29)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at com.sun.proxy.$Proxy142.apply(Unknown Source)[:]
	at org.eclipse.smarthome.model.script.internal.actions.TimerExecutionJob.execute(TimerExecutionJob.java:38)[129:org.eclipse.smarthome.model.script:0.9.0.b4]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202)[104:org.eclipse.smarthome.core.scheduler:0.9.0.b4]
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)[104:org.eclipse.smarthome.core.scheduler:0.9.0.b4]
13:12:49.424 [ERROR] [org.quartz.core.ErrorLogger         ] - Job (DEFAULT.2017-03-08T13:12:49.402+01:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
  <XFeatureCallImplCustom>.sendCommand(<XFeatureCallImplCustom>)
} ] threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.RuntimeException: The name '<unkown>' cannot be resolved to an item or type.]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:213)[104:org.eclipse.smarthome.core.scheduler:0.9.0.b4]
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)[104:org.eclipse.smarthome.core.scheduler:0.9.0.b4]
Caused by: java.lang.RuntimeException: The name '<unkown>' cannot be resolved to an item or type.
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:112)[129:org.eclipse.smarthome.model.script:0.9.0.b4]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:901)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:864)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:223)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:759)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:219)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:446)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:227)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:189)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:46)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:29)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at com.sun.proxy.$Proxy142.apply(Unknown Source)[:]
	at org.eclipse.smarthome.model.script.internal.actions.TimerExecutionJob.execute(TimerExecutionJob.java:38)[129:org.eclipse.smarthome.model.script:0.9.0.b4]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202)[104:org.eclipse.smarthome.core.scheduler:0.9.0.b4]
	... 1 more
13:12:49.427 [ERROR] [org.quartz.core.JobRunShell         ] - Job DEFAULT.2017-03-08T13:12:49.423+01:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
  <XFeatureCallImplCustom>.sendCommand(<XFeatureCallImplCustom>)
} ] threw an unhandled Exception: 
java.lang.RuntimeException: The name '<unkown>' cannot be resolved to an item or type.
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:112)[129:org.eclipse.smarthome.model.script:0.9.0.b4]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:901)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:864)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:223)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:759)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:219)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:446)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:227)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:189)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:46)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:29)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at com.sun.proxy.$Proxy142.apply(Unknown Source)[:]
	at org.eclipse.smarthome.model.script.internal.actions.TimerExecutionJob.execute(TimerExecutionJob.java:38)[129:org.eclipse.smarthome.model.script:0.9.0.b4]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202)[104:org.eclipse.smarthome.core.scheduler:0.9.0.b4]
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)[104:org.eclipse.smarthome.core.scheduler:0.9.0.b4]
13:12:49.448 [ERROR] [org.quartz.core.ErrorLogger         ] - Job (DEFAULT.2017-03-08T13:12:49.423+01:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
  <XFeatureCallImplCustom>.sendCommand(<XFeatureCallImplCustom>)
} ] threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.RuntimeException: The name '<unkown>' cannot be resolved to an item or type.]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:213)[104:org.eclipse.smarthome.core.scheduler:0.9.0.b4]
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)[104:org.eclipse.smarthome.core.scheduler:0.9.0.b4]
Caused by: java.lang.RuntimeException: The name '<unkown>' cannot be resolved to an item or type.
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:112)[129:org.eclipse.smarthome.model.script:0.9.0.b4]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:901)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:864)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:223)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:759)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:219)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:446)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:227)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:189)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:46)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:29)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
	at com.sun.proxy.$Proxy142.apply(Unknown Source)[:]
	at org.eclipse.smarthome.model.script.internal.actions.TimerExecutionJob.execute(TimerExecutionJob.java:38)[129:org.eclipse.smarthome.model.script:0.9.0.b4]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202)[104:org.eclipse.smarthome.core.scheduler:0.9.0.b4]
	... 1 more

The rule:

var Timer myTimer_Plattenspieler = null //Plattenspieler

rule "Plattenspieler auto off"
when
    Item HK_Schallplattenspieler changed
then
    if (HK_Schallplattenspieler.state==OFF) {
        myTimer_Plattenspieler?.cancel				//? bedeutet, dass .cancel ignoriert wird wenn der Timer = null ist
        //Thread::sleep(2000)
        myTimer_Plattenspieler = null
        logInfo("rules","Timer Plattenspieler set to 0")
    }
    else if (HK_Schallplattenspieler.state==ON) {
        if (myTimer_Plattenspieler!=null) {
            myTimer_Plattenspieler.cancel
            //Thread::sleep(2000)
            myTimer_Plattenspieler = null
            //Thread::sleep(2000)
            logInfo("rules","Timer Plattenspieler reset to 0")
            
        }
        logInfo("rules","Timer Plattenspieler wird gesetzt")
        myTimer_Plattenspieler = createTimer(now.plusHours((hours_auto_off_Plattenspieler.state as DecimalType).intValue) [|
            HK_Schallplattenspieler.sendCommand(OFF)
        ]
    }
end

I have no idea what this could be… Any ideas?

Thank you!

This is quite likely to be noticeable when editing a rules file that has already fired off hours-long timers The ‘orphans’ will generate errors later, like the one you see (I think because their environment has been destroyed)

Thank you, your are right! A second test finished with success!