Help with getting triggering item/event.itemName for group member in OH3 jython rules

  • Platform information:
    • Hardware: VMware ESXI 7 / 1vCPU / 2GB RAM / 40GB HDD
    • OS: Ubuntu 20.04
    • Java Runtime Environment: The one in the docker container (JRE11?)
    • openHAB version: docker.io openhab/openhab:latest
    • Jython addon installed via Settings -> Automation -> Jython Scripting

Hello,
I downloaded the 3.0 docker container to test porting over my system from 2.5 to 3.0 and I’m having an issue where some of my Jython rules work on groups so in my 2.5 rule files I would simply handle that like this, using the helper libraries

from org.slf4j import LoggerFactory, Logger
from core.rules import rule
from core.triggers import when

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

@rule("Group member received command")
@when("Member of GGroupWithSwitches changed to ON")
def gGroupWithSwitchesChangedEvent(event):
    log.info("Triggering item name: " + event.itemName)

But in the new 3.0, it seems you can now define jython rules in the UI in addition to .py files and I would like to move towards that approach for simplicity if possible but I’m stuck in how to get that event.itemName or equivalent in this new UI based approach. Here’s my attempt at the same in the 3.0, making the UID if the rule “gGroupWithSwitchesChangedEvent”

triggers:
  - id: "1"
    configuration:
      groupName: GGroupWithSwitches
      command: ON
    type: core.GroupCommandTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/python
      script: |
        from org.slf4j import LoggerFactory, Logger
        log = LoggerFactory.getLogger("org.openhab.automation.JYTHON")
        log.info("Loading test jython rule")

        def gGroupWithSwitchesChangedEvent(event):
          log.info("Triggering item name: " + event.itemName)
    type: script.ScriptAction

When I execute this rule, I only get this in the log, presumably because the gGroupWithSwitchesChangedEvent function is not getting called

2020-12-22 10:26:58.251 [INFO ] [org.openhab.automation.JYTHON       ] - Loading test jython rule

and if I take out the “def gGroupWithSwitchesChangedEvent(event):” line, I get this but i’m not surprised since there is no declaration of the object “event” anywhere in my code

2020-12-22 10:27:19.781 [INFO ] [org.openhab.automation.JYTHON       ] - Loading test jython rule
2020-12-22 10:27:19.789 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'gGroupWithSwitchesChangedEvent' failed: NameError: name 'event' is not defined in <script> at line number 6

Is there a way to do what I want to do with jython in UI specified rules? I have not had much luck finding any info from searches that isn’t for 2.5 with .py files and the helper libraries and those don’t seem to be available yet, although for my simplistic example I don’t know if they would really be needed anyway.

So it turns out that the “event” special item doesn’t exist if you use the play button to test a rule, in hindsight this seems obvious but live and learn I guess. So this code actually works fine if triggered by an actual group item.

log.info("Triggering item name: " + event.itemName)
1 Like