From 6432717306cf72b2c1be5f8a5469ecd4b4eaa356 Mon Sep 17 00:00:00 2001 From: Frank Blanning Date: Sun, 2 Aug 2020 13:11:48 +0300 Subject: [PATCH] Allow stateful to RUN command and pass state TO command --- index.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d04ab65..27df586 100755 --- a/index.js +++ b/index.js @@ -67,7 +67,20 @@ CmdTrigger.prototype._setOn = function(on, callback) { } if (this.stateful) { - this.storage.setItemSync(this.name, on); + var cachedState = this.storage.getItemSync(this.name); + if (on && ((cachedState === undefined) || (cachedState === false))) { + exec(this.command + " on"); + this.log("Command executed: '" + this.command + " on'"); + this.storage.setItemSync(this.name, on); + } + else if(!on && (cachedState === true)) { + exec(this.command + " off"); + this.log("Command executed: '" + this.command +" off'"); + this.storage.setItemSync(this.name, on); + } + else { + this.log("State was already '" + on + "'"); + } } callback();