Getting ruleName in js

oH 4.3.0
How can I query the name of a rule in js within this rule?

ruleUID

console.log("Rule",ruleUID,"started.“);

Thanks, but I tryed it before and got
2024-12-22 09:09:14.179 [WARN ] [e.automation.internal.RuleEngineImpl] - Failed to execute action: 1(Error: Failed to execute rule BalconyLightsOn: ReferenceError: “ruleUID” is not defined: ReferenceError: “ruleUID” is not defined

Maybe this:

//log the rule number that ran.
console.log("Ran rule: " + ctx.ruleUID)

2024-12-22 11:03:47.334 [ERROR] [enhab.automation.script.file.test.js] - Failed to execute rule rule-id: ReferenceError: “ctx” is not defined: ReferenceError: “ctx” is not defined

I am running openHab 4.3.0 and it works for me.

Do you have javascript installed from the addons?
This add-on provides support for JavaScript (ECMAScript 2022+) that can be used as a scripting language within automation rules. It is based on GraalJS from the GraalVM project.

You probably still have the old js script addon (nashorn) installed. Please make sure you have installed this:
https://www.openhab.org/addons/automation/jsscripting

and make sure that usage of integrated variables is activated in the settings.

I have not installed nashorn, only

and “usage of integrated variables” is activated

Are you using file based rules? Then this might not work.

edit: And please double check if your rules have this tag:

type: application/javascript

which can be seen in the code tab of your managed rule.

ctx only exists in UI based rules.

I only use file based rules

Ok. ruleUID works for managed rules. I do not know if there is an equivalent for file based rules.
You need to wait for a true expert here.

The rule ID is not available in file based rules. Instead there you have fileName. If you have more than one rule in that file I guess you have to guess based on context or go the the rule registry to find the currently running rule and get the ID that way.

I don’t use file based rules but the code in OHRT that Florian contributed shows one way to get itr. I don’t know if it’s the only way.

const createTimer = (when, func, name, key) => {
  const timeout = time.toZDT(when);
  if (name === null || name === undefined) {
    if (global.ruleUID !== undefined) { // Use UI ruleUID and key if available
      name = 'ui.' + global.ruleUID + ((key !== undefined) ? '.' + key : '');
    } else if (global['javax.script.filename'] !== undefined) { // Use filename and key if available
      name = 'file.' + global['javax.script.filename'].replace(/^.*[\\/]/, '') + ((key !== undefined) ? '.' + key : '');
    }
  }
  return actions.ScriptExecution.createTimer(name, timeout, func);
};