Hmmm. There are several things you can try.
First, I vaguely recall that there might be a global
variable. It might be in there. I seem to have that in one of my library functions but there is also indication that only works for UI rules.
global.ruleUID
But it also indicates that you can get at the filename for file based rules.
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 : '');
}
Maybe the filename is enough? This kind of smells like an XY Problem so I canāt be sure.
Secondly, it might work if we add it as an argument to the execute function.
execute: (event, ruleUID) => {
or
execute: (event, global.ruleUID) => {
Third is to import @runtime
. It might be available there, though I donāt see it documented as such.
execute: (event) => {
const runtime = require('@runtime');
console.log(runtime.ruleUID);
}
Fourth, the ruleUID is part of the default logger name for the rule so we could pull it from that. But here there is further indication that for file based rules you only get the filename, not the ruleUID.
The default logger name consists of the prefix org.openhab.automation.script
and the scriptās individual part .file.filename
or .ui.ruleUID
. This logger name can be changed by assigning a new string to the loggerName
property of the console:
But you can get the filename
or ruleUID
with a simple split:
var ruleID = console.loggerName.split('.').pop();
We also might be going about this the long way.
Youāve created the rule in a file. Hopefully youāve given it a meaningful ID thatās already hard coded there. Why not hard code it again in the execute function? Do you really need to pull it dynamically? You already know it. If you are just trying to add them to the logs, set the loggerName and just hard code it there.