• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
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.

Problem with Weather forecast

Scheduled Pinned Locked Moved Troubleshooting
47 Posts 14 Posters 35.9k Views 13 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.
  • S Offline
    Sp4M
    last edited by Sp4M Sep 25, 2017, 9:33 PM Sep 25, 2017, 9:06 PM

    @frank61BS
    This corrected version should works with the real maxNumberOfDays (=5 for example)

    /* global Module */
    
    /* Magic Mirror
     * Module: WeatherForecast
     *
     * By Michael Teeuw http://michaelteeuw.nl
     * MIT Licensed.
     */
    
    Module.register("weatherforecast",{
    
    	// Default module config.
    	defaults: {
    		location: false,
    		locationID: false,
    		appid: "",
    		units: config.units,
    		maxNumberOfDays: 5,
    		showRainAmount: false,
    		updateInterval: 10 * 60 * 1000, // every 10 minutes
    		animationSpeed: 1000,
    		timeFormat: config.timeFormat,
    		lang: config.language,
    		fade: true,
    		fadePoint: 0.25, // Start on 1/4th of the list.
    		colored: false,
    
    		initialLoadDelay: 2500, // 2.5 seconds delay. This delay is used to keep the OpenWeather API happy.
    		retryDelay: 2500,
    
    		apiVersion: "2.5",
    		apiBase: "http://api.openweathermap.org/data/",
    		forecastEndpoint: "forecast", //forecast/daily
    
    		appendLocationNameToHeader: true,
    		calendarClass: "calendar",
    
    		roundTemp: false,
    
    		iconTable: {
    			"01d": "wi-day-sunny",
    			"02d": "wi-day-cloudy",
    			"03d": "wi-cloudy",
    			"04d": "wi-cloudy-windy",
    			"09d": "wi-showers",
    			"10d": "wi-rain",
    			"11d": "wi-thunderstorm",
    			"13d": "wi-snow",
    			"50d": "wi-fog",
    			"01n": "wi-night-clear",
    			"02n": "wi-night-cloudy",
    			"03n": "wi-night-cloudy",
    			"04n": "wi-night-cloudy",
    			"09n": "wi-night-showers",
    			"10n": "wi-night-rain",
    			"11n": "wi-night-thunderstorm",
    			"13n": "wi-night-snow",
    			"50n": "wi-night-alt-cloudy-windy"
    		},
    
    		iconTableOrdered: [
    			"11d",
    			"09d",
    			"13d",
    			"10d",
    			"50d",
    			"04d",
    			"03d",
    			"02d",
    			"01d",
    			"11n",
    			"09n",
    			"13n",
    			"10n",
    			"50n",
    			"02n",
    			"03n",
    			"04n",
    			"01n"
    		],
    	},
    
    	// create a variable for the first upcoming calendaar event. Used if no location is specified.
    	firstEvent: false,
    
    	// create a variable to hold the location name based on the API result.
    	fetchedLocationName: "",
    
    	// Define required scripts.
    	getScripts: function() {
    		return ["moment.js"];
    	},
    
    	// Define required scripts.
    	getStyles: function() {
    		return ["weather-icons.css", "weatherforecast.css"];
    	},
    
    	// Define required translations.
    	getTranslations: function() {
    		// The translations for the default modules are defined in the core translation files.
    		// Therefor we can just return false. Otherwise we should have returned a dictionary.
    		// If you're trying to build yiur own module including translations, check out the documentation.
    		return false;
    	},
    
    	// Define start sequence.
    	start: function() {
    		Log.info("Starting module: " + this.name);
    
    		// Set locale.
    		moment.locale(config.language);
    
    		this.forecast = [];
    		this.loaded = false;
    		this.scheduleUpdate(this.config.initialLoadDelay);
    
    		this.updateTimer = null;
    
    	},
    
    	// Override dom generator.
    	getDom: function() {
    		var wrapper = document.createElement("div");
    
    		if (this.config.appid === "") {
    			wrapper.innerHTML = "Please set the correct openweather <i>appid</i> in the config for module: " + this.name + ".";
    			wrapper.className = "dimmed light small";
    			return wrapper;
    		}
    
    		if (!this.loaded) {
    			wrapper.innerHTML = this.translate("LOADING");
    			wrapper.className = "dimmed light small";
    			return wrapper;
    		}
    
    		var table = document.createElement("table");
    		table.className = "small";
    
    		for (var f in this.forecast) {
    			var forecast = this.forecast[f];
    
    			var row = document.createElement("tr");
    			if (this.config.colored) {
    				row.className = "colored";
    			}
    			table.appendChild(row);
    
    			var dayCell = document.createElement("td");
    			dayCell.className = "day";
    			dayCell.innerHTML = forecast.day;
    			row.appendChild(dayCell);
    
    			var iconCell = document.createElement("td");
    			iconCell.className = "bright weather-icon";
    			row.appendChild(iconCell);
    
    			var icon = document.createElement("span");
    			icon.className = "wi weathericon " + forecast.icon;
    			iconCell.appendChild(icon);
    
    			var maxTempCell = document.createElement("td");
    			maxTempCell.innerHTML = forecast.maxTemp;
    			maxTempCell.className = "align-right bright max-temp";
    			row.appendChild(maxTempCell);
    
    			var minTempCell = document.createElement("td");
    			minTempCell.innerHTML = forecast.minTemp;
    			minTempCell.className = "align-right min-temp";
    			row.appendChild(minTempCell);
    
    			if (this.config.showRainAmount) {
    				var rainCell = document.createElement("td");
    				if (isNaN(forecast.rain)) {
    					rainCell.innerHTML = "";
    				} else {
    					if(config.units !== "imperial") {
    						rainCell.innerHTML = forecast.rain + " mm";
    					} else {
    						rainCell.innerHTML = (parseFloat(forecast.rain) / 25.4).toFixed(2) + " in";
    					}
    				}
    				rainCell.className = "align-right bright rain";
    				row.appendChild(rainCell);
    			}
    
    			if (this.config.fade && this.config.fadePoint < 1) {
    				if (this.config.fadePoint < 0) {
    					this.config.fadePoint = 0;
    				}
    				var startingPoint = this.forecast.length * this.config.fadePoint;
    				var steps = this.forecast.length - startingPoint;
    				if (f >= startingPoint) {
    					var currentStep = f - startingPoint;
    					row.style.opacity = 1 - (1 / steps * currentStep);
    				}
    			}
    		}
    
    		return table;
    	},
    
    	// Override getHeader method.
    	getHeader: function() {
    		if (this.config.appendLocationNameToHeader) {
    			return this.data.header + " " + this.fetchedLocationName;
    		}
    
    		return this.data.header;
    	},
    
    	// Override notification handler.
    	notificationReceived: function(notification, payload, sender) {
    		if (notification === "DOM_OBJECTS_CREATED") {
    			if (this.config.appendLocationNameToHeader) {
    				this.hide(0, {lockString: this.identifier});
    			}
    		}
    		if (notification === "CALENDAR_EVENTS") {
    			var senderClasses = sender.data.classes.toLowerCase().split(" ");
    			if (senderClasses.indexOf(this.config.calendarClass.toLowerCase()) !== -1) {
    				this.firstEvent = false;
    
    				for (var e in payload) {
    					var event = payload[e];
    					if (event.location || event.geo) {
    						this.firstEvent = event;
    						//Log.log("First upcoming event with location: ", event);
    						break;
    					}
    				}
    			}
    		}
    	},
    
    	/* updateWeather(compliments)
    	 * Requests new data from openweather.org.
    	 * Calls processWeather on succesfull response.
    	 */
    	updateWeather: function() {
    		if (this.config.appid === "") {
    			Log.error("WeatherForecast: APPID not set!");
    			return;
    		}
    
    		var url = this.config.apiBase + this.config.apiVersion + "/" + this.config.forecastEndpoint + this.getParams();
    		var self = this;
    		var retry = true;
    
    		var weatherRequest = new XMLHttpRequest();
    		weatherRequest.open("GET", url, true);
    		weatherRequest.onreadystatechange = function() {
    			if (this.readyState === 4) {
    				if (this.status === 200) {
    					self.processWeather(JSON.parse(this.response));
    				} else if (this.status === 401) {
    					self.updateDom(self.config.animationSpeed);
    
    					Log.error(self.name + ": Incorrect APPID.");
    					retry = true;
    				} else {
    					Log.error(self.name + ": Could not load weather.");
    				}
    
    				if (retry) {
    					self.scheduleUpdate((self.loaded) ? -1 : self.config.retryDelay);
    				}
    			}
    		};
    		weatherRequest.send();
    	},
    
    	/* getParams(compliments)
    	 * Generates an url with api parameters based on the config.
    	 *
    	 * return String - URL params.
    	 */
    	getParams: function() {
    		var params = "?";
    		if(this.config.locationID) {
    			params += "id=" + this.config.locationID;
    		} else if(this.config.location) {
    			params += "q=" + this.config.location;
    		} else if (this.firstEvent && this.firstEvent.geo) {
    			params += "lat=" + this.firstEvent.geo.lat + "&lon=" + this.firstEvent.geo.lon
    		} else if (this.firstEvent && this.firstEvent.location) {
    			params += "q=" + this.firstEvent.location;
    		} else {
    			this.hide(this.config.animationSpeed, {lockString:this.identifier});
    			return;
    		}
    
    		params += "&units=" + this.config.units;
    		params += "&lang=" + this.config.lang;
    		/*
    		 * Submit a specific number of days to forecast, between 1 to 5 days.
    		 * The OpenWeatherMap API properly handles values outside of the 1 - 5 range and returns 5 days by default.
    		 * This is simply being pedantic and doing it ourselves.
    		 */
    		params += "&cnt=" + ((this.config.maxNumberOfDays < 1 || this.config.maxNumberOfDays > 5) ? 5 : this.config.maxNumberOfDays)*8;
    		params += "&APPID=" + this.config.appid;
    
    		return params;
    	},
    
    	/* processWeather(data)
    	 * Uses the received data to set the various values.
    	 *
    	 * argument data object - Weather information received form openweather.org.
    	 */
    	processWeather: function(data) {
    		var days_forecast = [];
    
    		this.fetchedLocationName = data.city.name + ", " + data.city.country;
    		this.forecast = [];
    
    		for (var i = 0, count = data.list.length; i < count; i++) {
    			let forecast = data.list[i];
    			let day = forecast.dt_txt.split(" ")[0];
    
    			if (!(day in days_forecast)) 
    				days_forecast[day] = {"dt":[], "icon":[], "temp":[]};
    
    			days_forecast[day].dt.push(forecast.dt);
    			days_forecast[day].icon.push(forecast.weather[0].icon);
    			days_forecast[day].temp.push(forecast.main.temp_min);
    			days_forecast[day].temp.push(forecast.main.temp_max);
    		}
    
    		for (let i in days_forecast) {
    			let min = days_forecast[i].temp.reduce((previous, current) => current = previous < current ? previous : current);
    			let max = days_forecast[i].temp.reduce((previous, current) => current = previous > current ? previous : current);
    			let icon = days_forecast[i].icon.reduce((previous, current) => current = this.config.iconTableOrdered.indexOf(previous) < this.config.iconTableOrdered.indexOf(current) ? previous : current);
    			
    			this.forecast.push({
    				day: moment(days_forecast[i].dt[0], "X").format("ddd"),
    				icon: this.config.iconTable[icon],
    				maxTemp: this.roundValue(min),
    				minTemp: this.roundValue(max)
    //				rain: this.roundValue(forecast.rain)
    			});
    		}
    
    		//Log.log(this.forecast);
    		this.show(this.config.animationSpeed, {lockString:this.identifier});
    		this.loaded = true;
    		this.updateDom(this.config.animationSpeed);
    	},
    
    	/* scheduleUpdate()
    	 * Schedule next update.
    	 *
    	 * argument delay number - Milliseconds before next update. If empty, this.config.updateInterval is used.
    	 */
    	scheduleUpdate: function(delay) {
    		var nextLoad = this.config.updateInterval;
    		if (typeof delay !== "undefined" && delay >= 0) {
    			nextLoad = delay;
    		}
    
    		var self = this;
    		clearTimeout(this.updateTimer);
    		this.updateTimer = setTimeout(function() {
    			self.updateWeather();
    		}, nextLoad);
    	},
    
    	/* ms2Beaufort(ms)
    	 * Converts m2 to beaufort (windspeed).
    	 *
    	 * see:
    	 *  http://www.spc.noaa.gov/faq/tornado/beaufort.html
    	 *  https://en.wikipedia.org/wiki/Beaufort_scale#Modern_scale
    	 *
    	 * argument ms number - Windspeed in m/s.
    	 *
    	 * return number - Windspeed in beaufort.
    	 */
    	ms2Beaufort: function(ms) {
    		var kmh = ms * 60 * 60 / 1000;
    		var speeds = [1, 5, 11, 19, 28, 38, 49, 61, 74, 88, 102, 117, 1000];
    		for (var beaufort in speeds) {
    			var speed = speeds[beaufort];
    			if (speed > kmh) {
    				return beaufort;
    			}
    		}
    		return 12;
    	},
    
    	/* function(temperature)
    	 * Rounds a temperature to 1 decimal or integer (depending on config.roundTemp).
    	 *
    	 * argument temperature number - Temperature.
    	 *
    	 * return number - Rounded Temperature.
    	 */
    	roundValue: function(temperature) {
    		var decimals = this.config.roundTemp ? 0 : 1;
    		return parseFloat(temperature).toFixed(decimals);
    	}
    });
    
    
    R F 3 Replies Last reply Sep 25, 2017, 9:26 PM Reply Quote 1
    • R Offline
      roramirez Core Contributors @Sp4M
      last edited by Sep 25, 2017, 9:26 PM

      So nice work guys! If somebody else can merge your changes with my PR or pushed other PR with theses changes go ahead.

      I’ll prefer to keep the same behavior if API not get 401

      Easy module development with MagicMirror Module Template

      1 Reply Last reply Reply Quote 0
      • S Offline
        Sp4M
        last edited by Sp4M Sep 25, 2017, 10:17 PM Sep 25, 2017, 9:37 PM

        Thanks @roramirez
        Unfortunately it seems that daily API will not come back on the free version (and I guess that 99% percent of MM users use the free version of openweather)

        I created a pull request with the code I posted.

        1 Reply Last reply Reply Quote 0
        • F Offline
          frank61BS @Sp4M
          last edited by Sep 26, 2017, 10:18 AM

          @Sp4M
          Hm, with this version the module doesn’t come up at all ??
          Where should I configure the parameter maxNumberOfDays ??
          Btw I have a free acount on openweather

          1 Reply Last reply Reply Quote 0
          • F Offline
            frank61BS @Sp4M
            last edited by Sep 26, 2017, 11:53 AM

            @Sp4M
            Sorry it was my mistake.
            The modul seems to work.
            I think I copied it in a wrong way.

            1 Reply Last reply Reply Quote 0
            • MichMichM Offline
              MichMich Admin
              last edited by Sep 29, 2017, 9:11 AM

              I just pushed a fix to the develop branch that should solve the weather forecast. It’s a bit of a hot fix while we are working on the new weather module.

              A new master release is planned to be released this weekend, so if you don’t want to install the develop version, hang tight.

              S 1 Reply Last reply Oct 22, 2017, 9:07 PM Reply Quote 1
              • S Offline
                stevegoble @MichMich
                last edited by Oct 22, 2017, 9:07 PM

                @MichMich fresh upgrade to 2.1.3 and using the entire sample config does not display the weather. I tried the develop branch and updated it with git pull but does the same. Was this fixed? Do I need a fresh build?

                1 Reply Last reply Reply Quote 0
                • MarosM Offline
                  Maros
                  last edited by Aug 27, 2018, 7:57 PM

                  is it possible to show in weatherforecast units ?

                  it’s still showing just number f.e. 22
                  but i want to see 22°C

                  i tried to add units and also decimalSymbol but it still only numbers no sign of Celsius.

                  1 Reply Last reply Reply Quote 0
                  • 1
                  • 2
                  • 3
                  • 4
                  • 5
                  • 5 / 5
                  • 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