Is it possible to abort a timer?

Hey guys,

is there any chance to abort a running timer?

Let me explain. I created some kind of a timer switch for the poweroutlet of my 3D printer. Idea behind this is that I sometimes start prints over night or while I’m at work. As I know how long the print will take I can define the time how long the printer should be powerd. Because if the print takes 3 hours and I’m in the office for 10 hours there is no need for having the printer powerd all the time.

In my sitemap I have a slider to select the time I want the poweroutlet to be on. When I start the timer (with a button in my sitemap) the poweroutlet switches to on (so printer is powerd) and a timer starts with the number of hours I defined in the sitemap. After timer ends poweroutlet switches to off. Pretty easy thing.

Here is the rule:

rule "Zeitschaltuhr 3D Drucker"
when
	Item KGD_Draussen_Zeitschaltuhr_3D_Drucker changed to ON
then
	Drei_D_Drucker.sendCommand(ON)
	KGD_Draussen_Zeitschaltuhr_3D_Drucker.sendCommand(OFF)
	createTimer(now.plusHours((Zeitspeicher_3D_Drucker.state as DecimalType).intValue))[|
	Drei_D_Drucker.sendCommand(OFF)
	]
end

Drei_D_Drucker = Poweroutlet of the 3D Printer
Zeitspeicher_3D_Drucker = Number item which has a slider in sitemap
KGD_Draussen_Zeitschaltuhr_3D_Drucker = Switch in the sitemap to start the timer

So far so good.
But sometimes it happens that a print breaks in the print process. So normaly before I leave or I go to bed I check the print. If a see a problem I abort the print, fix it and restart it or just restart it. But as the timer is already running in the background the printer will turn off after the selected time.

So is there any chance to abort this timer?

createTimer returns a pointer to that running Timer. Save that to a variable defined outside of the rule as a global variable.

As described in the docs

The Timer object supports the following methods:

  • cancel: prevents the scheduled timer from executing. Most of the time cancel is used used in conjunction with setting the timer handler to null as a convenient indicator that some previously defined timer is now finished with. However setting the handler to null does not interact with the timer itself.
  • isActive: returns true if the timer will be executed as scheduled, i.e. it has not been cancelled or completed.
  • isRunning: returns true if the code is currently executing (i.e. the timer activated the code but it is not done running).
  • hasTerminated: returns true if the code has run and completed.
  • reschedule(AbstractInstant instant): reschedules the timer to execute at the new time. If the Timer has terminated this method does nothing.

So check to see if the Timer exists. If if does, reschedule it. If it does not create a new Timer.

If you search the forum for createTimer you will find hundreds if not thousands of additional examples if the one in the docs is not clear. Or ask and we can try to help.

If your printer works with OctoPrint, I highly recommend setting it up. You can then send status messages to openHAB via MQTT or REST. I did this recently, and now I have toggles in my sitemap that enable me to do two things:

  1. Turn off the printer after it has cooled down to 50 degrees (so there’s no need for a timer).
  2. Send a notification to the openHAB app when the print is complete.

I could have just made these default rules, but I don’t always want the printer to turn off when it’s done, and I don’t always want a notification.

I’d been thinking about trying this for awhile, and I wish I had done it sooner.

@rlkoshak thank you very much Rick - Will check that. Descriptions look handy so I guess I will be able to find a solution.

@rpwong I’m very new to 3D printing. For now I start my prints manually but I already have an eye on octoprint. Good to know that octoprint and openhab can be combined. Especially notification is nice. Currently I set a timer in my mobile phone to remind me that the print should be ready… having a smarthome but setting timers for events is not the “smarthome way” :wink: but I have some other projects running / planned which I have to focus on first :frowning: