Use of Timers within OH 3

I was struggling with this myself today, and cobbled together bits of knowledge from a number of posts. Much thanks to @rlkoshak for his very helpful posts and knowledge!

This is what I settled on:

var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");
var ZonedDateTime = Java.type("java.time.ZonedDateTime");

if (this.timer != undefined) {
  this.timer.cancel();
  this.timer = undefined;
}

function turnOff() {
  events.sendCommand("Bath1_Fan_Switch", OFF);
  this.timer = undefined;
}

this.timer = ScriptExecution.createTimer(ZonedDateTime.now().plusMinutes(60), turnOff);
6 Likes