Current weather with sunrise & sunset

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]
}
2 Likes

Awesome! Thanks for sharing your script!

1 Like

I tried running this but it continually throws undefined is not an object in reference to json.sys.sunrise / sunset / and windDir.

Is this no longer a current api?

What am I doing wrong?

API is still operational.

Make sure you’ve signed up for an API key and put it in the script. getting started with openweathermap

Here’s a sample of what the API will return to you:

{
    "coord": {
        "lon": -73.9,
        "lat": 40.77
    },
    "weather": [
        {
            "id": 800,
            "main": "Clear",
            "description": "clear sky",
            "icon": "01d"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 84.58,
        "pressure": 1016,
        "humidity": 58,
        "temp_min": 80.6,
        "temp_max": 89.01
    },
    "visibility": 16093,
    "wind": {
        "speed": 4.7,
        "deg": 310
    },
    "clouds": {
        "all": 1
    },
    "dt": 1564411050,
    "sys": {
        "type": 1,
        "id": 4686,
        "message": 0.015,
        "country": "US",
        "sunrise": 1564393726,
        "sunset": 1564445758
    },
    "timezone": -14400,
    "id": 5128920,
    "name": "North Beach",
    "cod": 200
}
2 Likes