A script I put together to display current weather conditions and sunrise / sunset times for your current location.
It uses Open Weather Map as a data source and requires an API key. You can get one for free by registering at https://openweathermap.org (no affiliation)
This uses imperial units, but could easily be switched to metric. See the Open Weather Map API documentation.
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: globe-americas;
let apiKey = "YOUR-API KEY"
let baseURL = "http://api.openweathermap.org/data/2.5/weather?units=imperial&APPID="
let loc = await Location.current()
let lat = loc.latitude.toFixed(4)
let lon = loc.longitude.toFixed(4)
let elev = Math.round(loc.altitude)
let url = baseURL + apiKey + "&lat=" + String(lat) + "&lon=" + String(lon)
let req = new Request(url)
let json = await req.loadJSON()
let totalRows = 11
let table = new UITable()
// convert times to human-readable
let sRise = convertTime(json.sys.sunrise)
let sSet = convertTime(json.sys.sunset)
let windDir = convertWindDir(json.wind.deg)
let labels = ['conditions', 'temp', 'high', 'low', 'humidity', 'wind speed','wind dir','sunrise','sunset','latitude','longitude','place name']
let data = [json.weather[0].main, String(Math.round(json.main.temp)) + "º", String(Math.round(json.main.temp_max)) + "º", String(Math.round(json.main.temp_min)) + "º", String(json.main.humidity) + "%", String(Math.round(json.wind.speed)) + " mph", windDir, sRise, sSet, json.coord.lat, json.coord.lon, json.name]
for (var i = 0; i <= totalRows; i++) {
let row = new UITableRow()
let leftCell = row.addText(labels[i])
if (data[i]) {
let rightCell = row.addText(data[i].toString())
} else {
let rightCell = row.addText(" ")
}
row.height = 40
row.cellSpacing = 6
table.addRow(row)
}
QuickLook.present(table)
function convertTime(unixTime){
let dt = new Date(unixTime * 1000)
let h = dt.getHours()
let m = "0" + dt.getMinutes()
let t = h + ":" + m.substr(-2)
return t
}
function convertWindDir(deg) {
let compass = ["N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW","N"]
let index = Math.round((deg % 360) / 22.5 )
return compass[index]
}