Skip to content

Your first cron val

This is a step-by-step guide to a cron val that sends you a weather email every morning.

If you get stuck, please ask for help in the Val Town Discord. We’re friendly!

Step 1: Sign up to Val Town

Sign up to Val Town. It’s free.

Step 2: Create a Cron val

In the top-right corner of Val Town, click New > Cron.

This will create an schedule a new empty val in your account that looks like this:

Step 2
export default async function (interval: Interval) {
}

This function will be run on a schedule.

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.

  1. Add these lines to your code
  2. Replace Brooklyn, NY with your location
  3. Click Run Now
  4. View the output in the val’s logs
Step 3
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);
}

Step 4: Send yourself an email

We’re going to use std/email to send an email to yourself.

  1. Add these lines to your code
  2. Click Run Now
  3. Check your email inbox
Step 4
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`,
});
}

You should have an email with a subject like “Weather now is Sunny and 67°F”.

Step 5: Schedule the val

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).

  1. Go to crongpt.com and generate your cron expression
  2. Click ⚙ (the gear icon) in the top right corner of the val editor
  3. Click Cron
  4. Paste in your cron expression

Next steps

🥳 Congratulations! You’ve created a val 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 gain inspiration from: