Read the statement by Michael Teeuw here.
Module help for variable
-
Hi,
I’ve created a small module to turn my monitor off when my nest realizes I’ve left the house. This uses a notification from mmm-nest-status. Since I’m a noob I don’t understand how to keep track of a variable between notifications. I’m guessing I need something in start: ? Here is the code:Module.register("controlMonitor", { defaults: {}, notificationReceived: function(notification, payload, sender) { if (notification === 'MMM_NEST_STATUS_UPDATE') { this.processData(payload); } }, processData: function(data) { var awayState = 'unknown'; // 'home', 'away' // check for away state if (data.devices && data.structures) { var sId = Object.keys(data.structures)[0]; var sObj = data.structures[sId]; awayState = sObj.away; } var oldAwayState = this.awayState; this.awayState = awayState; // if state has changed and new is away then shut monitor off if (!(this.oldAwayState === awayState) && (this.awayState === 'away')) { //this.sendNotification("REMOTE_ACTION", {action: 'MONITOROFF'}); console.log(this.name + "awayState " + awayState + "oldAwayState " + oldAwayState + " " + this.datetime4log()); } // if state has changed and new is home then turn monitor on if (!(this.oldAwayState === awayState) && (this.awayState === 'home')) { //this.sendNotification("REMOTE_ACTION", {action: 'MONITORON'}); console.log(this.name + "awayState " + awayState + "oldAwayState " + oldAwayState + " " + this.datetime4log()); } }, datetime4log: function() { var d = new Date(); return (d.getMonth()+1) + "/" + d.getDate() + "/" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); } });
What’s happing now is the home state is firing off every 2 minutes for every notification. I need a module level oldAwayState variable, so the when I get the update I can test against it to only fire off an update if there is a change.
Thanks
-
@rts58 said in Module help for variable:
Module.register("controlMonitor", { defaults: {}, oldAwayState: 'unknown', awayState: 'unknown', notificationReceived:
-
Thank you @sdetweil ! So that helps me to create the module level variable. Sorry for dumb question, but how to I read/write it from within my process data function? I thought using
this.oldAwayState
would be the module level. -
@rts58 this should work w your code as is
-
That’s what I thought. But it still fires off every two minutes, there must be an error in the logic.
-
Is there something wrong with this:
if (!(this.oldAwayState === awayState) && (this.awayState === 'home'))
maybe they are both not strings or something?
-
@rts58 you can use the dev console and put stops in the code and look at the variables
-
@sdetweil thanks, I’ll give that a try. I’ve been writing the variables to the consol log but it’s not quite as helpful.
-
@rts58 the dev light is only for the module just file ( where getDom() is located)
Debugging the node helper or included scripts needs the console.log. sorry for confusion
-
@sdetweil Thanks, I got it. I needed to get rid of “this”. So the correct test was:
if (!(oldAwayState === awayState) && (awayState === 'home'))