Jython - help with types

Hello everyone,
I am currently getting into the whole JSR223 Jython Scripting stuff and stumbeld across something I cannot figure out myself.

heres a simple rule I created for a alarm:

from openhab.log import logging
from openhab.triggers import time_triggered, EVERY_MINUTE

from datetime import datetime, time

log = logging.getLogger("org.eclipse.smarthome.automation")

def alarm():
    if str(items["alarm_activated"]) == "ON":
        log.info("alarm rule every minute test")
        alarm_hour = int(str(items['alarm_hour']))
        alarm_minute = int(str(items['alarm_minute']))
        now = datetime.now()
        if now.hour == alarm_hour and now.minute == alarm_minute:
            log.info("alarm executed")
            events.sendCommand("szeneBedNightlight", "ON")

Notice the int(str(items['alarm_hour'])) … there has to be a more elegant way for type conversions in jython right?

Thanks for any help :wink:


Note that the openHAB Item interface has a method called getState. Jython automatically allows calling get/set methods as a Jython property (item.state instead of item.getState()).

Also look at the specific state types, like DecimalType, to find the methods that can be called on instances of them.

Remember, in many cases, Jython is just calling the openHAB/SmartHome Java objects directly.

1 Like

Thank you, that looks way better.

I did not need the .state however, the members of items already are DecimalType, so this works fine:

alarm_hour = items['alarm_hour'].intValue()

the documentation on jython is frankly pretty scattered around, maybe I will write a small tutorial as soon as I feel to have understood it enough.
Anyway, thank you :wink:


Did not know this notation, currently I am using ir.getItem(“Washing_Machine_State”).getState().intValue() but your notation us much better to read!