Timer Keeps Running [SOLVED}

  • Platform information:
    • Hardware: Pi4B 4GB
    • OS: openhabian Raspian/GNU Buster
    • Java Runtime Environment: JDK 11
    • openHAB 3.2 Release Version
  • Issue of the topic: I made my first rule using Blockly to create a script. It is a simple rule, to turn off the bathroom exhaust fan after it runs for 3 minutes. The problem is that the timer keeps repeating and sends an off command to the fan’s power switch every 3 minutes. I’m sure I’m missing something really simple.

Here is the code from the Main UI Rule:

configuration: {}
  - id: "1"
      itemName: MasterBathToiletFanMQTTThing_MasterBathToiletFanSwitch
    type: core.ItemStateUpdateTrigger
conditions: []
  - inputs: {}
    id: "2"
      blockSource: <xml xmlns="https://developers.google.com/blockly/xml"><block
        type="oh_timer" id="k{{F^64m8/X:m~9nUH/=" x="183" y="-729"><field
        name="delayUnits">plusMinutes</field><value name="delay"><shadow
        type="math_number" id="{UJoh9Ng].z}6GoV1P=#"><field
        name="NUM">3</field></shadow></value><value name="timerName"><shadow
        type="text" id="C6zI/@u/aOTUBe,-Qm`1"><field
        name="timerCode"><block type="oh_event" id="ey`lbnT4)Tl4NSTsqilL"><field
        name="eventType">sendCommand</field><value name="value"><shadow
        type="text" id="-Us@+~TACEKtnVl@~;{X"><field
        name="TEXT">OFF</field></shadow></value><value name="itemName"><shadow
        type="oh_item" id="WxuMRfoI`+CiUrhnk1MU"><field
        type="oh_timer_cancel" id="mR!aQp:G?TWRXl8~mRMc"><value
        name="timerName"><shadow type="text" id="qlYb{aO^:?!KpIia|(l{"><field
      type: application/javascript
      script: >
        var scriptExecution =

        var zdt = Java.type('java.time.ZonedDateTime');

        if (typeof this.timers === 'undefined') {
          this.timers = [];

        if (typeof this.timers['MasterToiletFanTimer'] === 'undefined' || this.timers['MasterToiletFanTimer'].hasTerminated()) {
          this.timers['MasterToiletFanTimer'] = scriptExecution.createTimer(zdt.now().plusMinutes(3), function () {
            events.sendCommand('MasterBathToiletFanMQTTThing_MasterBathToiletFanSwitch', 'OFF');
            if (typeof this.timers['MasterToiletFanTimer'] !== 'undefined') {
              this.timers['MasterToiletFanTimer'] = undefined;
    type: script.ScriptAction

Here is screenshot of the Blockly:

Thanks for any suggestions.

Add a log statement before the timer is created. Perhaps the rule is triggered more than once.

If so you probably need to use a different block:

You can choose what to do with the Timer when the rule runs again. You probably want to reschedule.

I use a “changed” trigger, then:


I don’t know if the timer persists after shutting off the fan, doesn’t seem to. This also cancels timer if manually turned off at switch.

Thanks. I learned a lot from both of your suggestions.

Of course the problem was simple user error. When I created the new rule, I used the UI to create the trigger, and I left the State blank, so it was triggering every time the state of the switch changed, not just when it came on. So the timer was being triggered every time it received the off from the previous timer cycle. I added ON to the state in the UI and now it performs as expected. Sorry I missed that.


Glad you found it. What happens if you manually turn off wall switch? That is an OFF trigger your code doesn’t capture, and timer will still run.

1 Like

Good point about the OFF situation. Not likely a problem in this instance, but not good programming practice.

I don’t see the “if do else if do” block in Blockly. Are there different versions, or am I not looking in the right place.?

It’s the If....do block. Drag that out, then click on the gear icon to add elseif and else.


Thanks. It took me a minute. I kept trying to drag it down to the main block. Once I tried on a laptop instead of a phone, it was clear that I needed to drag to the other side of small box pointing at the gear.

I’m wondering if the OFF section won’t be triggered on start up, when the Item status goes from NULL to OFF. Not a big deal.