MagicMirror² v2.13.0 is available! For more information about this release, check out this topic.

Snow amount on weather forecast


  • Module Developer

    Re: Weather Forecast Snow Amounts
    Please mark as solved.

    before you duplicate the rain amount put above this code

    var winter = moment().format('MM');
        	    if ((winter > = '01' && winter < = '03') || (winter > = '11' && winter < = '12')) { // snow amount show only in winter months
    
    			if (this.config.showSnowAmount) {
    				var snowCell = document.createElement("td");
    				if (isNaN(forecast.snow)) {
    					snowCell.innerHTML = "no snow";
    				} else {
    					if(config.units !== "imperial") {
    						snowCell.innerHTML = parseFloat(forecast.snow).toFixed(1).replace(".", this.config.decimalSymbol) + " mm";
    					} else {
    						snowCell.innerHTML = (parseFloat(forecast.snow) / 25.4).toFixed(2).replace(".", this.config.decimalSymbol) + " in";
    					}
    				}
    				snowCell.className = "align-right xsmall snow";
    				row.appendChild(snowCell);
    			}
    		}
    
    
    

    on processWeather you add:

    	day: day,
    	icon: this.config.iconTable[forecast.weather[0].icon],
    	maxTemp: this.roundValue(forecast.temp.max),
    	minTemp: this.roundValue(forecast.temp.min),
    	rain: this.processRain(forecast, data.list),
    	snow: this.processSnow(forecast, data.list) // snow amount
    
    
    

    then after processRain you add processSnow:

    
    
    
    processSnow: function(forecast, allForecasts) {
    		//If the amount of snow actually is a number, return it
    		if (!isNaN(forecast.snow)) {
    			return forecast.snow;
    		}
    
    	//Find all forecasts that is for the same day
    	var checkDateTime = (!!forecast.dt_txt) ? moment(forecast.dt_txt, "YYYY-MM-DD hh:mm:ss") : moment(forecast.dt, "X");
    	var daysForecasts = allForecasts.filter(function(item) {
    		var itemDateTime = (!!item.dt_txt) ? moment(item.dt_txt, "YYYY-MM-DD hh:mm:ss") : moment(item.dt, "X");
    		return itemDateTime.isSame(checkDateTime, "day") && item.snow instanceof Object;
    	});
    
    	//If no snow this day return undefined so it wont be displayed for this day
    	if (daysForecasts.length == 0) {
    		return undefined;
    	}
    
    	//Summarize all the snow from the matching days
    	return daysForecasts.map(function(item) {
    		return Object.values(item.snow)[0];
    	}).reduce(function(a, b) {
    		return a + b;
    	}, 0);
    }
    
    
    

    also on defaults you can add:

    
    showSnowAmount: true, // only for winter months
    
    

    result: https://github.com/hangorazvan/hourlyforecast/blob/master/preview.png


  • Project Sponsor

    @hango Please answer in the question thread instead of creating a new one.


  • Module Developer

    @broberg OK

    The topic you are replying to is quite old. Would you like to create a new topic instead, and reference this one in your reply?


  • Project Sponsor

    @hango nevertheless, easier to keep things in one thread. That questions is there so one don’t accidentally brings up a dead thread.


Log in to reply