[SOLVED] Error creating timer in rule

I just started playing with rules, and my first one is intended to turn the lights out if no motion is detected for five minutes. I have an error in the logs I can make no sense of :slight_smile:

Running openHAB 2.2.0 Build #1133; this is the error:

2017-12-13 01:58:02.248 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Office Motion Detected': An error occurred during the script execution: Couldn't invoke 'assignValueTo' for feature JvmVoid: (eProxyURI: office.rules#|::0.2.0.2.0.4::0::/1)

It’s being triggered by this block in the rule:

   timer = createTimer(now.plusMinutes(5)) [|
     logInfo("Office motion not detected; lights out")
     //sendCommand(Office_Lights, OFF)
   ]

Here’s the complete rule file:

var occupiedTimer = null

rule "Office Motion Detected"
when
  Channel "dlinksmarthome:DCH-S150:6c7220c50bc1:motion" triggered
then
  logInfo("OFFICE_MOTION", "Office motion detected")
  //postUpdate(OfficeOccupancyLastUpdated, new DateTime())
  logInfo("OFFICE_MOTION", "Office motion detected -- occupancy updated")

  if (occupiedTimer !== null) {
    logDebug("OFFICE_MOTION", "Office motion detected -- timer cleared")
    occupiedTimer.cancel
  }

  logInfo("OFFICE_MOTION", "Office motion detected -- reset timer")
  timer = createTimer(now.plusMinutes(5)) [|
    logInfo("Office motion not detected; lights out")
    //sendCommand(Office_Lights, OFF)
  ]
end

Any help appreciated…

Change to var Timer occupiedTimer = null

Probably not 100% necessary, but it’s a good practice to be explicit about the type.

More importantly, change timer to occupiedTimer

1 Like

Oh wow… Thanks for pointing out the mistake – that error message could use some refinement! Also, thanks for the tip on explicitly typing variables.

Laurie

In this case it is necessary. The way the Rules Engine is able to infer the Type for a variable is by the type of the object on the right side of the =. Since null is not of type Timer it can’t set the type for occupiedTimer correctly. In all other cases except when you initialize a variable to null the type can be inferred.

1 Like

Makes total sense. Thanks for the info!