MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord

    SOLVED Connecting Modules to Each Other

    Troubleshooting
    3
    16
    418
    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.
    • T
      tonkxy last edited by

      Hello!

      I am wanting to take the variables from a weather module and use that to decide a background. Do I need to rebuild current weather in my module, or can i import it essentially in order to use the variables / gather temps?

      Thank you for your time!

      M 2 Replies Last reply Reply Quote 0
      • M
        MMRIZE @tonkxy last edited by MMRIZE

        @tonkxy
        Usually some module might spit out some notifications with various data those want to be consumed.
        With luck you can gather all data you need from the notifications.

        Some modules might provide public method functions to interact with other modules.

        Some modules might provide WebAPI url endpoints to communicate with world.

        Well, even if there exists nothing to use, you can inject or reassign(overwrite) MM module methods for your needs, because MM module is not encapsulated by default.

        1 Reply Last reply Reply Quote 1
        • M
          MMRIZE @tonkxy last edited by

          @tonkxy
          And there be already similar modules. Look inside and study how they did.

          T 1 Reply Last reply Reply Quote 1
          • T
            tonkxy @MMRIZE last edited by tonkxy

            @mmrize I took some time to try and figure this one out… sadly a lot of weather API’s are falling out, so some references use darkskies and that is out. I did write this up and so far the only issue I am having is at setImage the type variable is not being filled with a value.

            When i run npm start dev and I have the log return the value of type it does say info undefined… so it seems im not pulling off currentweather correctly with openweather as my api

            defaults: {
                    height: "600px",   // your display's resolution in pixels. Enter in config.js
                    width: "1024px",    // your display's resolution in pixels. Enter in config.js
                    updateInterval: 60 * 60 * 100,
                },
            //set current weather from moduel
            currentWeatherType: "weather, currentweather",
            //scripts
            getScripts: function(){
            	return ["moment.js" , "moment-timezone.js"];
            },
               
            	
            start: function () {
            	  this.timer = null
            	  this.url = null
            	  this.setImage()
            },
            
            getDom: function () {
            	  console.log(">", this.url)
            	  var dom = document.createElement('div')
            	  dom.style.backgroundImage = `url(${this.url})`
            	  dom.style.height = '100vh' // do in css
            	  dom.style.width = '100vw'
            	  dom.style.backgroundSize = 'cover'
            	  return dom
            },
            //From data currentweather set weather type
            	setCurrentWeatherType: function(type){
            	this.currentWeatherType = type;
            },
            
            notificationReceived: function (notification, payload, sender) {
            	  if (notification === 'DOM_OBJECTS_CREATED') {
            	    this.job()
            	  }
            //weather dom
            	if(notification === "CURRENTWEATHER_TYPE"){
            		this.setCurrentWeatherType(payload.type);
            	}
            },
            
            
            job: function () {
            	  clearTimeout(this.timer)
                  	  this.updateDom()
                 	  this.setImage()
              	  this.timer = setTimeout(() => {
                	  this.job()
              	}, this.config.updateInterval)
            },
            
            
            setImage: function () {
              let rand = Math.floor(Math.random() * 4)+1; //randomn number to pick gif
            	var day = new Date();
            	var hour = day.getHours();
            	var type = this.currentWeatherTemperature;
            //if criteria for folders
            //	if(hour >= 22 || hour <= 4){
            //	folder = "Night";
            //	}else
            //	if(hour <= 10){
            //	folder = "Morning";
            //	}else
            //	if(hour <= 17){
            //	folder = "Afternoon";
            
            	if(type = "rain"){ //testing weather to see if it passes if condition
            		folder = "Afternoon";
            	}else folder = "Morning";
            
            	this.url = "modules/MMM-MainScreen/images/" + folder +"/" + rand +".gif";
            	}
            
            });
            
            S M 2 Replies Last reply Reply Quote 0
            • S
              sdetweil @tonkxy last edited by

              @tonkxy maybe the payload in the notification doesn’t have ‘type’

              u can debug the code in the developers window, ctrl-shift-i, select the source tab,
              and navigate to your module and select the js file

              then the source will appear on the right…

              to stop at an instruction

              click the line number in the left edge of the source display, it should turn blue

              then fresh the page (f5)

              and code will stop there, you can examine and change the variables/data and step thru the code (the dot with the curved arrow over it, is step one instruction at a time. )

              to resume, hit run (blue arrow) and if the debugger is still up, it will stop there again at the next time
              (if u stopped on a weather notification for example)

              Sam

              Create a working config
              How to add modules

              T 1 Reply Last reply Reply Quote 0
              • M
                MMRIZE @tonkxy last edited by

                @tonkxy
                Hmmmm… I cannot understand the setCurrentWeatherType.

                Anyway, I recommend you this module; https://github.com/BrianHepler/MMM-WeatherBackground
                You need to study how MM module works, at least to train how to use.

                You don’t need to pull weather info from an external weather API like Darksky. Already there are many modules showing weather info on MM screen and spitting that info out through notification.

                For example, default weather module also emit notifications of weather info periodically. All you need to do is catching that notification and consume them in your module.

                default weather module would emit CURRENTWEATHER_TYPE notification.
                You can catch that notification like this.

                
                notificationReceived: function (notification, payload, sender) {
                  if (notification === 'CURRENTWEATHER_TYPE') {
                     console.log(payload) // manipulate payload then extract data what you need.
                     // do your job
                  }
                }
                

                Anyway, Study the above module. That has almost all features you are trying. (except the image source)

                1 Reply Last reply Reply Quote 0
                • T
                  tonkxy @sdetweil last edited by

                  @sdetweil great callout! I did not know I could debug. I do get a type back. I did the break trick on where its in an if statement and the type is still undefined in setImage

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

                    @tonkxy well

                    setImage: function () {
                      let rand = Math.floor(Math.random() * 4)+1; //randomn number to pick gif
                    	var day = new Date();
                    	var hour = day.getHours();
                    	var type = this.currentWeatherTemperature;   // < ----
                    

                    notification

                    //From data currentweather set weather type
                    	setCurrentWeatherType: function(type){
                    	this.currentWeatherType = type;    // < ----
                    },
                    
                    notificationReceived: function (notification, payload, sender) {
                    	  if (notification === 'DOM_OBJECTS_CREATED') {
                    	    this.job()
                    	  }
                    //weather dom
                    	if(notification === "CURRENTWEATHER_TYPE"){
                    		this.setCurrentWeatherType(payload.type);
                    	}
                    },
                    

                    this.currentWeatherTemperature is NOT this.currentWeatherType

                    Sam

                    Create a working config
                    How to add modules

                    T 1 Reply Last reply Reply Quote 1
                    • T
                      tonkxy @sdetweil last edited by

                      @sdetweil that definitely was part of it.

                      I am able to successfully see the payload in notifications. Just working a way to retain it and be able to use in setImage. Thank you!

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

                        @tonkxy should be able to save the payload pointer and then use that day later. I do it all the time

                        this.weatherInfo = payload

                        for example

                        remember your module could get called at getDom() long before the other module sends the notification, so u need to check if it has arrived yet.

                        Sam

                        Create a working config
                        How to add modules

                        T 1 Reply Last reply Reply Quote 0
                        • 1
                        • 2
                        • 1 / 2
                        • First post
                          Last post
                        Enjoying MagicMirror? Please consider a donation!
                        MagicMirror created by Michael Teeuw.
                        Forum managed by Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
                        This forum is using NodeBB as its core | Contributors
                        Contact | Privacy Policy