Wind Direction (Compass Points)



  • One aspect of the weather module I found was missing was the wind direction. Open Weather produces a wind direction as a number in degrees, however I prefer the compass points.

    For anyone interested in my code in the currentweather.js file:

    1. For the main display of wind speed and direction (I’ve also got rid of the Beaufort speeds and changed it back to km/h) my code is as follows:
    var windSpeed = document.createElement("span");
    windSpeed.innerHTML = " " + this.windSpeed + "km/h " + this.windDirection;
    small.appendChild(windSpeed);
    
    1. In the “processWeather” function I have the following:
    processWeather: function(data) {
    		this.temperature = this.roundValue(data.main.temp);
    		this.windSpeed = this.roundValue((data.wind.speed)* 60 * 60 / 1000);
    		this.windDeg = data.wind.deg;
    		this.windDirection = this.deg2dir(this.windDeg);
    		this.weatherType = this.config.iconTable[data.weather[0].icon];
    

    From this function I removed the coversion to the Beaufort scale and added a windSpeed variable to convert metres per second to km/h. Then I have two lines, one giving me the current wind direction in degrees (windDeg) and windDirection which I convert below…

    1. My deg2dir function is a simple nested array which converts the numerical degrees value into a compass point direction:
    deg2dir: function(windDeg) {
    		var degrees = [[11.25,"N"],[33.75,"NNE"],[56.25, "NE"],[78.75, "ENE"],[101.25, "E"], [123.75,"ESE"], [146.25,"SE"],[168.75, "SSE"],[191.25,"S"],[213.75, "SSW"],[236.25, "SW"],[258.75, "WSW"],[281.25,"W"],[303.75,"WNW"],[326.25,"NW"],[348.75,"NNW"],[360, "N"]];
    			for (var i=0; i windDeg) {
    				return degrees[i][1];
    			}
    		}
    		return "";
    	},
    

    Hope this is of use to someone who is looking for alternative data to display.

    Cheers,

    Mat

    Note from Moderator: Please use Markdown on code snippets for easier reading.



  • It seems that since I have updated my mirror to the live version 2 (was working on v2-beta for some time) that there has been very similar code included in the latest version to cater for wind direction, as well as the option to not use the Beaufort scale for wind speed.

    I’ll have to come up with some new code to blow your minds with…

    Cheers,

    Mat


Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.