Met Office weather in daily note

Had a bit of fun getting the Met Office API (UK provider) working as an addition to my daily notes. Used Templater’s user Javascript feature. Sharing here in case anyone else would like a similar set up.

  1. Sign up for an API key via the Met Office Data Hub. I used their global spot daily API but there are other options.
  2. Copy the code below into a JS file in your templater scripts folder (you may need to make this from the settings if it’s not there already).
  3. Add your own ID and API keys in the headers (you have to subscribe and then reset your secret to get both of them), add the latitude and longitude in the fetch URL for the place you want the weather for (can be found via google maps if you don’t have it).
  4. Customise the output for ‘weatherString’ to what works for you.
  5. Add function call to your daily notes template <% tp.user.getWeather() %> where you want it to appear.
function getWeather() {

    // Get weather from the Met Office API

    var myHeaders = new Headers();
    myHeaders.append("X-IBM-Client-Id", "ADD YOUR CLIENT ID");
    myHeaders.append("X-IBM-Client-Secret", "ADD YOUR SECRET");

    var requestOptions = {
        method: 'GET',
        headers: myHeaders,
        redirect: 'follow'
    };

    // Met office standard codes for weather summary

    var weatherCodes = {
        0: 'Clear night',
        1: 'Sunny day',
        2: 'Partly cloudy (night)',
        3: 'Partly cloudy (day)',
        4: 'Not used',
        5: 'Fog',
        7: 'Cloudy',
        8: 'Overcast',
        9: 'Light rain shower (night)',
        10: 'Light rain shower (day)',
        11: 'Drizzle',
        12: 'Light rain',
        13: 'Heavy rain shower (night)',
        14: 'Heavy rain shower (day)',
        15: 'Heavy rain',
        16: 'Sleet shower (night)',
        17: 'Sleet shower (day)',
        18: 'Sleet',
        19: 'Hail shower (night)',
        20: 'Hail shower (day)',
        21: 'Hail',
        22: 'Light snow shower (night)',
        23: 'Light snow shower (day)',
        24: 'Light snow',
        25: 'Heavy snow shower (night)',
        26: 'Heavy snow shower (day)',
        27: 'Heavy snow',
        28: 'Thunder shower (night)',
        29: 'Thunder shower (day)',
        30: 'Thunder'
    };

    // API Call & Formatting of input for daily note

    return fetch("https://api-metoffice.apiconnect.ibmcloud.com/v0/forecasts/point/daily?excludeParameterMetadata=true&includeLocationName=false&latitude=ADD YOUR LATITUDE &longitude=ADD YOUR LONGITUDE", requestOptions)
        .then(response => response.json())
        .then(result => {
            let weather = result.features[0].properties.timeSeries[1];
            let code = weather.daySignificantWeatherCode
            let sigWeather = weatherCodes[code];
            let minTemp = Math.round(weather.dayLowerBoundMaxTemp);
            let maxTemp = Math.round(weather.dayUpperBoundMaxTemp);
            let windSpeed = Math.round(weather.midday10MWindSpeed);
            let rainProb = weather.dayProbabilityOfPrecipitation;

            let weatherString =
                `| 🌤️ Weather for YOUR LOCATION| ${sigWeather}|
                | ------ | --- |
                |Min 🌡️|${minTemp}°C|
                |Max 🌡️|${maxTemp}°C|
                |Chance of 🌧️|${rainProb}%|
                |💨 Speed|${windSpeed}m/s|`;

            return weatherString;
        }
        )
        .catch(error => console.log('error', error));
};

module.exports = getWeather
    
2 Likes