Timers (canceling timer by item state) in Blockly rules (Help needed)

I have a rule that when a music player is stopped (Sonos) (itemstate changed to PAUSE), it turns off an amplifer after 10 min (by sending commands to a Global caché IP controler onnect by RS232 to the amp)

But if the state of player changes to PLAY again during this 10min (during the timer), it should cancel the timer and not turn off the amp after those 10min.

2022-12-10 21:31:36.498 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'SonosConnectSalon_MediaControl' changed from UNDEF to PLAY

It tried this but it does not cancel the timer.

(I put 30seconds timer just for testing purpose)

Any idea/tips how to make it work?

What triggers this script that you have shown?

My conclusion also.if this is triggered when the item state changed to pause, the if clause will never be true

it is triggered by the itemstate change of the player:

2022-12-10 21:31:25.803 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'SonosConnectSalon_MediaControl' changed from PLAY to PAUSE

It is triggered by item state changed to pause, but if during this 10 min i decide to press play again, the timer should be cancelled.

For example, i have phone call, or someone at the door, i can press play again.

Ps: sonos players don’t have a different Play/Pause button. it’s 1 button.

Will your rule get triggered again?

Once you’ve solved that, the next thing is, do not even start the timer if the state is not pause. Pseudocode:

cancel timer
if state == pause {
  start timer

In the above code you’d cancel timer regardless of whether it’s pause or play, then start a timer when it’s pause.

It’s really helpful to show the full rule trigger here, by using the code tab.

As you’ve helpfully included a logInfo to show when the trule runs, it would be helpful to include the log of that (openhab.log) as well as the events.log

But you misunderstand how rules and timers work, perhaps.

When the rule is triggered and run, it sets up the timer code for future execution. Then having done the setup, the rule immediately continues executes any following code (the if() block in this case) and the rule exits, all within milliseconds.
It’s like setting an alarm clock - the clock does not stop.

Then the timer code runs at some later time.

Perhaps you meant to trigger the rule on some other changes or updates, so that it can examine the current change and cancel any running timer.

here is the full code, i did not include it, because i have no programing skills, and wanted to try to solve this in blockly.

configuration: {}
  - id: "1"
      itemName: SonosConnectSalon_MediaControl
      state: PAUSE
      previousState: PLAY
    type: core.ItemStateChangeTrigger
conditions: []
  - inputs: {}
    id: "2"
      blockSource: <xml xmlns="https://developers.google.com/blockly/xml"><block
        type="oh_log" id="$4g_5qbc_;MhvfqzV=Np" x="166" y="27"><field
        name="severity">info</field><value name="message"><shadow type="text"
        id="{-H8a?x;L?T5k}*tGl^$"><field name="TEXT">Sonos Salon state change to
        Pause</field></shadow></value><next><block type="oh_timer"
        name="delayUnits">plusMinutes</field><value name="delay"><shadow
        type="math_number" id="6Rgu/rE=i+[;]Xg=.x{O"><field
        name="NUM">2</field></shadow></value><value name="timerName"><shadow
        type="text" id="es=;4o$#u3/u@*Z.U)Ix"><field name="TEXT">Sonos OFF
        timer</field></shadow></value><statement name="timerCode"><block
        type="oh_store_value" id="m6NFDzJ-w=4v9VZUjRQi"><value
        name="value"><shadow type="text" id="[QV2c@9Y@sEzVXvj:~|M"><field
        name="TEXT">value</field></shadow><block type="oh_getitem_state"
        id="].N5q/6CsVWVblM*chwu"><value name="itemName"><shadow type="oh_item"
        name="key"><shadow type="text" id="J_+zyHv9NA*e/-+V|P@A"><field
        name="TEXT">Sonos state</field></shadow></value><next><block
        type="controls_if" id="d`Hb[PLO{wY^W{N%Qp:J"><value name="IF0"><block
        type="logic_compare" id="w@8m#9GRz2}Q3e|JG8=2"><field
        name="OP">EQ</field><value name="A"><block type="oh_get_value"
        id="%=yK7/MW`d,tkf|~vJEb"><value name="key"><shadow type="text"
        id=";H=`v4:YQpYE6ky_=ZJU"><field name="TEXT">Sonos
        state</field></shadow></value></block></value><value name="B"><block
        type="text" id="0#feZh*$farWFsGun$]h"><field
        name="DO0"><block type="oh_timer_cancel"
        id="]Ax.PQquHVo(_OmvoVH+"><value name="timerName"><shadow type="text"
        id="1h8eLQ/Kw?dYEvKGmKml"><field name="TEXT">Sonos OFF
        type="oh_log" id="Mxj4GpoMFf[M_c#10~w|"><field
        name="severity">info</field><value name="message"><shadow type="text"
        id="|f/Vr+v$02qH4ppA!oyS"><field name="TEXT">Set volume to
        -30.0</field></shadow></value><next><block type="oh_event"
        name="eventType">sendCommand</field><value name="value"><shadow
        type="text" id=":/?O`%]={kLwv3uSNC-W"><field
        name="itemName"><shadow type="oh_item" id="|(*}0mqxbg[]hG/Lz*]s"><field
        type="oh_sleep" id=":/~tJ[v_out{:+QQj(x3"><field
        name="milliseconds">1000</field><next><block type="oh_log"
        id="4!gJ_F.`.,6bF|kkbnrI"><field name="severity">info</field><value
        name="message"><shadow type="text" id="ZUiOHW?qR6q)+n*uh}xl"><field
        name="TEXT">Turn Amp Off</field></shadow></value><next><block
        type="oh_event" id="sHCivY1|z}y2hc#NS/mZ"><field
        name="eventType">sendCommand</field><value name="value"><shadow
        type="text" id="s[GTNmq,)lz+BM[W%NP;"><field
        name="itemName"><shadow type="oh_item" id="i2fU/!_fV9wdu/Ni@|IT"><field
      type: application/javascript
      script: >
        var logger =
        Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' +

        var scriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');

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

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

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

        var thread = Java.type('java.lang.Thread')

        logger.info('Sonos Salon state change to Pause');

        if (typeof this.timers['Sonos OFF timer'] === 'undefined' || this.timers['Sonos OFF timer'].hasTerminated()) {
          this.timers['Sonos OFF timer'] = scriptExecution.createTimer(zdt.now().plusMinutes(2), function () {
            this.storedValues['Sonos state'] = itemRegistry.getItem('SonosConnectSalon_MediaControl').getState();
            if ((this.storedValues['Sonos state']) == 'PLAY') {
              if (typeof this.timers['Sonos OFF timer'] !== 'undefined') {
                this.timers['Sonos OFF timer'].cancel();
                this.timers['Sonos OFF timer'] = undefined;
            logger.info('Set volume to -30.0');
            events.sendCommand('GlobalCacheGC10012_Slm1_C1direct', '%02%06%a0%52%00%01%e2%00%25');
            logger.info('Turn Amp Off');
            events.sendCommand('GlobalCacheGC10012_Slm1_C1direct', '%02%04%a0%60%00%00%fc');
    type: script.ScriptAction

Try something like this.

Here is the code.

After the state goed to PAUSE it turns off the amp after 2min which is normal.
But if i press P¨LAY again during those 2 min it does nog cancel the timer

2022-12-11 09:10:02.548 [INFO ] [org.openhab.rule.eb31657142         ] - Sonos Salon state change to Play
==> /var/log/openhab/events.log <==
2022-12-11 09:10:02.567 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'GlobalCacheGC10012_Slm1_C1direct' received command %02%04%a0%60%00%01%fb
2022-12-11 09:10:02.569 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'GlobalCacheGC10012_Slm1_C1direct' predicted to become %02%04%a0%60%00%01%fb
2022-12-11 09:10:02.579 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'GlobalCacheGC10012_Slm1_C1direct' changed from %02%06%a0%52%00%01%e2%00%25 to %02%04%a0%60%00%01%fb
2022-12-11 09:10:03.174 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'GlobalCacheGC10012_Slm1_C1direct' received command %02%04%a0%42%00%02%18
2022-12-11 09:10:03.176 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'GlobalCacheGC10012_Slm1_C1direct' predicted to become %02%04%a0%42%00%02%18
2022-12-11 09:10:03.179 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'GlobalCacheGC10012_Slm1_C1direct' changed from %02%04%a0%60%00%01%fb to %02%04%a0%42%00%02%18
2022-12-11 09:10:03.784 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'GlobalCacheGC10012_Slm1_C1direct' received command %02%06%a0%52%00%01%ff%fe%0a
2022-12-11 09:10:03.787 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'GlobalCacheGC10012_Slm1_C1direct' predicted to become %02%06%a0%52%00%01%ff%fe%0a
2022-12-11 09:10:03.792 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'GlobalCacheGC10012_Slm1_C1direct' changed from %02%04%a0%42%00%02%18 to %02%06%a0%52%00%01%ff%fe%0a
2022-12-11 09:10:05.984 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'SonosConnectSalon_MediaControl' changed from UNDEF to PLAY
==> /var/log/openhab/openhab.log <==
2022-12-11 09:10:14.586 [INFO ] [org.openhab.rule.911aba0794         ] - Sonos Salon state change to Pause
==> /var/log/openhab/events.log <==
2022-12-11 09:10:14.540 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'SonosConnectSalon_MediaControl' changed from PLAY to PAUSE
==> /var/log/openhab/openhab.log <==
2022-12-11 09:11:58.418 [INFO ] [org.openhab.rule.911aba0794         ] - Set volume to -30.0
==> /var/log/openhab/events.log <==
2022-12-11 09:11:58.430 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'GlobalCacheGC10012_Slm1_C1direct' received command %02%06%a0%52%00%01%e2%00%25
2022-12-11 09:11:58.433 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'GlobalCacheGC10012_Slm1_C1direct' predicted to become %02%06%a0%52%00%01%e2%00%25
2022-12-11 09:11:58.446 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'GlobalCacheGC10012_Slm1_C1direct' changed from %02%06%a0%52%00%01%ff%fe%0a to %02%06%a0%52%00%01%e2%00%25
==> /var/log/openhab/openhab.log <==
2022-12-11 09:11:59.438 [INFO ] [org.openhab.rule.911aba0794         ] - Turn Amp Off
==> /var/log/openhab/events.log <==
2022-12-11 09:11:59.450 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'GlobalCacheGC10012_Slm1_C1direct' received command %02%04%a0%60%00%00%fc
2022-12-11 09:11:59.453 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'GlobalCacheGC10012_Slm1_C1direct' predicted to become %02%04%a0%60%00%00%fc
2022-12-11 09:11:59.460 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'GlobalCacheGC10012_Slm1_C1direct' changed from %02%06%a0%52%00%01%e2%00%25 to %02%04%a0%60%00%00%fc

Of course it doesn’t - as everyone else has said: you have setup your rule to ONLY trigger when your Item changes FROM play TO pause.

      itemName: SonosConnectSalon_MediaControl
      state: PAUSE
      previousState: PLAY
    type: core.ItemStateChangeTrigger

So if you click PLAY, this rule will NOT run, so your timer will NEVER cancel.

Change the trigger in your rule to remove the pause and play bits, so that this rule runs whenever the Item changes state (from anything to anything).

There is no “SonosConnectSalon_MediaControl” change within the 2 minutes!

OMG! Of course!!

Thank you guys, it works now!

Here is my blockly:

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