Action

7 Day Forecast

Posted by @stephenkorsholm, Last update 8 months ago

This action requires an API key from DarkSky.
Dark Sky API site.

The first time this action is run, you will be prompt to add a Dark Sky API key, which will be remembered in Credentials for future use.

You can edit the script to change the units of measurement. All the values to change are at the top of the script.

This action will display a sheet with the current weather and display a 7 day forecast.

It will adapt for dark mode.

Steps

  • script

    // weatherLocation sets the units, go to https://darksky.net/dev/docs for list of unit types.
    // "us" or "ca" would be the most common.
    var weatherLocation = "auto"
    
    // set displayed text for wind. change value below. 
    let windUnit = "kph"
    
    // Set unit type for percipatation
    // Options are "Metric" "Imperial"
    let percipMesurmentType = "Metric"
    
    
    
    // Values below do not need to change
    let percipMesurmentSnow = ''
    let percipMesurementRain = ''
    
    if (percipMesurmentType === "Metric"){
    percipMesurmentSnow = 'cm'
    percipMesurementRain = 'mm'
    }
    
    if (percipMesurmentType === "Imperial"){
    percipMesurmentSnow = 'in'
    percipMesurementRain = 'in'
    }
    
    var percipTypePrint = ""
    
    // set personal API key credential
    let credential = Credential.create("Dark Sky API Key", "Enter your Dark Sky API key, which can be copied from the darksky.net developer console page after creating a free account.");
    
    credential.addTextField("key", "API Key");
    credential.authorize();
    let apiKey = credential.getValue("key");
    
    // Define function for parsing day of week
    var day;
    var month;
    var percipAmmounts = ''
    function getDayOfWeekString(dayAsNumber) {
    
    switch (dayAsNumber) {
      case 0:
        day = "Sunday";
        break;
      case 1:
        day = "Monday";
        break;
      case 2:
        day = "Tuesday";
        break;
      case 3:
        day = "Wednesday";
        break;
      case 4:
        day = "Thursday";
        break;
      case 5:
        day = "Friday";
        break;
      case  6:
        day = "Saturday";
    }
    return day
    }
    
    function getMonthString(monthAsNumber) {
    
    switch (monthAsNumber) {
      case 0:
        month = "January";
        break;
      case 1:
        month = "February";
        break;
      case 2:
        month = "March";
        break;
      case 3:
        month = "April";
        break;
      case 4:
        month = "May";
        break;
      case 5:
        month = "June";
        break;
      case  6:
        month = "July";
        break;
      case 7:
      	month = "August";
      	break;
      case 8:
      	month = "September";
      	break;
      case 9:
      	month = "October";
      	break;
      case 10:
      	month = "November";
      	break;
      case 11:
      	month = "December";
    }
    return month
    }
    
    // method to fetch forecast data
    let getForecast = (latitude, longitude) => {
    	// build URL to Dark Sky API forecast endpoint
    	let endpoint = `https://api.darksky.net/forecast/${apiKey}/${lat},${lon}?units=${weatherLocation}`;
    
    	// create and make HTTP request
    	let http = HTTP.create();
    	let response = http.request({
    		"url": endpoint,
    		"method": "GET"
    	});
    
    	// check for a success 200 response
    	if (response.statusCode != 200) {
    		context.fail();
    		console.log("Dark Sky Error: " + response.error);
    		return null;
    	}
    
    	// parse response JSON to object
    	let data = JSON.parse(response.responseText);
    	if (!data) {
    		context.fail();
    		console.log("Dark Sky Error: Unable to parse response");
    		return null;
    	}
    
    	// return the forecast data object
    	return data;	
    }
    
    let lat = draft.processTemplate("[[latitude]]")
    let lon = draft.processTemplate("[[longitude]]");
    let darkSkyURL = `https://darksky.net/forecast/${lat},${lon}`;
    
    let data = getForecast(lat, lon);
    if (data) {
    	// See details on all information in response at API docs
    	// https://darksky.net/dev/docs
    	let d = [];
    	
    	//time
    	var date = new Date();
    	var formatDate;
    	var getDay;
    	var fomatDateString;
    	var getMonth;
    	var getYear;
    	var dayForJason;
    	function getTimeFromJason (numberForData) {
    		let time = data.daily.data[numberForData].time*1000
    		date.setTime(time)
    		formatDate = date.getDate()
    		getDay = date.getDay()
    		fomatDateString = date.toDateString();
    		getDayOfWeekString(getDay)
    		getMonth = date.getMonth()
    		getMonthString(getMonth)
    		getYear = date.getFullYear()
    		dayForJason = numberForData
    		if(data.daily.data[dayForJason].hasOwnProperty('precipAccumulation')){
    	if (data.daily.data[dayForJason].precipType === "snow"){
    percipTypePrint = percipMesurmentSnow
    }
    if (data.daily.data[dayForJason].precipType === "rain"){
    percipTypePrint = percipMesurmentRain
    }
    	percipAmmounts = `ammount ${(data.daily.data[dayForJason].precipAccumulation).toFixed(0)}`;
    	}
    
    
    } 
    d.push(`<h2>Currently</font></h2>`);
    	d.push('')	
    	d.push(`<p><font color="	#A9A9A9">${(data.currently.summary)}</font></p>`);
    	d.push(`<b>${(data.currently.temperature).toFixed(0)}° feels like ${(data.currently.apparentTemperature).toFixed(0)}°</font></b>`);
    	d.push('')	
    			d.push(`<p>Cloud cover ${(data.currently.cloudCover*100).toFixed(0)}%</font></p>`);
    	d.push('')	
    		d.push(`<p>Wind ${(data.currently.windSpeed).toFixed(0)}-${(data.currently.windGust).toFixed(0)}${windUnit}</font></p>`);
    	
    	d.push('')	
    	d.push('---')	
    	d.push('')	
    	getTimeFromJason(0)
    	d.push('## '+ day + ', ' + month + ' ' + formatDate + ' ')
    	d.push('')	
    	d.push(`<p><font color="	#A9A9A9"> ${(data.hourly.summary)}</font></p>`);
    
     d.push('')
    	d.push(`<b>H: ${(data.daily.data[dayForJason].temperatureHigh).toFixed(0)}°</b>`);
    	d.push('')
    	d.push(`<b>L: ${(data.daily.data[dayForJason].temperatureLow).toFixed(0)}°</b>`);
    		 d.push('')
    		d.push(`Cloud cover ${(data.daily.data[dayForJason].cloudCover*100).toFixed(0)}%`);
    	d.push('')
    	d.push(`Wind ${(data.daily.data[dayForJason].windSpeed).toFixed(0)}-${(data.daily.data[dayForJason].windGust).toFixed(0)}${windUnit}`);
    	d.push('')
    	
    
    
    	d.push(`${(data.daily.data[dayForJason].precipProbability*100).toFixed(0)}% chance of ${data.daily.data[dayForJason].precipType} ${percipAmmounts}${percipTypePrint}`);
    
    	percipAmmounts = ''
    	d.push('')
    	d.push('---')
    	var i;
    	for (i = 1; i < 8; i++) {
    	getTimeFromJason(i)
    	d.push('## '+ day + ', ' + month + ' ' + formatDate)
    	 d.push('')
    	d.push(`<p><font color="	#A9A9A9">${(data.daily.data[dayForJason].summary)}</font></p>`);
    
     d.push('')
    	d.push(`<b>H: ${(data.daily.data[dayForJason].temperatureHigh).toFixed(0)}°</b>`);
    	d.push('')
    	d.push(`<b>L: ${(data.daily.data[dayForJason].temperatureLow).toFixed(0)}°</b>`);
    	
    		
    	 d.push('')
    		d.push(`Cloud cover ${(data.daily.data[dayForJason].cloudCover*100).toFixed(0)}%`);
    		
    	d.push('')
    	d.push(`Wind ${(data.daily.data[dayForJason].windSpeed).toFixed(0)}-${(data.daily.data[dayForJason].windGust).toFixed(0)}${windUnit}`);
    	d.push('')
    
    
    	d.push(`${(data.daily.data[dayForJason].precipProbability*100).toFixed(0)}% chance of ${data.daily.data[dayForJason].precipType} ${percipAmmounts}${percipTypePrint}`);
    
    	percipAmmounts = ''
    	d.push('')
    	d.push('---')
    	d.push('')
    	percipTypePrint = ""
    	}
    	
    		//editor.setText(d.join("\n"));
    		
    		let s = d.join("\n")
    draft.setTemplateTag("displayText",s);
    
    }
    else {
    	alert("Unable to fetch Dark Sky forecast information. Check action log for details");
    }
    //editor.deactivate()
  • htmlpreview

    <!DOCTYPE html>
    <html dir="auto">
    
    <head>
      <title>Weather</title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <style>
        @charset "utf-8";
    
        :root {
          --main-bg-color: white;
          --main-color: black;
          --alternate-bg-color: #efefef;
          --alternate-color: #222222;
          --main-border-color: #BBBBBB;
            --link-color: #3a9fbf;
        }
    
        @media (prefers-color-scheme: dark) {
          :root {
            --main-bg-color: #222222;
            --main-color: #eeeeee;
            --alternate-bg-color: #444444;
            --alternate-color: #cccccc;
            --main-border-color: #AAAAAA;
              --link-color: #FC830A;
          }
        }
    
        html {
          font-size: 90%;
          font-family: iA Writer Duospace, iA Writer Quattro S, -apple-system;
          line-height: 1.0em;
        }
    
        body {
          margin: 0;
          padding: 1em;
          background-color: var(--main-bg-color);
          color: var(--main-color);
        }
    
        @media (max-device-width: 480px) {}
    
        @media (min-device-width: 481px) {
          body {
            margin: auto;
            max-width: 600px;
          }
        }
    
        blockquote {
          font-style: italic;
          margin: 1.5em 2em;
          padding: 1em;
          background-color: var(--alternate-bg-color);
          color: var(--alternate-color);
        }
    
        a {
          color: var(--link-color);
        }
    	b {
          color: var(--link-color);
        }
        pre {
          display: block;
          overflow: scroll;
          width: 100%;
          background-color: var(--alternate-bg-color);
          padding: .5em 1em;
          margin: 1em 0;
        }
    
        code {
          background-color: var(--alternate-bg-color);
          color: var(--alternate-color);
          font-family: Menlo, Courier, sans-serif;
          padding: 2px 3px;
        }
    
        table {
          margin: 1.5em 0;
          border: 1px solid var(--main-border-color);
          border-collapse: collapse;
        }
    
        th {
          padding: .25em .5em;
          background: var(--alternate-bg-color);
          border: 1px solid var(--main-border-color);
        }
    
        td {
          padding: .25em .5em;
          border: 1px solid var(--main-border-color);
        }
    
        img {
          max-width: 90%;
        }
      </style>
    </head>
    
    <body>
      %%[[displayText]]%%
    </body>
    
    </html>
    

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.