From 7b8229a04743bace3d7c034aa66c652ced9e763e Mon Sep 17 00:00:00 2001 From: Christian Weimann Date: Sat, 19 Aug 2023 05:51:11 +0200 Subject: [PATCH] Delete watchRule if no more watchObjects are present --- utils/watch.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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) },