Skip to content

Instantly share code, notes, and snippets.

@csteinlehner
Last active August 10, 2022 16:56
Show Gist options
  • Save csteinlehner/6208ce202f6ffcdd710d32a47152f41f to your computer and use it in GitHub Desktop.
Save csteinlehner/6208ce202f6ffcdd710d32a47152f41f to your computer and use it in GitHub Desktop.
Scriptable local temperature in F and C
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: magic;
// OpenWeatherMap API 2.5 Key
const apiKey = "__YOUR_API_KEY__";
// Config sizes & colors
let detailFontSize = 40
let cColor = Color.white();
let fColor = Color.orange();
// Get current location
Location.setAccuracyToKilometer();
const loc = await Location.current();
// Get Weather data
async function getCurrentTemp() {
let currentTemp = 0
try {
let weatherReq = "https://api.openweathermap.org/data/2.5/onecall?lat=" + loc.latitude + "&lon=" + loc.longitude + "&exclude=minutely,hourly,daily,alerts" + "&appid=" + apiKey
let weatherData = await new Request(weatherReq).loadJSON();
let currentTemp = weatherData.current.temp;
return currentTemp;
} catch (error) {
}
}
// Fill widget
let widget = new ListWidget();
widget.backgroundColor = Color.black();
// Get temp in Kelvin
let tempValK = await getCurrentTemp();
// Show temp in C
let tempCVal = Math.round(tempValK - 273.15).toString() + "º C";
let tempC = widget.addText(tempCVal);
tempC.font = Font.mediumRoundedSystemFont(detailFontSize)
tempC.textColor = cColor;
tempC.centerAlignText();
// Show temp in F
let tempFVal = Math.round(tempValK * 1.8 - 459.67).toString() + "º F";
let tempF = widget.addText(tempFVal);
tempF.font = Font.mediumRoundedSystemFont(detailFontSize)
tempF.textColor = fColor;
tempF.centerAlignText();
// Close Widget
Script.setWidget(widget);
widget.presentSmall();
Script.complete();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment