[SOLVED] Getting timer remaining time

I want to show how much time before a timer runc, but as long as I see it is impossigle (Determine Timer Elapsed or Remaining) has something changed in these nearly 4 years?

Nope, nothing changed.

One alternative approach -


Another -

If you use Scripted Automation, I submitted a countdown timer module to the Python Helper Libraries.

It’s largely based on rossko57’s original Rules DSL idea but doesn’t use Expire. You just need to copy it to your automation folder and instantiate it in a Rule, passing it the time duration, the function to call when it expires, and an Item to update every second while the timer counts down.

Wow! I have a lot to learn to keep up!
I am having some trouble with a timer because of a not as good as expected humidity sensor.
I have a fan in bathroom to remove humidity after shower, but some time my sensors halt so I cannot run a trigger “if humidity higher than”, so I started playing with timers setting and rescheduling “on sensor update”, but then … and then …
So I was searching for a way to get information and debug. Most of time I end with telegram bot messages, but I was looking for a way to publish this info on sitemap.
I will have to take a look at next generation rule engine.

Thanks to both of you

consider using persistence and a moving average

You probably don’t need these complicated countdown timers then. Just a fixed length timer to be started or rescheduled? Place logInfo()s in your rule(s) to drop useful messages, so that you can follow what is going on in your openhab.log

Hi,

I try to implement this Countdown Timer. Generaly it works, but I don’t know how to cancel it if the switch goes OFF before time expires.

import time
import personal.countdown_timer
reload (personal.countdown_timer)
from personal.countdown_timer import CountdownTimer
from core.log import log_traceback, logging, LOG_PREFIX
from datetime import datetime, timedelta
log = logging.getLogger("{}.TEST.countdown_timer".format(LOG_PREFIX))

from core.rules import rule
from core.triggers import when

func_called = False

def test():
    global func_called
    func_called = True

number = "alarmTime"         // number Item for countdown
delay_item = "alarmDelay"    // switch Item for starting countdown


@rule("Countdown timer", description="Start countdown for getting out")
@when("Item alarmDelay received update")
def countdown_timer(event):
    if((items[delay_item]) == ON):
        timer = CountdownTimer(log, (datetime.now() + timedelta(seconds=10)), test, number)
        time.sleep(0.1)

#   elif((items[delay_item]) == OFF):
#       log.info("Cancel...")
#       timer.cancel()
 


If I try to do else or elif timer.cancel, I got local variable ‘timer’ referenced before assignment error. As I am total noob in python, could you please show me some example how it should be implemented?

timer.cancel()

Thanks,

but if I try to use it:

   elif((items[delay_item]) == OFF):
       log.info("Cancel...")
       timer.cancel()

I got error:

2020-01-22 15:48:07.685 [ERROR] [jsr223.jython.Countdown timer ] - Traceback (most recent call last): File "/etc/openhab2/automation/lib/python/core/log.py", line 51, in wrapper return fn(*args, **kwargs) File "<script>", line 31, in countdown_timer UnboundLocalError: local variable 'timer' referenced before assignment

You have to declare timer as a global variable.

Really don’t know how to do it correctly. This couses to run timer just when script is loaded, it is possible to cancel it, but can’t be started with error TypeError: 'CountdownTimer' object is not callable

def test():
    global func_called
    func_called = True

number = "alarmTime"
delay_item = "alarmDelay"

timer = CountdownTimer(log, (datetime.now() + timedelta(seconds=10)), test, number)


@rule("Countdown timer", description="Start countdown for getting out")
@when("Item alarmDelay received update")
def countdown_timer(event):
    if((items[delay_item]) == ON):
           timer.cancel()
           timer()
        time.sleep(0.1)

    elif((items[delay_item]) == OFF):
             log.info("Cancel...")
            timer.cancel()

number = "alarmTime"
delay_item = "alarmDelay"

timer = None


@rule("Countdown timer", description="Start countdown for getting out")
@when("Item alarmDelay received update")
def countdown_timer(event):
    global timer
    if((items[delay_item]) == ON):
        timer = CountdownTimer(log, (datetime.now() + timedelta(seconds=10)), test, number)
        time.sleep(0.1)

    elif((items[delay_item]) == OFF):
             log.info("Cancel...")
            timer.cancel()

Aaah… Thanks, it is working OK now!
as I said - I’m total noob with this.