Variable timer set with a slider input OH3 rule

I am sure this is been don but I could not find it and I thought it could come in handy
I was able to piece this together thanks to many posts from others here it is likely not the best way to do things but it worked for me I was able to get feed back in the log as I went I’m still pretty green at scripting rules

but the context here is my bedroom tv has a terrible menu system for enabling the sleep timer
but openhab of course can control it so I added a rule and a couple of items and I have a timer i can set with my phone or by voice with google

setup is one switch to toggle the timer and one dimmer
I set my length of time with the dimmer and then turn on the timer with switch
which triggers the following rule

var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");
var ZonedDateTime = Java.type("java.time.ZonedDateTime");
var logger          = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var timer_time = items["Timmerthings_Timedimmer"];
logger.info("timer started");

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

function turnOff() {
  logger.info("bedroomtv should be off");
  events.sendCommand("Bedroomtv_Power", OFF);
  events.sendCommand("Timmerthings_Timedimmer", 0);
  events.sendCommand("Timmerthings_Bedroomtvtimerswitch", OFF);
  this.timer = undefined;
}

this.timer = ScriptExecution.createTimer(ZonedDateTime.now().plusMinutes(timer_time), turnOff);

this works quite well I would like to in the future be able to cancel the timer by turning the switch off
but I have not gotten that far yet

anyways thanks for all the help. hopefully this helps someone else I will happily answer questions to the best of my ability if needed

Have a look into the event variable and trigger your rule not only once your switch will turn on, but trigger your rule based on any change of your switch item.

https://openhab-scripters.github.io/openhab-helper-libraries/Guides/Event%20Object%20Attributes.html

Thanks I looked at that
while I was piecing my current script together
it doesn’t seem that hard to add the canceling feature
just haven’t tested or tried it

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.