OH3 Delete rule using Javascript

I’ve created simple rules using “SimpleRule()” and added it to the Registry using “UID=automationManager.addRule(sRule)” from within a Javascript rule, this works well and does what I want. However, I also need to delete rules I have created. Quite reasonably they cannot be deleted via the UI, not that I want to anyway. Unfortunately, however “rules.remove(UID)” does not work either as it fails with a Class exception:

2021-01-28 13:13:37.129 [WARN ] [e.automation.internal.RuleEngineImpl] - Fail to execute action: script

java.lang.ClassCastException: class org.openhab.core.automation.internal.RuleImpl cannot be cast to class java.lang.String (org.openhab.core.automation.internal.RuleImpl is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @187e57b; java.lang.String is in module java.base of loader 'bootstrap')

	at org.openhab.core.automation.ManagedRuleProvider.keyToString(ManagedRuleProvider.java:1) ~[?:?]

	at org.openhab.core.common.registry.AbstractManagedProvider.remove(AbstractManagedProvider.java:90) ~[?:?]

I’ve also noticed that the UID returned by “automationManager.addRule()” is different to the UID shown in the User Interface. E.g. The returned UID is
“org.openhab.core.automation.internal.RuleImpl@55d5266a”
whereas the Unique ID shown in the UI is image the two don’t seem to be related.

The actual code I am using is:

var logger          = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var FrameworkUtil = Java.type("org.osgi.framework.FrameworkUtil");
var _bundle = FrameworkUtil.getBundle(scriptExtension.class);
var bundle_context = _bundle.getBundleContext()
var MetadataRegistry_Ref = bundle_context.getServiceReference("org.openhab.core.items.MetadataRegistry");
var MetadataRegistry = bundle_context.getService(MetadataRegistry_Ref);
var Metadata = Java.type("org.openhab.core.items.Metadata");
var MetadataKey = Java.type("org.openhab.core.items.MetadataKey");
var RuleRegistry = Java.type("org.openhab.core.automation.Rule");
var ruleEngine = Java.type("org.openhab.core.automation.RuleManager"); 

scriptExtension.importPreset("RuleSupport");
scriptExtension.importPreset("RuleSimple");

var ArrayOfKeys = {};
var ListOfRules;
var ReturnValue;
var TimesCalled = 1;
var UID;

//----------------------------------------------------------------------------------------------------------------

outputCurrentRules = function()
{
var i =0; 
logger.info("\n\n\n Rules found on Call:  " +TimesCalled);
var ReturnValue=rules.getAll().stream().forEach(function(keys)
                                            {
                                              ArrayOfKeys[i] = (keys);
                                              logger.info("Key Value [" +i +"] is: " +ArrayOfKeys[i] +" and Type is: " +typeof ArrayOfKeys[i]);
                                              i++;
                                            });
logger.info("\n\nReturn value of rules.getAll() on call[" +TimesCalled +"] is: " +ReturnValue );

TimesCalled = TimesCalled + 1;
}

//----------------------------------------------------------------------------------------------------------------

var sRule = new SimpleRule() {
    execute: function( module, input) {
        logger.info("This is a 'hello world!' from a Javascript rule.");
    }
};

sRule.setTriggers([
    TriggerBuilder.create()
        .withId("aTimerTrigger")
        .withTypeUID("timer.GenericCronTrigger")
        .withConfiguration(
            new Configuration({
                "cronExpression": "0 * * * * ?"
            })).build()
    ]);
sRule.name = "JavaScript Hello World example (raw API)";
sRule.description = "This is an example Hello World rule using the raw API";

logger.info("\n\nCalling outputCurrentRules() before creating new rule.  UID type is: " +typeof UID +"  value: " +UID +"\n\n");
outputCurrentRules();

UID=automationManager.addRule(sRule);

logger.info("\n\nCalling outputCurrentRules() after creating new rule.  UID type is: " +typeof UID +"  value: " +UID +"\n\n");
outputCurrentRules();

var status = rules.remove(UID);

logger.info("\n\nCalling outputCurrentRules() after attempting to remove new rule.  Status is: " +status +"  UID type is: " +typeof UID +"  value: " +UID +"\n\n");
outputCurrentRules();

var ListOfRules = rules.getAll();
logger.info("\n\n ListOfRules is Type: " +typeof ListOfRules +" and has a value of: " +ListOfRules);
logger.info("ListOfRules has Keys: " +Object.keys(ListOfRules));

and the logger output is:

2021-01-28 13:13:03.492 [INFO ] [openhab.event.RuleUpdatedEvent      ] - Rule '5be9c178bc' has been updated.

==> /var/log/openhab/openhab.log <==

2021-01-28 13:13:34.367 [INFO ] [org.openhab.rule.5be9c178bc         ] - 

Calling outputCurrentRules() before creating new rule.  UID type is: undefined  value: undefined

2021-01-28 13:13:34.520 [INFO ] [org.openhab.rule.5be9c178bc         ] - 

 Rules found on Call:  1

2021-01-28 13:13:34.753 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [0] is: org.openhab.core.automation.internal.RuleImpl@69d06cd4 and Type is: object

2021-01-28 13:13:34.778 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [1] is: org.openhab.core.automation.internal.RuleImpl@9fdff718 and Type is: object

2021-01-28 13:13:34.797 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [2] is: org.openhab.core.automation.internal.RuleImpl@681c78b4 and Type is: object

2021-01-28 13:13:34.815 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [3] is: org.openhab.core.automation.internal.RuleImpl@5678fbf1 and Type is: object

2021-01-28 13:13:34.833 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [4] is: org.openhab.core.automation.internal.RuleImpl@bf73ef19 and Type is: object

2021-01-28 13:13:34.851 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [5] is: org.openhab.core.automation.internal.RuleImpl@388cf63 and Type is: object

2021-01-28 13:13:34.869 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [6] is: org.openhab.core.automation.internal.RuleImpl@c43cf370 and Type is: object

2021-01-28 13:13:34.887 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [7] is: org.openhab.core.automation.internal.RuleImpl@23397516 and Type is: object

2021-01-28 13:13:34.906 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [8] is: org.openhab.core.automation.internal.RuleImpl@b5761806 and Type is: object

2021-01-28 13:13:34.926 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [9] is: org.openhab.core.automation.internal.RuleImpl@ca8efeff and Type is: object

2021-01-28 13:13:34.956 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [10] is: org.openhab.core.automation.internal.RuleImpl@13917c22 and Type is: object

2021-01-28 13:13:34.974 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [11] is: org.openhab.core.automation.internal.RuleImpl@a8ba356d and Type is: object

2021-01-28 13:13:34.993 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [12] is: org.openhab.core.automation.internal.RuleImpl@a0b8dfcf and Type is: object

2021-01-28 13:13:35.011 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [13] is: org.openhab.core.automation.internal.RuleImpl@e0f71c8c and Type is: object

2021-01-28 13:13:35.030 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [14] is: org.openhab.core.automation.internal.RuleImpl@674571fe and Type is: object

2021-01-28 13:13:35.047 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [15] is: org.openhab.core.automation.internal.RuleImpl@a17fa663 and Type is: object

2021-01-28 13:13:35.074 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [16] is: org.openhab.core.automation.internal.RuleImpl@57fb2b07 and Type is: object

2021-01-28 13:13:35.092 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [17] is: org.openhab.core.automation.internal.RuleImpl@7f6c6d90 and Type is: object

2021-01-28 13:13:35.112 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [18] is: org.openhab.core.automation.internal.RuleImpl@e042ab8 and Type is: object

2021-01-28 13:13:35.130 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [19] is: org.openhab.core.automation.internal.RuleImpl@2b1cfda0 and Type is: object

2021-01-28 13:13:35.148 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [20] is: org.openhab.core.automation.internal.RuleImpl@91d1496d and Type is: object

2021-01-28 13:13:35.166 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [21] is: org.openhab.core.automation.internal.RuleImpl@55d5266a and Type is: object

2021-01-28 13:13:35.185 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [22] is: org.openhab.core.automation.internal.RuleImpl@a2063019 and Type is: object

2021-01-28 13:13:35.321 [INFO ] [org.openhab.rule.5be9c178bc         ] - 

Return value of rules.getAll() on call[1] is: undefined

2021-01-28 13:13:36.094 [INFO ] [org.openhab.rule.5be9c178bc         ] - 

Calling outputCurrentRules() after creating new rule.  UID type is: object  value: org.openhab.core.automation.internal.RuleImpl@8e4e86c6

2021-01-28 13:13:36.218 [INFO ] [org.openhab.rule.5be9c178bc         ] - 

 Rules found on Call:  2

2021-01-28 13:13:36.324 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [0] is: org.openhab.core.automation.internal.RuleImpl@69d06cd4 and Type is: object

2021-01-28 13:13:36.352 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [1] is: org.openhab.core.automation.internal.RuleImpl@9fdff718 and Type is: object

2021-01-28 13:13:36.371 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [2] is: org.openhab.core.automation.internal.RuleImpl@8e4e86c6 and Type is: object

2021-01-28 13:13:36.389 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [3] is: org.openhab.core.automation.internal.RuleImpl@5678fbf1 and Type is: object

2021-01-28 13:13:36.408 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [4] is: org.openhab.core.automation.internal.RuleImpl@c43cf370 and Type is: object

2021-01-28 13:13:36.425 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [5] is: org.openhab.core.automation.internal.RuleImpl@a8ba356d and Type is: object

2021-01-28 13:13:36.444 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [6] is: org.openhab.core.automation.internal.RuleImpl@a17fa663 and Type is: object

2021-01-28 13:13:36.462 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [7] is: org.openhab.core.automation.internal.RuleImpl@a2063019 and Type is: object

2021-01-28 13:13:36.481 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [8] is: org.openhab.core.automation.internal.RuleImpl@681c78b4 and Type is: object

2021-01-28 13:13:36.500 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [9] is: org.openhab.core.automation.internal.RuleImpl@bf73ef19 and Type is: object

2021-01-28 13:13:36.519 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [10] is: org.openhab.core.automation.internal.RuleImpl@388cf63 and Type is: object

2021-01-28 13:13:36.536 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [11] is: org.openhab.core.automation.internal.RuleImpl@23397516 and Type is: object

2021-01-28 13:13:36.555 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [12] is: org.openhab.core.automation.internal.RuleImpl@b5761806 and Type is: object

2021-01-28 13:13:36.579 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [13] is: org.openhab.core.automation.internal.RuleImpl@ca8efeff and Type is: object

2021-01-28 13:13:36.597 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [14] is: org.openhab.core.automation.internal.RuleImpl@13917c22 and Type is: object

2021-01-28 13:13:36.616 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [15] is: org.openhab.core.automation.internal.RuleImpl@a0b8dfcf and Type is: object

2021-01-28 13:13:36.634 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [16] is: org.openhab.core.automation.internal.RuleImpl@e0f71c8c and Type is: object

2021-01-28 13:13:36.652 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [17] is: org.openhab.core.automation.internal.RuleImpl@674571fe and Type is: object

2021-01-28 13:13:36.670 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [18] is: org.openhab.core.automation.internal.RuleImpl@57fb2b07 and Type is: object

2021-01-28 13:13:36.688 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [19] is: org.openhab.core.automation.internal.RuleImpl@7f6c6d90 and Type is: object

2021-01-28 13:13:36.706 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [20] is: org.openhab.core.automation.internal.RuleImpl@e042ab8 and Type is: object

2021-01-28 13:13:36.724 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [21] is: org.openhab.core.automation.internal.RuleImpl@2b1cfda0 and Type is: object

2021-01-28 13:13:36.742 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [22] is: org.openhab.core.automation.internal.RuleImpl@91d1496d and Type is: object

2021-01-28 13:13:36.760 [INFO ] [org.openhab.rule.5be9c178bc         ] - Key Value [23] is: org.openhab.core.automation.internal.RuleImpl@55d5266a and Type is: object

2021-01-28 13:13:36.791 [INFO ] [org.openhab.rule.5be9c178bc         ] - 

Return value of rules.getAll() on call[2] is: undefined

2021-01-28 13:13:37.129 [WARN ] [e.automation.internal.RuleEngineImpl] - Fail to execute action: script

java.lang.ClassCastException: class org.openhab.core.automation.internal.RuleImpl cannot be cast to class java.lang.String (org.openhab.core.automation.internal.RuleImpl is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @187e57b; java.lang.String is in module java.base of loader 'bootstrap')

	at org.openhab.core.automation.ManagedRuleProvider.keyToString(ManagedRuleProvider.java:1) ~[?:?]

	at org.openhab.core.common.registry.AbstractManagedProvider.remove(AbstractManagedProvider.java:90) ~[?:?]

	at org.openhab.core.common.registry.AbstractRegistry.remove(AbstractRegistry.java:359) ~[?:?]

	at jdk.nashorn.internal.scripts.Script$Recompilation$28716$\^eval\_$cu1$restOf.:program(<eval>:141) ~[?:?]

	at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:655) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:513) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:527) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:456) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:413) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:409) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:162) ~[jdk.scripting.nashorn:?]

	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264) ~[java.scripting:?]

	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:62) ~[?:?]

	at java.util.Optional.ifPresent(Optional.java:183) ~[?:?]

	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:59) ~[?:?]

	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1179) [bundleFile:?]

	at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1031) [bundleFile:?]

	at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1047) [bundleFile:?]

	at org.openhab.core.automation.rest.internal.RuleResource.runNow(RuleResource.java:314) [bundleFile:?]

	at jdk.internal.reflect.GeneratedMethodAccessor73.invoke(Unknown Source) ~[?:?]

	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]

	at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]

	at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:179) [bundleFile:1.0.9]

	at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96) [bundleFile:1.0.9]

	at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:201) [bundleFile:1.0.9]

	at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:104) [bundleFile:1.0.9]

	at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59) [bundleFile:1.0.9]

	at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96) [bundleFile:1.0.9]

	at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) [bundleFile:1.0.9]

	at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121) [bundleFile:1.0.9]

	at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:267) [bundleFile:1.0.9]

	at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234) [bundleFile:1.0.9]

	at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208) [bundleFile:1.0.9]

	at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160) [bundleFile:1.0.9]

	at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:216) [bundleFile:1.0.9]

	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:301) [bundleFile:1.0.9]

	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:220) [bundleFile:1.0.9]

	at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) [bundleFile:3.1.0]

	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:276) [bundleFile:1.0.9]

	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:852) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:544) [bundleFile:9.4.20.v20190813]

	at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:71) [bundleFile:?]

	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:536) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1581) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1307) [bundleFile:9.4.20.v20190813]

	at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:293) [bundleFile:?]

	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:482) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1549) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1204) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [bundleFile:9.4.20.v20190813]

	at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:80) [bundleFile:?]

	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.Server.handle(Server.java:494) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:374) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:268) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:117) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:336) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:313) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:171) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:129) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:367) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:782) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:918) [bundleFile:9.4.20.v20190813]

	at java.lang.Thread.run(Thread.java:834) [?:?]

2021-01-28 13:14:00.692 [INFO ] [org.openhab.rule.5be9c178bc         ] - This is a 'hello world!' from a Javascript rule.

If I just get a list of rules using the following code:

var ListOfRules = rules.getAll();
logger.info("\n\n ListOfRules is Type: " +typeof ListOfRules +" and has a value of: " +ListOfRules);
logger.info("ListOfRules has Keys: " +Object.keys(ListOfRules));

then although typeof reports that ListOfRules is an Object if I try and get the Keys using Object.keys() then I get a type error:

2021-01-28 13:31:33.151 [INFO ] [org.openhab.rule.5be9c178bc         ] - 

 ListOfRules is Type: object and has a value of: [org.openhab.core.automation.internal.RuleImpl@69d06cd4, org.openhab.core.automation.internal.RuleImpl@9fdff718, org.openhab.core.automation.internal.RuleImpl@8e4e86c6, org.openhab.core.automation.internal.RuleImpl@5678fbf1, org.openhab.core.automation.internal.RuleImpl@c43cf370, org.openhab.core.automation.internal.RuleImpl@a8ba356d, org.openhab.core.automation.internal.RuleImpl@a17fa663, org.openhab.core.automation.internal.RuleImpl@a2063019, org.openhab.core.automation.internal.RuleImpl@681c78b4, org.openhab.core.automation.internal.RuleImpl@bf73ef19, org.openhab.core.automation.internal.RuleImpl@388cf63, org.openhab.core.automation.internal.RuleImpl@23397516, org.openhab.core.automation.internal.RuleImpl@b5761806, org.openhab.core.automation.internal.RuleImpl@ca8efeff, org.openhab.core.automation.internal.RuleImpl@13917c22, org.openhab.core.automation.internal.RuleImpl@a0b8dfcf, org.openhab.core.automation.internal.RuleImpl@e0f71c8c, org.openhab.core.automation.internal.RuleImpl@674571fe, org.openhab.core.automation.internal.RuleImpl@57fb2b07, org.openhab.core.automation.internal.RuleImpl@7f6c6d90, org.openhab.core.automation.internal.RuleImpl@e042ab8, org.openhab.core.automation.internal.RuleImpl@2b1cfda0, org.openhab.core.automation.internal.RuleImpl@91d1496d, org.openhab.core.automation.internal.RuleImpl@55d5266a]

2021-01-28 13:31:33.414 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '5be9c178bc' failed: TypeError: [org.openhab.core.automation.internal.RuleImpl@69d06cd4, org.openhab.core.automation.internal.RuleImpl@9fdff718, org.openhab.core.automation.internal.RuleImpl@8e4e86c6, org.openhab.core.automation.internal.RuleImpl@5678fbf1, org.openhab.core.automation.internal.RuleImpl@c43cf370, org.openhab.core.automation.internal.RuleImpl@a8ba356d, org.openhab.core.automation.internal.RuleImpl@a17fa663, org.openhab.core.automation.internal.RuleImpl@a2063019, org.openhab.core.automation.internal.RuleImpl@681c78b4, org.openhab.core.automation.internal.RuleImpl@bf73ef19, org.openhab.core.automation.internal.RuleImpl@388cf63, org.openhab.core.automation.internal.RuleImpl@23397516, org.openhab.core.automation.internal.RuleImpl@b5761806, org.openhab.core.automation.internal.RuleImpl@ca8efeff, org.openhab.core.automation.internal.RuleImpl@13917c22, org.openhab.core.automation.internal.RuleImpl@a0b8dfcf, org.openhab.core.automation.internal.RuleImpl@e0f71c8c, org.openhab.core.automation.internal.RuleImpl@674571fe, org.openhab.core.automation.internal.RuleImpl@57fb2b07, org.openhab.core.automation.internal.RuleImpl@7f6c6d90, org.openhab.core.automation.internal.RuleImpl@e042ab8, org.openhab.core.automation.internal.RuleImpl@2b1cfda0, org.openhab.core.automation.internal.RuleImpl@91d1496d, org.openhab.core.automation.internal.RuleImpl@55d5266a] is not an Object in <eval> at line number 150

I’m thoroughly confused and am not sure what to try next. But while I have failed to find a solution myself or by searching the forum I am sure someone more experienced than me must have come across this problem and solved it. So can someone please shine some light on what I need to do to get the ID of a rule and then delete the rule.

Many thanks in advance.

UID isn’t a String, its an Object. According to the log, it’s a RuleImpl (openHAB Core 3.1.0-SNAPSHOT API) Object. I suspect calling “getName()” on UID will return the String you are looking for.

The error is telling you that the call to delete the Rule is failing because it cannot cast the RuleImpl Object to a String, which is reasonable because it’s not a String.

Thanks for the response I agree UID is an Object and from what I’ve managed to gather it is what “rules.remove()” requires in order to delete a rule. Calling “getName()” does sucessfuly return the name of the rule, which may prove useful in the future, but unfortunately “rules.remove()” needs the UID of the rule one wishes to delete. If I cast the UID to a string using “String()” I get what looks llike the required ID and “rules.remove()” does not throw an exception. It does however return a status of null indicating it could not find the rule, and indeed the list of rules does not change.

Also while UID is an object if one tries to get its key using Object.keys(UID) it thows a “type error” exception.

What are you gathering this from? The error is pretty clear. It’s expecting a String and it received a RuleImpl.

The naive belief that because “addRule()” & “rules.getAll()” return the UID as type Object then rules.remove would require the UID in the same format. Yes I was being very silly.

I’ve now progressed slightly. In so far as by replacing “rules.remove()” with “ruleRegistry.remove()” the following code will create a rule and remove it immediately after it has been created, but not a rule that was created by another instance of my test script.

UID=automationManager.addRule(sRule);               // Returns UID different to that shown in UI
var trythisUID = UID.getUID();                                   // Returns UID shown in UI as a string
var status = ruleRegistry.remove(trythisUID);

“UID.getUID()” returns the UID shown in the User Interface as a string.

As stated above my test script can only remove a rule created in the same instance of the Script which created it. That is I cannot remove rules created in previous incarnations of the Script. But if I paste the same code into a rule, using the “Run Script” option that rule is able to remove a rules that were created in different instances of the rule and even those rules created by my test script. So I am now able to clear all the test rules and do what I need in my automation.

Once again @rlkoshak many thanks for your invaluable help.

Forget that, on trying again this morning, it turns out that I am unable to delete existing rules previously created using a script/rule.

Any suggestions as to what I need to do to delete existing rules?

Are you trying to refuse variables between runs? By this I mean ate you going to use the value of a variable from a previous run in the current run if the rule? If so you need to save the variable to this and avoid initializing it if it already has a value.

I do reuse the UID provided by addRule() to remove it when I remove it immediately after creating it. When I am attempting to remove a rule which was created by a previous run of the script I am extracting the UID of the rule from the rules registry and then using the extracted UID to attempt to remove the rule.

Further investigation has shown that the rules I create using Javascript disappear on restarting OH. So the solution to removing orfan rules is to do a restart.

I’ve also found that if a rule deletes itself it is then removed so I’ve “solved” the problem of deleting a rule by implementing a “kill trigger”. That is, when triggered by the “kill trigger” the rule uses the unique name it was given at time of creation to look up its UID and issue a ruleRegistry.remove

You can remove rules at runtime using the method you are trying to, I do it all the time. addRule function doesn’t return a UID, it returns the rule object (the @xxxxx is a memory address or something similar).

As for them not being saved, it’s because the rule manager is not a Managed Provider, it is just a Provider. A Managed Provider has provisions to save the objects it provides to disk and then load them when OH starts. I’ve never looked into the rule engine to see if a Managed Provider is/can be used, the only places I’m using this are in services that I’ve written that reconfigure their rules when group membership changes so they need to generate their rules at startup anyway.