Hi again everyone
I’ve been struggling with removing rules via Jython, and was wondering if anyone could point me in the right direction. I’m calling rules.remove(<rule UID>)
but getting a None
result back (I believe this should be returning the rule itself) and the rule still appears to be present. It also continues to show in Paper UI and comes up in karaf as well.
import inspect
import org.eclipse.smarthome.config.core.Configuration
from openhab.rules import rule
from openhab.triggers import when
from openhab.log import logging
from datetime import datetime, timedelta
LOG_BASE = "org.eclipse.smarthome.model.script.JS223.ufh"
@rule("UFH Cron Manager")
@when ("Member of gUFHScheduleTime received update")
def ufhCronManager(event):
log = logging.getLogger(LOG_BASE + "." + inspect.currentframe().f_code.co_name)
log.info("[DEBUG] Rule triggered. event = {}".format(event))
rule_name = event.itemName + " Cron Rule"
filtered_rules = filter(lambda rule: rule.name == rule_name, rules.getAll())
log.info("[Log 1] Filtered list of rules: {}, len = {}".format(filtered_rules, len(filtered_rules)))
cron_rule = filtered_rules[0] if len(filtered_rules) >0 else None
if cron_rule:
log.info("[Log 2] Rule for item '{}' exists with UID {}. Removing.......".format(event.itemName, cron_rule.UID))
log.info("-------> Removal result: {}".format(rules.remove(cron_rule.UID)))
filtered_rules = filter(lambda rule: rule.name == rule_name, rules.getAll())
log.info("[Log 3] Rules collection post removal of cron_rule: {}, len: {}".format(filtered_rules, len(filtered_rules)))
cron_rule = filtered_rules[0] if len(filtered_rules) >0 else None
if cron_rule:
log.info("[Log 4] Double check shows rule with UID {} STILL EXISTS after deletion (len = {}). Abandoning....".format(cron_rule.UID, len(filtered_rules)))
else:
log.info("[Log 5] Creating new rule...")
@rule(rule_name)
@when("Time cron 0 " + str(datetime.now().minute + 2) + " " + str(datetime.now().hour) + " * * ?")
def newcronrule(event):
log = logging.getLogger(LOG_BASE + ".newCronRule")
log.info("[Log 6] CronRule execute called: {}, type {} .................................................".format(event, type(event)))
The log results show:
2018-11-28 22:33:54.325 [INFO ] [odel.script.JS223.ufh.ufhCronManager] - [DEBUG] Rule triggered. event = UFH_MasterEnSuite_SchedOnTime2 updated to 1820
2018-11-28 22:33:54.329 [INFO ] [odel.script.JS223.ufh.ufhCronManager] - [Log 1] Filtered list of rules: [], len = 0
2018-11-28 22:33:54.332 [INFO ] [odel.script.JS223.ufh.ufhCronManager] - [Log 3] Rules collection post removal of cron_rule: [], len: 0
2018-11-28 22:33:54.334 [INFO ] [odel.script.JS223.ufh.ufhCronManager] - [Log 5] Creating new rule...
2018-11-28 22:33:54.347 [INFO ] [odel.script.JS223.ufh.ufhCronManager] - [DEBUG] Rule triggered. event = UFH_MasterEnSuite_SchedOnTime2 updated to 1810
2018-11-28 22:33:54.349 [INFO ] [odel.script.JS223.ufh.ufhCronManager] - [Log 1] Filtered list of rules: [org.eclipse.smarthome.automation.core.internal.RuleImpl@45d591ef], len = 1
2018-11-28 22:33:54.351 [INFO ] [odel.script.JS223.ufh.ufhCronManager] - [Log 2] Rule for item 'UFH_MasterEnSuite_SchedOnTime2' exists with UID 4f6c9220-74a0-4885-afcf-52985f503019. Removing.......
2018-11-28 22:33:54.353 [INFO ] [odel.script.JS223.ufh.ufhCronManager] - -------> Removal result: None
2018-11-28 22:33:54.355 [INFO ] [odel.script.JS223.ufh.ufhCronManager] - [Log 3] Rules collection post removal of cron_rule: [org.eclipse.smarthome.automation.core.internal.RuleImpl@45d591ef], len: 1
2018-11-28 22:33:54.358 [INFO ] [odel.script.JS223.ufh.ufhCronManager] - [Log 4] Double check shows rule with UID 4f6c9220-74a0-4885-afcf-52985f503019 STILL EXISTS after deletion (len = 1). Abandoning....
Interestingly, karaf also doesn’t seem to remove the rule, claiming that the id does not exist. I’ve tried using both the ‘ID’ value and ‘UID’ as listed by karaf’s listRules
but both come back with the same error:
openhab> smarthome:automation listRules
----------------------------------------------------------------------------------------------------
ID UID NAME STATUS
----------------------------------------------------------------------------------------------------
1 1a94476a-f3ce-4392-95e4-a257bafa4417 MasterEnsuite UFH Cron1 IDLE
2 29922cc3-63e2-44ea-b87f-417a68c40bb3 UFH_MasterEnSuite_SchedOnTime2 Cron Rule IDLE
3 66be4353-c651-46dd-851e-62d6420a0c7a MasterBedroom Xiamoi Switch Ch2 IDLE
4 732279c9-847b-45cb-80ab-ad1ca09649f9 Light_FF_MasterEnsuite_Spots changed IDLE
5 79de5b51-b6c0-43e2-b131-db7f3083e235 Office_MotionSensor_Last changed IDLE
6 7f231d45-1208-420e-a2ff-68fac77d201d MasterBedroom Xiamoi Switch Ch1 IDLE
7 80a50241-70e3-4b07-b9ad-2852eef83988 This is the name of a test rule IDLE
8 a54bba71-5b3f-47db-a5f9-222d8d15359b Motion Sensor Triggered IDLE
9 bcba078b-b81d-4ecc-bb96-564e8732647e UFH Cron Manager IDLE
----------------------------------------------------------------------------------------------------
openhab> smarthome:automation removeRule 29922cc3-63e2-44ea-b87f-417a68c40bb3
Rule with id '29922cc3-63e2-44ea-b87f-417a68c40bb3' does not exist.
Finally, I’ve also tried using the REST api with the same UID, but again the rule just will not delete…
Am I missing something obvious here?