Plugin: Today's Weather (°C or °F)

name

Today's Weather

icon

thermostat

description

A plugin that inserts the weather's Max and Min temperatures (°C or °F) on your cursor location. Choose between default, server or custom locations. ✨

instructions

Just start with the { shortcut and select the one of the weather options from the list.

You can set your preferred temperature unit in the settings ('C' or 'F'). Default is Celsius.

There are three options:

"Weather from Server Location": Finds the weather based on your IP address.


"Custom weather": The app will prompt you to insert a town name or the latitude and longitude, and then find the weather for that location.

GPS Coordinates


Town Name

"Default Town weather": After you have set the 'Latitude', 'Longitude' and 'Location Name' settings in the Amplenote Plugin settings , it should work

Example (in the settings):


Example output:


setting

Latitude

setting

Longitude

setting

Location Name

setting

Temperature Unit ('C' or 'F')

linkCode:

{
// Main insertText object for handling different expressions
insertText: {
// Dynamic location-based weather
"Weather from Server Location": async function (app) {
return await this.currentLocationWeather(app);
},
 
// Weather based on user-specified GPS coordinates or town name via prompt
"Custom Weather": async function (app) {
try {
const input = await app.prompt("Enter GPS coordinates (latitude, longitude) or a town name:");
if (!input) {
return "No input provided.";
}
 
let latitude, longitude, location;
 
// Check if input is GPS coordinates
const coordinateMatch = input.match(/^\s*(-?\d+(\.\d+)?),\s*(-?\d+(\.\d+)?)\s*$/);
if (coordinateMatch) {
latitude = parseFloat(coordinateMatch[1]);
longitude = parseFloat(coordinateMatch[3]);
location = await this.getLocationFromCoordinates(latitude, longitude);
} else {
// If not coordinates, treat input as a town name and geocode it
const geoUrl = `https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(input)}&limit=1`;
const geoResponse = await fetch(geoUrl);
const geoData = await geoResponse.json();
 
if (geoData.length === 0) {
return `Unable to find location for "${input}".`;
}
 
latitude = parseFloat(geoData[0].lat);
longitude = parseFloat(geoData[0].lon);
location = geoData[0].display_name;
}
 
const unit = app.settings["Temperature Unit ('C' or 'F')"] || "C";
const weather = await this.getWeatherForCoordinates(latitude, longitude, unit);
return `${location}: ${weather}`;
} catch (error) {
return "Unable to retrieve weather data.";
}
},
 
// Weather for default location set in settings
"Default Town Weather": async function (app) {
const latitude = app.settings["Latitude"] || "43.836700";
const longitude = app.settings["Longitude"] || "4.360100";
const locationName = app.settings["Location Name"] || "Default Location";
const unit = app.settings["Temperature Unit ('C' or 'F')"] || "C";
const weather = await this.getWeatherForCoordinates(latitude, longitude, unit);
return `${locationName}: ${weather}`;
}
},
 
// Function to get weather for the user's current location based on IP
async currentLocationWeather(app) {
try {
const geoResponse = await fetch("https://ipinfo.io/json");
const geoData = await geoResponse.json();
const [latitude, longitude] = geoData.loc.split(",");
const city = geoData.city || "Unknown City";
const country = geoData.country || "Unknown Country";
const location = `${city}, ${country}`;
 
const unit = app.settings["Temperature Unit ('C' or 'F')"] || "C";
const weather = await this.getWeatherForCoordinates(latitude, longitude, unit);
return `${location}: ${weather}`;
} catch (error) {
return "Unable to retrieve location-based weather data.";
}
},
 
// Helper function to reverse geocode coordinates into a location name
async getLocationFromCoordinates(latitude, longitude) {
try {
const reverseGeoUrl = `https://nominatim.openstreetmap.org/reverse?format=json&lat=${latitude}&lon=${longitude}&zoom=10&addressdetails=1`;
const response = await fetch(reverseGeoUrl);
const data = await response.json();
 
const city = data.address.city || data.address.town || data.address.village || "Unknown City";
const state = data.address.state || "";
const country = data.address.country || "Unknown Country";
 
return state ? `${city}, ${state}, ${country}` : `${city}, ${country}`;
} catch (error) {
return "Unknown Location";
}
},
 
// Helper function to convert Celsius to Fahrenheit
convertToFahrenheit(celsius) {
return (celsius * 9 / 5) + 32;
},
 
// Helper function to get weather for specified coordinates with unit support
async getWeatherForCoordinates(latitude, longitude, unit = "C") {
try {
const weatherUrl = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&daily=temperature_2m_min,temperature_2m_max,weathercode&timezone=auto`;
const weatherResponse = await fetch(weatherUrl);
const weatherData = await weatherResponse.json();
 
let minTemp = weatherData.daily.temperature_2m_min[0];
let maxTemp = weatherData.daily.temperature_2m_max[0];
const weatherCode = weatherData.daily.weathercode[0];
 
if (unit === "F") {
minTemp = this.convertToFahrenheit(minTemp).toFixed(1);
maxTemp = this.convertToFahrenheit(maxTemp).toFixed(1);
}
 
// Weather code to emoji mapping
const weatherEmojis = {
0: "☀️", // Clear
1: "🌤️", 2: "⛅️", 3: "🌥️", // Partly cloudy
45: "🌫️", 48: "🌫️", // Fog
51: "🌦️", 53: "🌦️", 55: "🌧️", // Drizzle
61: "🌧️", 63: "🌧️", 65: "🌧️", // Rain
71: "❄️", 73: "❄️", 75: "❄️", // Snow
80: "🌧️", 81: "🌧️", 82: "🌧️", // Showers
95: "⛈️", 96: "⛈️", 99: "⛈️" // Thunderstorms
};
 
const weatherEmoji = weatherEmojis[weatherCode] || "🌈"; // Default emoji if code not found
const tempUnit = unit === "F" ? "°F" : "°C";
return `${weatherEmoji} Max: ${maxTemp}${tempUnit}, Min: ${minTemp}${tempUnit}`;
} catch (error) {
return "Unable to retrieve weather data.";
}
}
}

linkChangelog

January 21st, 2025: Implemented Temperature Unit setting

January 13th, 2025: Published the initial version