This is a guide to creating a cron job that sends you a weather email every morning.
Step 1: Sign up to Val Town
Section titled “Step 1: Sign up to Val Town”Sign up to Val Town. It’s free.
Step 2: Set up a Cron Trigger
Section titled “Step 2: Set up a Cron Trigger”Create a new val. In a new file, set the trigger type to Cron.
This will create a scheduled trigger associated with this file. The default code will look like this:
export default async function (interval: Interval) { // your code…}
This function will be run on a schedule. By default, this runs every hour. You can change the frequency by clicking on the Cron in the top right corner of the file.
Step 3: Get the weather
Section titled “Step 3: Get the weather”In the following example code, we are using getWeather
from stevekrouse/getWeather. It uses the free wttr.in service to get weather data.
- Add these lines to your code
import { getWeather } from "https://esm.town/v/stevekrouse/getWeather";
export default async function (interval: Interval) { let weather = await getWeather("Brooklyn, NY"); console.log(weather.current_condition[0].FeelsLikeF);}
- Replace
Brooklyn, NY
with your location - Click Run
- View the output in the val’s logs
Step 4: Send yourself an email
Section titled “Step 4: Send yourself an email”We’re going to use std/email to send an email to yourself.
- Add these lines to your code
import { email } from "https://esm.town/v/std/email";import { getWeather } from "https://esm.town/v/stevekrouse/getWeather";
export default async function (interval: Interval) { let weather = await getWeather("Brooklyn, NY"); let feelsLike = weather.current_condition[0].FeelsLikeF; let description = weather.current_condition[0].weatherDesc[0].value; await email({ subject: `Weather now is ${description} and ${feelsLike}°F`, });}
- Click Run
- Check your email inbox
You should have an email with a subject like “Weather now is Sunny and 67°F”.
Step 5: Schedule the Job
Section titled “Step 5: Schedule the Job”We want this to run every morning. For example, in NYC, you could use 0 9 * * *
to run at 5am ET every morning.
We recommend crongpt.com to help you write cron expressions. It also handles the timezone conversion from your timezone to Val Town’s server timezone (UTC).
- Go to crongpt.com and generate your cron expression
- Click the clock icon (or find the schedule settings) in the file editor for your
weatherNotifier.ts
file. - Click Cron
- Paste in your cron expression.
Next steps
Section titled “Next steps”🥳 Congratulations! You’ve created a Cron job that sends you a weather notification every morning. You can customize it to your needs. There are a LOT of weather vals you can use or get inspiration from:
Which_Jacket
- email alerts about which jacket to wearumbrellaReminder
- email alerts when it’s going to rainweatherGPT
- email alerts with a ChatGPT-written messagepowderNotify
- email alerts when it snowssailingNotifyCron
- email alerts when it’s a good day to saildiscordWebhookWeatherHyd
- send weather updates to Discordweather_forecast_in_the_morning
- weather forecast on TelegramweatherBot
- OpenAI Weather Bot via function callingaqi
- email alerts when AQI is unhealthy near you- …add yours here! (via the edit button below)