Script to retrieve Weather report from National Weather Service #299
-
I installed the Macro Movie And Series Script today. It's amazing. I was wondering if the same could be done to pull in a weather report from the National Weather Service? Their API is free and doesn't require a Key. I was able to setup a Templater User Command to pull back the JSON document to look at but I'm not proficient in creating a JavaScript to parse it into something useful. The Templater User Command I tested was: curl https://api.weather.gov/gridpoints/PSR/105,73/forecast which will pull back a JSON document. I don't have the skills to translate the Movie and Series Script to pull back the weather forecast instead. I've tried using the WTTR calls but it is hit and miss and the forecast doesn't display on Windows in a user friendly way. I could see the script asking for the X and Y area blocks and storing those and given the same access to the variables in the JSON document as the Movie and Series Script does. I'd be willing to help support someone that could put this script together and buying them some coffees! The reference information is here: https://www.weather.gov/documentation/services-web-api |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Here's a quick script that fetches the weather from the endpoint you've mentioned. module.exports = async (QuickAdd) => {
const fetchRes = await requestUrl("https://api.weather.gov/gridpoints/PSR/105,73/forecast");
if (fetchRes.status !== 200) {
new Notice("Error fetching weather data.");
}
const { properties } = fetchRes.json;
const [now] = properties.periods;
QuickAdd.variables = {
...now,
};
}; Now, any value associated with the Here's what is associated with the most recent period, as of writing: {
"number": 1,
"name": "Today",
"startTime": "2023-02-18T08:00:00-07:00",
"endTime": "2023-02-18T18:00:00-07:00",
"isDaytime": true,
"temperature": 66,
"temperatureUnit": "F",
"temperatureTrend": null,
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": null
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": -5.555555555555555
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 35
},
"windSpeed": "5 to 10 mph",
"windDirection": "ESE",
"icon": "https://api.weather.gov/icons/land/day/sct?size=medium",
"shortForecast": "Mostly Sunny",
"detailedForecast": "Mostly sunny, with a high near 66. East southeast wind 5 to 10 mph."
}, However, personally I prefer wttr.in, as it's easier to work with, and seems to provide data in a more 'accurate' format. const CITY_OPTION = "City name";
module.exports = {
entry: async (QuickAdd, settings) => {
const fetchRes = await requestUrl(`https://wttr.in/${settings[CITY_OPTION]}?format=j1`);
if (fetchRes.status !== 200) {
new Notice("Error fetching weather data.");
}
const { current_condition } = fetchRes.json;
QuickAdd.variables = {
...QuickAdd.variables,
weather: current_condition[0].weatherDesc[0].value,
temperatureC: current_condition[0].temp_C,
temperatureF: current_condition[0].temp_F,
...current_condition[0]
};
},
settings: {
name: "Weather fetcher",
author: "Christian B. B. Houmann",
options: {
[CITY_OPTION]: {
type: "text",
defaultValue: "",
placeholder: "Copenhagen",
},
}
},
}; You'll need to enter your city name in settings, like with the movies script.
|
Beta Was this translation helpful? Give feedback.
Hi @TheHuntyBadger
Here's a quick script that fetches the weather from the endpoint you've mentioned.
Now, any value associated with the
now
period will be put in variables you can use in captures/templates, e.g. ``{{VALUE:temperature}}`.Here's what is associated with the most recent period, as of writing: