[SOLVED] JSR223 - Jython: creating a timer

I have a script which reads data from a .ini file to set the maximum time of all lights beeing on:

log = logging.getLogger("org.eclipse.smarthome.model.script.Rules.lights.py")

@rule("Die Lichter automatisiert wieder ausschalten")
@when("Item gLights received command ON")
def lichterAutomatisertAusschalten(event):
        global lightTimer
        #falls der Timer abgelaufen ist
        if lightTimer is None or str(lightTimer.getState()) == "TERMINATED": #lightTimer is None or
                #auslesen der Werte der config
                myConfig=open("/etc/openhab2/system/runtime/environment.ini","r")
                if myConfig.mode == "r":
                        configuration=myConfig.readlines()
                        #log.info(""+configuration)
                        #myConfig=configuration.readlines()
                        #jede Zeile durch iteraten
                        for x in configuration:
                                log.info(" "+ x)
                                #finden der richtigen Zeile
                                if x.startswith("LichterMinuten"):
                                        x=x.replace("LichterMinuten=","")
                                        lightTimer(int(x), lambda: events.sendCommand("gLights","OFF")) #line 38
                                        lightTimer.start()
                                        log.info("Timer fuer alle Lichter erstellt. (Laufzeit: "+str(x)+")")
                                        myConfig.close()
                else:
                        log.info("Kann nicht auf die Datei zugreifen!")
        elif lightTimer is not None and str(lightTimer.getState()) == "TIMED_WAITING":
                lightTimer.stop()

I get the right output but I can’t create the timer with the logs saying:


I’m really new to Jython so any help is appreciated

What line of code is at line 38 in your script file?

I’ve marked it in the code, I looked through the code again and realized I didn’t instanticed the timer class.
Didn’t call the constructor, useless issue here
I already tagged it as spam but staff didn’t deleted the topic yet
Thanks for helping anyway ! :slight_smile:

It might be better if you add a response with the corrected code so if anyone else in the future has issues with timers you post might help.

1 Like

you’re right!
This line did it:

lightTimer= Timer(x, lambda: events.sendCommand("gLights","OFF"))