I am trying to set a timer for a PIR sensor.
This has been tried and tested many times; however, I want to catch when the PIR comes on, which causes errors on rule reload.
This rule w/o the catch “ON” works, but errors on reload.
rule "Shed_NLO_PIR debounce"
when
Item Shed_NLO_PIR received update ON
then
if (Shed_NLO_PIR_debounceTimer === null || Shed_NLO_PIR_debounceTimer.hasTerminated())
{
Shed_NLO_PIR_debounceTimer = createTimer(now.plusMinutes(5), [|
logInfo("Sonoff.2.03", "Shed_NLO_PIR.............: OFF")
Shed_NLO_PIR_LUP.postUpdate(new DateTimeType())
Shed_NLO_PIR.sendCommand(OFF)
Shed_NLO_PIR_debounceTimer = null
])
}
else
{
Shed_NLO_PIR_debounceTimer.reschedule(now.plusMinutes(5))
}
end
The error on rule reload when the timer is active is:
2020-11-07 11:41:45.596 [INFO ] [e.smarthome.model.script.Sonoff.2.03] - Shed_NLO_PIR.............: OFF
2020-11-07 11:41:45.599 [ERROR] [org.quartz.core.JobRunShell ] - Job DEFAULT.Timer 82 2020-11-07T11:39:38.209+10:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
logInfo(<XStringLiteralImpl>,<XStringLiteralImpl>)
<XFeatureCallImplCustom>.postUpdate(<XConstructorCallImplCustom>)
<XFeatureCallImplCustom>.sendCommand(<XFeatureCallImplCustom>)
<null>.Shed_NLO_PIR_debounceTimer = <XNullLiteralImplCustom>
} ] threw an unhandled Exception:
java.lang.NullPointerException: null
Adding any one of these three statements to catch the PIR ON state…
logInfo("Sonoff.2.01", "Shed_NLO_PIR historic....: {}", Shed_NLO_PIR.historicState(now.minusSeconds(61),"rrd4j").state)
logInfo("Sonoff.2.01", "Shed_NLO_PIR.prev state..: {}", Shed_NLO_PIR.previousState.state)
if (Shed_NLO_PIR.previousState.state === null || Shed_NLO_PIR.previousState.state == OFF)
{
logInfo("Sonoff.2.02", "Shed_NLO_PIR.............: ON")
}
creates this error:
2020-11-07 10:55:43.103 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Shed_NLO_PIR debounce': cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.persistence.HistoricItem.getState() on null
To complete the config:
var Timer Shed_NLO_PIR_debounceTimer = null
rule "Sonoff rules started"
when
System started
then
logInfo("sonoff.0.0", "System start or rule file reload for Sonoff rules")
if (Shed_NLO_PIR.state == NULL || Shed_NLO_PIR.state == ON)
{
Shed_NLO_PIR.postUpdate(OFF)
}
end
(another rule identifying the PIR postUpdates the item:)
switch PIR_id
{
case "54978":
{
Shed_NLO_PIR.postUpdate(ON)
}
}
Persistence is on as question in this post and here.
Is there a way to:
a) deal cleanly with timers?
b) test for the previousState.state on reload?
[edit 1] to my query a) I read Rossko’s post, regarding running timers on reload. Sorted! by design.