diff --git a/utils/watch.js b/utils/watch.js index ede8a5d..9435dab 100644 --- a/utils/watch.js +++ b/utils/watch.js @@ -71,6 +71,12 @@ class Watch { } delete this.#watchObjects[watchUUID]; + + // Delete watchRule if no more watchObjects are present + if (Object.keys(this.#watchObjects).length == 0) { + console.debug(`Remove openHAB watch rule for item ${this.#watchItemName}`); + rules.removeRule(this.watchRuleID); + } } deleteAll() { @@ -135,7 +141,9 @@ class Watch { #createWatchRule() { // Create openHAB rule console.log(`Create openHAB watch rule for item ${this.#watchItemName}`); - let ruleID = rules.JSRule({ + this.watchRuleID = String(utils.randomUUID()); + rules.JSRule({ + id: this.watchRuleID, name: 'Watch rule for ' + this.#watchItemName, triggers: [triggers.ItemStateUpdateTrigger(this.#watchItemName)], execute: (event) => { this.#processItemEvent(event) },