Skip to content

Guide

Blynk Automations and Schedules for ESP32: Timers, Sunset and Sensor Rules

Automate an ESP32 with Blynk: expose datastreams, create Schedule, Sunrise/Sunset and Device State rules, and add a local NTP fallback so lights still switch offline.

  • 4 min read
  • Updated November 2, 2026
Blynk Automations and Schedules for ESP32: Timers, Sunset and Sensor Rules

Quick answer: Blynk Automations turn your ESP32 into a scheduled smart device without extra code. In Blynk.Console → Templates → your template → Automations → Edit, switch on Action for the datastream you want to control (for example V0 Relay, type Switch), then Save And Apply. Then open the Automations page in the Blynk app or console and create a rule: When Schedule (time and days), Sunrise/Sunset or Device State (for example temperature above 30 °C), Do Control Device → V0 = 1. Your sketch only needs the normal BLYNK_WRITE(V0) handler. If the schedule must keep working without internet, add a local fallback schedule in the firmware using NTP time (sketch below).

Automation trigger (Blynk "condition")Example
ScheduleGarden light ON at 18:30, Mon–Sun
Sunrise/SunsetPorch light ON 15 min after sunset, OFF at sunrise
Device StateFan ON when V2 temperature > 30 °C
SceneOne tap "Night mode" sets several devices at once
ActionNotes
Control DeviceWrites a value to a datastream; your BLYNK_WRITE runs on the ESP32
Wait, Then Do SomethingDelay from 1 second to 24 hours, e.g. pump ON → wait 10 min → OFF
Send In-App Notifications / Send EmailAlerts when a rule fires
Send SMSEnterprise plan only

(Trigger and action names are from Blynk's Automations documentation; available features depend on your plan.)

1. Make a datastream available to Automations

Automations can only use datastreams you've exposed in the template:

  1. Turn on Developer Mode (your profile menu in Blynk.Console).
  2. Templates → your template → Automations tab → Edit (top right).
  3. For V0 Relay: switch on Action, choose the automation type Switch (on/off control). Optionally for V2 Temperature: switch on Condition, type Sensor.
  4. Click Save And Apply.

The Automations menu now appears in the Blynk app and the console. If you don't see it, no datastream has Condition or Action switched on yet.

2. Create a schedule

In the Blynk IoT app: Automations → + → Schedule:

  1. When: time 18:30, repeat on the days you want, and check the time zone (it defaults to your account/phone setting).
  2. Do: Control Device → pick your ESP32 → Relay → ON.
  3. Save. Create a second automation for OFF at 23:00, or use Wait, Then Do Something inside the first one.

For "lights at dusk", choose Sunrise/Sunset instead of Schedule, set your location and an offset (for example +15 minutes after sunset), and the time moves with the seasons automatically.

For a sensor rule: Device State → Temperature (V2) is above 30 → Control Device → Fan ON. Use the rule's limit period setting so it doesn't fire on every reading while the value stays high.

Where should the schedule run?

OptionRuns onWorks without internetEasy to changeBest for
Blynk AutomationsBlynk.CloudNoYes, from the appEveryday timetables, sunset lights, sensor rules
Local NTP schedule (sketch below)The ESP32Yes, after one time syncNo, needs a re-flash (or a datastream for the time)Safety rules: "always off at 23:00", maximum run times
ESP32 + DS3231 RTCThe ESP32Yes, even from a cold bootNoDevices that may boot with no internet at all
Node-RED or Home AssistantYour own serverYes, on your LANYesWhole-house automations across many devices

Most projects combine the first two: Blynk for convenience and a small local rule for safety. If you already run a home server, compare the options in Blynk vs Node-RED vs Home Assistant.

3. The ESP32 sketch (with a local fallback schedule)

The cloud automation simply writes V0, so the core of the sketch is the usual handler. The extra part keeps a local schedule running from NTP time: if Wi-Fi or Blynk is down at 23:00, the ESP32 still switches the light off on its own.

Set TZ_INFO to your time zone as a POSIX string:

LocationTZ_INFO
IndiaIST-5:30
UKGMT0BST,M3.5.0/1,M10.5.0
Central EuropeCET-1CEST,M3.5.0,M10.5.0/3
US EasternEST5EDT,M3.2.0,M11.1.0

(The sign is inverted in POSIX strings: India is UTC+5:30 but written -5:30.)

sketch.ino
#define BLYNK_TEMPLATE_ID   "TMPLxxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "Scheduled Light"
#define BLYNK_AUTH_TOKEN    "REPLACE_WITH_DEVICE_AUTH_TOKEN"
#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <time.h>

const char* WIFI_SSID = "REPLACE_WITH_WIFI";
const char* WIFI_PASS = "REPLACE_WITH_WIFI_PASSWORD";
const char* TZ_INFO   = "IST-5:30";          // your POSIX time zone

const int  RELAY_PIN = 26;
const bool RELAY_ACTIVE_LOW = true;
const int  LOCAL_OFF_HOUR = 23, LOCAL_OFF_MIN = 0;   // fallback "lights off"

BlynkTimer timer;
bool relayOn = false;

void setRelay(bool on) {
  relayOn = on;
  digitalWrite(RELAY_PIN, (on == RELAY_ACTIVE_LOW) ? LOW : HIGH);
  if (Blynk.connected()) Blynk.virtualWrite(V1, on);  // reported state
  Serial.printf("Relay %s\n", on ? "ON" : "OFF");
}

BLYNK_WRITE(V0) { setRelay(param.asInt() == 1); }      // automations and the app write V0
BLYNK_CONNECTED() { Blynk.virtualWrite(V0, relayOn); Blynk.virtualWrite(V1, relayOn); }

// Runs every 20 s: fires the local OFF once per day at LOCAL_OFF_HOUR:LOCAL_OFF_MIN
void localSchedule() {
  struct tm now;
  if (!getLocalTime(&now, 10)) return;                 // no valid time yet: do nothing
  static int lastDay = -1;
  if (now.tm_hour == LOCAL_OFF_HOUR && now.tm_min == LOCAL_OFF_MIN && now.tm_yday != lastDay) {
    lastDay = now.tm_yday;
    if (relayOn) setRelay(false);
  }
}

void setup() {
  digitalWrite(RELAY_PIN, RELAY_ACTIVE_LOW ? HIGH : LOW);   // safe OFF before networking
  pinMode(RELAY_PIN, OUTPUT);
  Serial.begin(115200);

  WiFi.begin(WIFI_SSID, WIFI_PASS);
  configTzTime(TZ_INFO, "pool.ntp.org", "time.google.com"); // keeps syncing in the background
  Blynk.config(BLYNK_AUTH_TOKEN);

  timer.setInterval(20000L, localSchedule);
  timer.setInterval(10000L, []() {
      if (WiFi.status() == WL_CONNECTED && !Blynk.connected()) Blynk.connect(3000);
  });
}

void loop() {
  if (Blynk.connected()) Blynk.run();
  timer.run();
}

How it works

PartPurpose
BLYNK_WRITE(V0)Blynk's Schedule, Sunrise/Sunset and Device State automations all arrive here, exactly like a tap in the app
configTzTime()Gets time from NTP and applies your time zone and daylight-saving rules
getLocalTime(&now, 10)Returns false until NTP time is valid, so nothing fires on a bogus 1970 date
lastDay checkFires the local rule once per day, not on every check within that minute
Blynk.config() + connect()Non-blocking: the local schedule runs even when the cloud is unreachable

Cloud or local: who owns the schedule? Let Blynk own the normal timetable (easy to change from your phone) and keep only a safety rule locally, such as "off at 23:00 no matter what" or "pump never runs longer than 10 minutes". Don't put the same ON rule in both places, or you'll get double actions after a reconnect.

4. Safety limits belong in the firmware

A schedule requests an action; the ESP32 should still enforce limits:

  • Maximum run time for pumps, heaters and motors, for example if (relayOn && millis() - onSince > 10UL*60*1000) setRelay(false);
  • Safe boot state: OFF before Wi-Fi starts (done in setup() above), and no blind restore of an old command after a power cut.
  • No valid time = no time-based action: the sketch skips the local rule until NTP succeeds. Add a DS3231 RTC if the device must keep time through long internet outages.
  • Manual override: a physical button (see Blynk relay control) so the light works even if both cloud and schedule fail.

5. Test it properly

TestExpected result
Schedule set 2 minutes aheadRelay switches at that minute; V1 and the app follow
Sunrise/Sunset ruleCheck the next trigger time Blynk shows matches your local sunset
Device State rule (warm the sensor)Fires once, then respects the limit period
Unplug the router before 23:00Local fallback switches OFF at 23:00
Reboot the ESP32 with the relay ONComes back OFF, and the app shows OFF
Serial Monitor timePrint getLocalTime once: correct local time, not UTC

Troubleshooting

SymptomLikely causeFix
No Automations menuNo datastream has Condition/Action enabledTemplate → Automations → Edit → switch on Action → Save And Apply
Datastream not offered in the ruleNot exposed, or wrong automation typeExpose it with type Switch (for on/off)
Automation fires but relay doesn't moveHandler on a different virtual pin, or device offlineCheck BLYNK_WRITE(V0) and the Online badge
Fires at the wrong hourAutomation time zone differs from yoursSet the time zone in the automation; check the account's time zone
Local rule fires at the wrong timeWrong TZ_INFO or signRemember POSIX signs are inverted (India = IST-5:30)
Local rule never firesNo NTP time (no internet since boot)Needs one successful NTP sync; add an RTC for offline boots
Device State rule fires repeatedlyValue stays above the thresholdUse the limit period, or a hysteresis band
Double switching after reconnectSame rule in Blynk and in firmwareOne owner per action; keep only safety rules local

FAQ

Does a Blynk schedule work if my phone is off? Yes. Automations run on Blynk.Cloud, not on your phone. The ESP32 must be online to receive the command.

What happens if the ESP32 is offline when an automation fires? It misses that command. When it reconnects, this sketch reports its real state rather than guessing. Use the local fallback for actions that must happen.

Can Blynk switch a light at sunset? Yes: create an automation with the Sunrise/Sunset condition, set your location and an optional offset.

Can an automation use a sensor value? Yes: a Device State condition on a sensor datastream (exposed with Condition switched on), for example temperature above 30 °C.

Is BlynkTimer a scheduler? No. BlynkTimer runs functions at intervals (every 2 seconds, every minute). For clock times, use Blynk Automations or NTP time as shown above.

Next steps

Official references: Blynk Automations and Blynk documentation.

Continue learning

Related articles

All articles

Signal In

Built from what makers search for

The lookups piling up in SolderHub's search bar every day shape what we publish next — new component pages, board guides, and firmware notes, sent out as they go live.

By subscribing you agree to our Privacy Policy. Unsubscribe anytime.