Hi Rich,
I’ve followed your advice and adapted my rule like so:
rule "Switch off bureau light common after 10 seconds"
when
Item Sw_Bureau_B changed (or 'received update', behaviour is the same...)
then
if (Sw_Bureau_B.state==ON) {
if (timer==null) {
timer = createTimer(now.plusSeconds(10)) [|
sendCommand(Sw_Bureau_B, OFF)
timer.cancel
timer = null
logInfo("Bureau light", "Time elapsed, switched off light")
]
}
logInfo("Bureau light", "Will switch off bureau light common within 2 minutes")
} else {
if (timer!=null) {
timer.cancel
timer = null
logInfo("Bureau light", "Prematurely switched off bureau light timer")
}
}
end
When the rule is ran, I have the following output:
2016-10-15 14:50:45.193 [INFO ] [runtime.busevents ] - Sw_Bureau_B received command ON
2016-10-15 14:50:45.198 [DEBUG] [.mqtt.internal.MqttItemBinding] - Publishing command ON to home/verdiep/Switch/Sw_Bureau_B
2016-10-15 14:50:45.213 [INFO ] [runtime.busevents ] - Sw_Bureau_B state updated to ON
2016-10-15 14:50:45.219 [DEBUG] [o.i.t.m.i.MqttBrokerConnection] - Publishing message 8 to topic 'home/verdiep/Switch/Sw_Bureau_B'
2016-10-15 14:50:45.243 [INFO ] [nhab.model.script.Bureau light] - Will switch off bureau light common within 10 seconds
2016-10-15 14:50:55.251 [INFO ] [runtime.busevents ] - Sw_Bureau_B received command OFF
2016-10-15 14:50:55.253 [DEBUG] [.mqtt.internal.MqttItemBinding] - Publishing command OFF to home/verdiep/Switch/Sw_Bureau_B
2016-10-15 14:50:55.260 [DEBUG] [o.i.t.m.i.MqttBrokerConnection] - Publishing message 9 to topic 'home/verdiep/Switch/Sw_Bureau_B'
2016-10-15 14:50:55.267 [INFO ] [runtime.busevents ] - Sw_Bureau_B state updated to OFF
2016-10-15 14:50:55.276 [INFO ] [nhab.model.script.Bureau light] - Time elapsed, switched off light
2016-10-15 14:50:55.345 [INFO ] [nhab.model.script.Bureau light] - Prematurely switched off bureau light timer
However, I don’t understand why I get the last line (Prematurely switched off bureau light timer
). I shouldn’t get this.
When looking to the rule, the code should go into the else
block of the main if
construction when the state of Sw_Bureau_B
is OFF
(that is, after the 10 seconds).
But I have a “timer guard” to check if the timer is null
or not into that else
block.
Since just after the sendCommand(Sw_Bureau_B, OFF)
I’ve put the timer to null
when my 10 seconds timer elapsed (and I also canceled the timer first in that block of code but that might not be needed in this case…), I’m expecting that the if
condition in the else
block is failing.
So, I don’t understand why the logInfo("Bureau light", "Prematurely switched...")
is still executed in that case… That should only happen if the switch is put to OFF
within 10 seconds after it’s put to ON
, I assume…
You have any clue?
I also sometimes have this output:
2016-10-15 14:53:40.985 [INFO ] [runtime.busevents ] - Sw_Bureau_B received command ON
2016-10-15 14:53:40.990 [DEBUG] [.mqtt.internal.MqttItemBinding] - Publishing command ON to home/verdiep/Switch/Sw_Bureau_B
2016-10-15 14:53:41.000 [DEBUG] [o.i.t.m.i.MqttBrokerConnection] - Publishing message 12 to topic 'home/verdiep/Switch/Sw_Bureau_B'
2016-10-15 14:53:41.019 [INFO ] [runtime.busevents ] - Sw_Bureau_B state updated to ON
2016-10-15 14:53:41.290 [INFO ] [nhab.model.script.Bureau light] - Will switch off bureau light common within 10 seconds
2016-10-15 14:53:41.311 [INFO ] [nhab.model.script.Bureau light] - Will switch off bureau light common within 10 seconds
2016-10-15 14:53:51.322 [INFO ] [runtime.busevents ] - Sw_Bureau_B received command OFF
2016-10-15 14:53:51.325 [DEBUG] [.mqtt.internal.MqttItemBinding] - Publishing command OFF to home/verdiep/Switch/Sw_Bureau_B
2016-10-15 14:53:51.328 [DEBUG] [o.i.t.m.i.MqttBrokerConnection] - Publishing message 13 to topic 'home/verdiep/Switch/Sw_Bureau_B'
2016-10-15 14:53:51.342 [INFO ] [runtime.busevents ] - Sw_Bureau_B state updated to OFF
2016-10-15 14:53:51.365 [INFO ] [runtime.busevents ] - Sw_Bureau_B received command OFF
2016-10-15 14:53:51.367 [DEBUG] [.mqtt.internal.MqttItemBinding] - Publishing command OFF to home/verdiep/Switch/Sw_Bureau_B
2016-10-15 14:53:51.374 [DEBUG] [o.i.t.m.i.MqttBrokerConnection] - Publishing message 14 to topic 'home/verdiep/Switch/Sw_Bureau_B'
2016-10-15 14:53:51.380 [INFO ] [runtime.busevents ] - Sw_Bureau_B state updated to OFF
2016-10-15 14:53:51.489 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Switch off bureau light common after 2 minutes': cannot invoke method public abstract boolean org.openhab.model.script.actions.Timer.cancel() on null
2016-10-15 14:53:51.546 [INFO ] [nhab.model.script.Bureau light] - Time elapsed, switched off light
2016-10-15 14:53:51.560 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Switch off bureau light common after 2 minutes': cannot invoke method public abstract boolean org.openhab.model.script.actions.Timer.cancel() on null
Any reason why those errors are triggered? AFAICS, I’ve put all the necessary timer guards I need. I don’t cancel a timer when it’s null
. At least, that’s not my intention…
And I also see twice the message Will switch off bureau light common within 10 seconds
. I can’t explain this neither…
As usual, thanks for your valuable feedback.