OH3 JSR223 Rules

  • Platform information:
    • Hardware: Raspi 4B
    • OS: Linux/5.4.79-v7l+ (arm)
    • Java Runtime Environment: 11.0.9 (Zulu11.43+88-CA)
    • openHAB version: 3.1.0 “Build #2123
  • Issue of the topic: JSR223 Rules migration from OH 2.5.11 to OH 3.0.0

I did a complete fresh openhabian install on a Raspi 4B and updated to OH3 everything is up and running, including the Jython helper libraries, set up according to this

All Rules (30+) are up and running without any errors except one.
I struggle with the syntax and have no idea where to start.
Here is the rule which was working on OH 2.5.11

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

@rule("Muell")
@when("Time cron 0 0 17 1/1 * ? *")
def muell(event):
 
     muellDateTime = DateTime(str(items["Muellkalender_Ergebnisstart1t"]))  #Item State is DateTime Format
     muellname = (str(items["Muellkalender_Ergebnistitel1"]))
     now = DateTime.now()
     if now.isAfter(muellDateTime.minusDays(1)) and now.isBefore(muellDateTime):
         actions.get("telegram", "telegram:telegramBot:1f84b343").sendTelegram(-326642842, "Morgen ist " + muellname + ",  nicht vergessen")

I know in OH 3 i have to import java.time

from core.rules import rule
from core.triggers import when
from java.time import ZonedDateTime as DateTime
from org.slf4j import LoggerFactory, Logger

log = LoggerFactory.getLogger("org.openhab.automation.JYTHON")
log.info("Loading test jython rule ")

@rule("Muell")
@when("Time cron 0 0 17 1/1 * ? *")
@when("Item TasmotaTest_state changed")
def muell(event):
     
     muellDateTime = ir.getItem("Muellkalender_Ergebnisstart1").state
     muellname = str(items["Muellkalender_Ergebnistitel1"])
     now = DateTime.now()
     #if now.isAfter(muellDateTime.minusDays(1)) and now.isBefore(muellDateTime):
      #   actions.get("telegram", "telegram:telegramBot:9b4ccbc1b4").sendTelegram(-326642842, "Morgen ist " + muellname + ",  nicht vergessen")
     log.info(str(muellDateTime))
     log.info(muellname)
     log.info(str(now))

If i log muellDateTime, muellname and now as string i get this output

log

If i run the full scipt i get this Log.

2021-01-08 19:41:27.150 [WARN ] [jsr223.jython                       ] - Traceback (most recent call last):

  File "/etc/openhab/automation/lib/python/core/log.py", line 65, in wrapper

    return function(*args, **kwargs)

  File "/etc/openhab/automation/lib/python/core/rules.py", line 109, in execute

    self.callback(inputs.get('event'))

  File "<script>", line 77, in muell

AttributeError: 'org.openhab.core.library.types.DateTimeType' object has no attribute 'minusDays'

I have no Idea where to start.
Any help appreciated

Roli

Your problem is that you use the item type (org.openhab.core.library.types.DateTimeType) here and expect it to be a ZonedDateTime, which has minusDays, plusSeconds, etc, methods.

As you can see in the javadoc of org.openhab.core.library.types.DateTimeType, a DateTime Item has the getZonedDateTime() method gives you the right java type.

So it should work like this:

     muellDateTime = ir.getItem("Muellkalender_Ergebnisstart1").state.getZonedDateTime()
     muellname = str(items["Muellkalender_Ergebnistitel1"])
     now = DateTime.now()

     if now.isAfter(muellDateTime.minusDays(1)) and now.isBefore(muellDateTime):
         actions.get("telegram", "telegram:telegramBot:9b4ccbc1b4").sendTelegram(-326642842, "Morgen ist " + muellname + ",  nicht vergessen")

     log.info(str(muellDateTime))
     log.info(muellname)
     log.info(str(now))

Thank you Frank, works like a charm.