MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Module help for variable

    Scheduled Pinned Locked Moved Solved Troubleshooting
    10 Posts 2 Posters 2.3k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R Offline
      rts58
      last edited by

      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

      S 1 Reply Last reply Reply Quote 0
      • S Offline
        sdetweil @rts58
        last edited by sdetweil

        @rts58 said in Module help for variable:

        Module.register("controlMonitor", {
        	defaults: {},
                oldAwayState: 'unknown',
                awayState: 'unknown',
        	notificationReceived:
        

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        1 Reply Last reply Reply Quote 1
        • R Offline
          rts58
          last edited by

          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.

          S 1 Reply Last reply Reply Quote 0
          • S Offline
            sdetweil @rts58
            last edited by

            @rts58 this should work w your code as is

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            1 Reply Last reply Reply Quote 0
            • R Offline
              rts58
              last edited by

              That’s what I thought. But it still fires off every two minutes, there must be an error in the logic.

              1 Reply Last reply Reply Quote 0
              • R Offline
                rts58
                last edited by

                Is there something wrong with this:

                if (!(this.oldAwayState === awayState) && (this.awayState === 'home')) 
                

                maybe they are both not strings or something?

                S 1 Reply Last reply Reply Quote 0
                • S Offline
                  sdetweil @rts58
                  last edited by

                  @rts58 you can use the dev console and put stops in the code and look at the variables

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  R 2 Replies Last reply Reply Quote 1
                  • R Offline
                    rts58 @sdetweil
                    last edited by

                    @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.

                    S 1 Reply Last reply Reply Quote 0
                    • S Offline
                      sdetweil @rts58
                      last edited by

                      @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

                      Sam

                      How to add modules

                      learning how to use browser developers window for css changes

                      1 Reply Last reply Reply Quote 0
                      • R Offline
                        rts58 @sdetweil
                        last edited by

                        @sdetweil Thanks, I got it. I needed to get rid of “this”. So the correct test was:

                        if (!(oldAwayState === awayState) && (awayState === 'home')) 
                        
                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Enjoying MagicMirror? Please consider a donation!
                        MagicMirror created by Michael Teeuw.
                        Forum managed by Sam, technical setup by Karsten.
                        This forum is using NodeBB as its core | Contributors
                        Contact | Privacy Policy