Read the statement by Michael Teeuw here.
Connecting Modules to Each Other
-
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!
-
@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.
-
@tonkxy
And there be already similar modules. Look inside and study how they did. -
@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"; } });
-
@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 filethen 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) -
@tonkxy
Hmmmm… I cannot understand thesetCurrentWeatherType
.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 emitCURRENTWEATHER_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)
-
@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
-
@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
-
@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!
-
@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.