OH3 JSR223 Rules: ID

Hi guys,

When creating a rule via Main-UI, a rule can get a custom ID.
How to do the same when creating a file based rule? The tags id or ‚uid don’t work…
(I’m not talking about name)

Background: log entries with random rule ID’s a la Failed to execute rule 'fd1a1200-6044-4be6-bdb3-916242375f1b': Fail to execute action: 1 are not too helpful in identifying which rule is creating trouble…

Do you get those from file based rules? I’ve only seen ‘xxxx-2’ where xxxx is the filename and 2 the second rule in that file. That’s DSL rules, though it’d be surprising if it was different for other languages.

Jep, from file based Jython rules.

I don’t thing I ever had that behavior - but that might be because I rewrote all my old DSL rules as Jython rules :thinking:

To give on more complete example - I have the following rule in <openhab_root_dir>/conf/automation/jsr223/python/personal/startup.py:

@rule("On Startup", description="initialize items with state NULL", tags=["maintenance"])
@when("System started")
def onStartup(event):
    onStartup.log.info("Rule \"On Startup\" triggered")

    global myTimer
    global delay
    onStartup.log.debug("if timer != null: cancel timer")
    if myTimer is not None and not myTimer.hasTerminated():
        myTimer.cancel()

    def timerFunc():
        global startupDict
        onStartup.log.debug("for itemName, state in startupDict.items()")
        for itemName, state in startupDict.items():
            onStartup.log.debug("itemName: " + itemName + ", state: " + state)
            if ir.getItem(itemName).state == NULL:
                onStartup.log.debug("current state is NULL, set state: " + state)
                events.sendCommand(itemName, state)
    
    onStartup.log.debug("createTimer with " + str(delay) + " min delay")
    myTimer = ScriptExecution.createTimer(ZonedDateTime.now().plusMinutes(delay), timerFunc)

And, leaving my own log messages aside, the log entry reads 2021-05-13 10:20:35.128 [DEBUG] [e.automation.internal.RuleEngineImpl] - The rule '36c4aa58-f7c8-421b-aa62-a376f74249bc' is executed..
As I said, not really helpful…

Are you running my fork if the Helper Libraries? If so you should be getting a Python stack trace when a rule crashes. It uses {LOG_PREFIX}.jython as a logger if I recall correctly.

Unfortunately there doesn’t seem to be a way (at least that I know of) to have the log entry you note give the rule name. If you wanted to find the rule by UID, they are visible in the rule list in the OH3 main UI.

Yes - thx for your work :grin:

Yes, that works fine when I write something to the log with <ruleName>.log.debug() - but I would like to influence the rules ID so that the automatic log messages make more sense…
As said before - when you create a rule via Main-UI, you can decide on your own custom rule ID (besides name, description, tags…), but not when you use a file based rule…

Would it be possible to create an additional keyword for the @rule part to set an ID?

Probably, but you don’t need it to get the details of why it failed. As I said there is a Python stack trace that gets printed to the log when a Python rule errors. Are you not seeing it in your log?

Hi @fex,

The new tag is a great idea. I have some ui cards to trigger rules or I would but the ids always change and my card cannot trigger the rule anymore.

1 Like

@JanK interesting, I did not know you could do this.

To anyone interested in adding this: there is a UUID validation function in utils.py already. You would need to add a parameter to the rule function and run that through the validator, accounting of course for it not being supplied or an invalid value being supplied.
I can add this but I don’t know when I will have time right now, but if someone submits a PR tested on OH2 and OH3 then I can pop over and approve it.

1 Like

Yes, if there occurs an error, you get the stack trace. My beef with the random rules ID is more general - I want to know for each log entry which rule caused it - even if it is only something like Rule XY executed :slight_smile:

I just got started with Python in general and Jython rules in the specific - so it might take some time until I feel comfortable enough with the Helper-Libraries to contribute. But I’m not saying no :sweat_smile:
But I would also not be sad if someone else solves that issue in the meantime :grin: - so I created an GitHub issue.

1 Like