Harmony rule with timer

Hello!
I have a problem with a harmony rule. within the rule a task (the first if clause) is started with a timer which should be executed after 64 seconds. unfortunately this task is not always started reliably, i.e. in this case the harmony activity is not started. i must then send the harmony command again. the harmony is online after max. 25 seconds, after the socket is on. so the 64 seconds should be enough… i also see the logs, so the if loop is also executed…

does anyone know where the problem is?

var Timer timer = null
var tmp = ""

rule "Harmony activity"
when
    Item Harmony_activity received command
then
    var harmony_stat = getThingStatusInfo("harmonyhub:hub:HarmonyHub").getStatus().toString()
    // wenn steckdose aus ist und Aktivität erfolgt und kein timer aktiv:
    if( receivedCommand != "PowerOff" && Sonoff_S20_1.state == OFF && timer === null ) {
        logInfo("harmony.rules", "Starte Harmony (harmony offline),Command:" + receivedCommand)
        Sonoff_S20_1.sendCommand(ON)
        tmp = receivedCommand
        timer = createTimer(now.plusSeconds(64), [|
            if( receivedCommand.toString.toLowerCase.contains("beamer") ) {
                beamer_lift.sendCommand(OFF)
                Thread::sleep(5000)
            }
            logInfo("harmony.rules", "Harmony activity:" + tmp)
            harmonyhub_hub_HarmonyHub_currentActivity.sendCommand(tmp)
            timer = null
        ])
        //logInfo("harmony.rules", "DEBUG: cmd an harmony")
    // wenn steckdose an ist und Aktivität erfolgt und kein timer aktiv:
    } else if( receivedCommand != "PowerOff" && Sonoff_S20_1.state == ON && timer === null ) {
        logInfo("harmony.rules", "Starte Harmony (harmony online),Command:" + receivedCommand)
        if(harmony_stat == "ONLINE"){
            harmonyhub_hub_HarmonyHub_currentActivity.sendCommand(receivedCommand)
        }
    // wenn alles ausgeschaltet werden soll:
    } else if ( receivedCommand == "PowerOff" ){ //wenn harmony vorher an gewesen ist (also einen Status hatte), dann erst ausschalten! Sonst Schleife!
        logInfo("harmony.rules", "Schalte Harmony aus ...")
        tmp = harmonyhub_hub_HarmonyHub_currentActivity.state.toString.toLowerCase
        harmonyhub_hub_HarmonyHub_currentActivity.sendCommand(receivedCommand)
        timer = createTimer(now.plusSeconds(25), [|
            if( tmp.contains("beamer") ) {
                beamer_lift.sendCommand(ON)
                Thread::sleep(5000)
            }
            Sonoff_S20_1.sendCommand(OFF)
            timer = null
        ])
    }
    
end


I don’t think I even understand what the problem is.

What exactly is not working with this rule? The rule isn’t running? It’s running but not working the way you want? Timer isn’t running? Something else? 64 seconds is enough for what?

i would like to achieve the following with the rule: first my socket is switched on, which switches on the tv and the harmony hub. then the rule waits 64 seconds until all devices are ready. after the 64 seconds, a certain harmony activity is started. however, this activity is not always started; sometimes only the socket is switched on and nothing else.
and i do not know why

The first thing to determine is whether or not your Timer executes after the 64 seconds. Add some logging and/or watch events.log to see if the Timer executes.

If it runs make sure the right command is sent (log out receivedCommand too).

If it runs and the right command is sent, the problem is in the binding and not your Rule.

If it doesn’t run than we should have enough information at that point to start debugging the rule. I’m not convinced there is anything wrong with this rule.

ok, here is my log. there is an error on sending the command to the harmony hub: “cannot onvoke method on instance null”. i dont know whats the error is…

2020-12-27 13:08:45.204 [INFO ] [smarthome.model.script.harmony.rules] - Starte Harmony (harmony offline),Command:Beamer
2020-12-27 13:09:22.215 [INFO ] [er.impl.jmdns.JmdnsHomekitAdvertiser] - Registering _hap._tcp.local. on port 9123
2020-12-27 13:09:24.199 [INFO ] [org.quartz.core.QuartzScheduler     ] - Scheduler openHAB-job-scheduler_$_NON_CLUSTERED started.
2020-12-27 13:09:26.465 [INFO ] [org.quartz.core.QuartzScheduler     ] - Scheduler openHAB-job-scheduler_$_NON_CLUSTERED started.
2020-12-27 13:09:54.595 [ERROR] [org.quartz.core.JobRunShell         ] - Job DEFAULT.Timer 99 2020-12-27T13:09:49.205+01:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
  org.eclipse.xtext.xbase.impl.XIfExpressionImpl@6e36bca8 (conditionalExpression: false)
  logInfo(<XStringLiteralImpl>,<XBinaryOperationImplCustom>)
  <XFeatureCallImplCustom>.sendCommand(<XFeatureCallImplCustom>)
  <null>.timer = <XNullLiteralImplCustom>
} ] threw an unhandled Exception:
java.lang.IllegalStateException: Could not invoke method: org.eclipse.xtext.xbase.lib.StringExtensions.operator_plus(java.lang.String,java.lang.String) on instance: null
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1192) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1150) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1136) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:1081) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:151) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:991) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:237) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:215) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluateArgumentExpressions(XbaseInterpreter.java:1205) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1135) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:1081) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:151) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:991) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:954) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:235) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:215) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:458) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:239) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:215) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:201) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:46) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:29) ~[?:?]
        at com.sun.proxy.$Proxy159.apply(Unknown Source) ~[?:?]
        at org.eclipse.smarthome.model.script.internal.actions.TimerExecutionJob.execute(TimerExecutionJob.java:48) ~[?:?]
        at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [bundleFile:?]
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [bundleFile:?]
Caused by: java.lang.IllegalArgumentException: java.lang.ClassCastException@7caa44f
        at sun.reflect.GeneratedMethodAccessor675.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_152]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_152]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1175) ~[?:?]
        ... 28 more
2020-12-27 13:09:54.610 [ERROR] [org.quartz.core.ErrorLogger         ] - Job (DEFAULT.Timer 99 2020-12-27T13:09:49.205+01:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
  org.eclipse.xtext.xbase.impl.XIfExpressionImpl@6e36bca8 (conditionalExpression: false)
  logInfo(<XStringLiteralImpl>,<XBinaryOperationImplCustom>)
  <XFeatureCallImplCustom>.sendCommand(<XFeatureCallImplCustom>)
  <null>.timer = <XNullLiteralImplCustom>
} ] threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception.
        at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [bundleFile:?]
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [bundleFile:?]
Caused by: java.lang.IllegalStateException: Could not invoke method: org.eclipse.xtext.xbase.lib.StringExtensions.operator_plus(java.lang.String,java.lang.String) on instance: null
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1192) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1150) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1136) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:1081) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:151) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:991) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:237) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:215) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluateArgumentExpressions(XbaseInterpreter.java:1205) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1135) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:1081) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:151) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:991) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:954) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:235) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:215) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:458) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:239) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:215) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:201) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:46) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:29) ~[?:?]
        at com.sun.proxy.$Proxy159.apply(Unknown Source) ~[?:?]
        at org.eclipse.smarthome.model.script.internal.actions.TimerExecutionJob.execute(TimerExecutionJob.java:48) ~[?:?]
        at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[?:?]
        ... 1 more
Caused by: java.lang.IllegalArgumentException: java.lang.ClassCastException@7caa44f
        at sun.reflect.GeneratedMethodAccessor675.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_152]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_152]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1175) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1150) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1136) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:1081) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:151) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:991) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:237) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:215) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluateArgumentExpressions(XbaseInterpreter.java:1205) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1135) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:1081) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:151) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:991) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:954) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:235) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:215) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:458) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:239) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:215) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:201) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:46) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:29) ~[?:?]
        at com.sun.proxy.$Proxy159.apply(Unknown Source) ~[?:?]
        at org.eclipse.smarthome.model.script.internal.actions.TimerExecutionJob.execute(TimerExecutionJob.java:48) ~[?:?]
        at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[?:?]
        ... 1 more

there is an error i cannot explain…

Caused by: java.lang.IllegalStateException: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.String) on instance: null

ok, here is my final working rule. the problem was, that i have to write “receivedCommand.toString()” and the harmony needs a little bit more time to receive a comman after its turned on (therefore the while in the rule).

var Timer timer = null
var tmp = ""

rule "Harmony activity"
when
    Item Harmony_activity received command
then
    var harmony_stat = getThingStatusInfo("harmonyhub:hub:HarmonyHub").getStatus().toString()
    // wenn steckdose aus ist und Aktivität erfolgt und kein timer aktiv:
    if( receivedCommand != "PowerOff" && Sonoff_S20_1.state == OFF && timer === null ) {
        logInfo("harmony.rules", "Starte Harmony (harmony offline),Command:" + receivedCommand.toString())
        Sonoff_S20_1.sendCommand(ON)
        timer = createTimer(now.plusSeconds(64), [|
            if( receivedCommand.toString().toLowerCase.contains("beamer") ) {
                beamer_lift.sendCommand(OFF)
                Thread::sleep(7000)
            }
            // in case of missing of switching activity from harmony binding:
            while (harmonyhub_hub_HarmonyHub_currentActivity.state.toString() != receivedCommand.toString()) {
                logInfo("harmony.rules", "Starte Harmony activity:" + receivedCommand.toString())
                harmonyhub_hub_HarmonyHub_currentActivity.sendCommand(receivedCommand.toString())
                Thread::sleep(4000)
            }
            timer = null
        ])
        //logInfo("harmony.rules", "DEBUG: cmd an harmony")
    // wenn steckdose an ist und Aktivität erfolgt und kein timer aktiv:
    } else if( receivedCommand != "PowerOff" && Sonoff_S20_1.state == ON && timer === null ) {
        logInfo("harmony.rules", "Starte Harmony (harmony online),Command:" + receivedCommand.toString())
        if(harmony_stat == "ONLINE"){
            harmonyhub_hub_HarmonyHub_currentActivity.sendCommand(receivedCommand.toString())
        }
    // wenn alles ausgeschaltet werden soll:
    } else if ( receivedCommand == "PowerOff" ){ //wenn harmony vorher an gewesen ist (also einen Status hatte), dann erst ausschalten! Sonst Schleife!
        logInfo("harmony.rules", "Schalte Harmony aus ...")
        tmp = harmonyhub_hub_HarmonyHub_currentActivity.state.toString.toLowerCase
        harmonyhub_hub_HarmonyHub_currentActivity.sendCommand(receivedCommand.toString())
        timer = createTimer(now.plusSeconds(25), [|
            if( tmp.contains("beamer") ) {
                beamer_lift.sendCommand(ON)
                Thread::sleep(5000)
            }
            Sonoff_S20_1.sendCommand(OFF)
            timer = null
        ])
    }
    
end

That’s got the potential to turn nasty. Should your device never get to the desired state, your rule is cycling forever.

correct, i changed the while to a for loop, thanks!

can you please tell / show what firmware your hub is are running ( latest ) ?
and maybe show the rest of your config regarding your hub from openhab i cant get the binding to stay online.

i have the latest firmware on my hub and the harmony binding v3.0.1. I have configured it via OH3 UI.

pls post your config and openhab log