How to create and reschedule a timer with OH 3.1?

I’m leaving this boilerplate, even though it is not really important for my question.

  • Platform information:
    • Hardware: RPI4, openHAB in a docker-container
    • OS: Raspbian, kernel 5.10
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version: 3.1, mainly with GUI rules

I’m trying to brief:

I have a light which I’d like to have turned off automatically after 5 minutes after it has been turned on.
This light is turned on by different switches or by motion detectors.
I’d like to reschedule the turn-off if a motion is detected during the 5 minutes timeout.

How do I implement such a rule with OpenHAB 3.1 with GUI rules? I’m triggering on the transition of the light-state (OFF → ON) or on motion of my detectors. But what to do in the then section?

Of course I did some research and found good stuff. The nicest one was a scripted rule with triggers inside which declared a global variable containing the timer and if it was not null it was reschedule otherwise created. With GUI rules this seems not to be possible any longer.

Then I read about an expire-addon, which is now integrated in OH directly, but I didn’t find an example how to use it with OH 3.1 GUI.

Can I create a temporary cron/timer-item/thing which is deleting itself once fired and is created or edited by my rule to reschedule?

Expire example:

Thanks for making me learn about expire-metadata on items.

This solves the issue where I want to turn off the light after 5 minutes (as it creates an expiration on item-state, iiuc), but this doesn’t allow me to reschedule this expiration when a new motion-event is received, or does it?

Every update to the item resets the timer.
Try it.

No. But your motion event can trigger a rule to command or update the light ON (even though it’s already ON) which will restart expire timer.

Expire is deliberately simplistic, and will not suit every timing need. There is always more than one way to do things. Rules based timers allow more subtle controls.

I use a rule like this:
The rule turns on the light from the motion sensor and the light has the expire timer.
If the motion sensor sends again the light timer is reset.
Simple but it works well.

As you’ve discovered the expire metadata isn’t really reliable as a timer since any item updates trigger it. What you can do is setup your rule via the gui and use a short script to execute your action in the ‘then’ statement via add action > run script > ecma javascript. I’ve been using the following successfully, cobbled together from a number of community posts. Cut and paste it into the ‘then’ as ecma javascript and update the Item variable and TimerMinutes to suite.

var logger = Java.type(‘org.slf4j.LoggerFactory’).getLogger(‘org.openhab.rule.’ + ctx.ruleUID);
var ScriptExecution = Java.type(“org.openhab.core.model.script.actions.ScriptExecution”);
var ZonedDateTime = Java.type(“java.time.ZonedDateTime”);
var TimerMinutes = 5;
var Item = “PantryLight_Power”;

if (this.timer != undefined) {

function turnOff() {
events.sendCommand(Item, OFF); +" Expire Rule after “+TimerMinutes +” minutes");
this.timer = undefined;

this.timer = ScriptExecution.createTimer(, turnOff);

To clarify, expire is perfectly reliable, this is exactly what it is supposed to do.

It was designed as a tool to detect lack of updates (e.g. broken device) and that is what it is good at.
We’re “misusing” it a little as a general purpose timer, it is just about understanding the limitations.

1 Like

I believe I said the expire metadata isn’t reliable as a timer…which it kind of isn’t. Point is well taken though, we probably shouldn’t be trying to use it as such.