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 |
|---|---|
| Schedule | Garden light ON at 18:30, Mon–Sun |
| Sunrise/Sunset | Porch light ON 15 min after sunset, OFF at sunrise |
| Device State | Fan ON when V2 temperature > 30 °C |
| Scene | One tap "Night mode" sets several devices at once |
| Action | Notes |
|---|---|
| Control Device | Writes a value to a datastream; your BLYNK_WRITE runs on the ESP32 |
| Wait, Then Do Something | Delay from 1 second to 24 hours, e.g. pump ON → wait 10 min → OFF |
| Send In-App Notifications / Send Email | Alerts when a rule fires |
| Send SMS | Enterprise 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:
- Turn on Developer Mode (your profile menu in Blynk.Console).
- Templates → your template → Automations tab → Edit (top right).
- For V0 Relay: switch on Action, choose the automation type Switch (on/off control). Optionally for V2 Temperature: switch on Condition, type Sensor.
- 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:
- When: time
18:30, repeat on the days you want, and check the time zone (it defaults to your account/phone setting). - Do: Control Device → pick your ESP32 → Relay → ON.
- 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?
| Option | Runs on | Works without internet | Easy to change | Best for |
|---|---|---|---|---|
| Blynk Automations | Blynk.Cloud | No | Yes, from the app | Everyday timetables, sunset lights, sensor rules |
| Local NTP schedule (sketch below) | The ESP32 | Yes, after one time sync | No, needs a re-flash (or a datastream for the time) | Safety rules: "always off at 23:00", maximum run times |
| ESP32 + DS3231 RTC | The ESP32 | Yes, even from a cold boot | No | Devices that may boot with no internet at all |
| Node-RED or Home Assistant | Your own server | Yes, on your LAN | Yes | Whole-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:
| Location | TZ_INFO |
|---|---|
| India | IST-5:30 |
| UK | GMT0BST,M3.5.0/1,M10.5.0 |
| Central Europe | CET-1CEST,M3.5.0,M10.5.0/3 |
| US Eastern | EST5EDT,M3.2.0,M11.1.0 |
(The sign is inverted in POSIX strings: India is UTC+5:30 but written -5:30.)
#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
| Part | Purpose |
|---|---|
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 check | Fires 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
| Test | Expected result |
|---|---|
| Schedule set 2 minutes ahead | Relay switches at that minute; V1 and the app follow |
| Sunrise/Sunset rule | Check 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:00 | Local fallback switches OFF at 23:00 |
| Reboot the ESP32 with the relay ON | Comes back OFF, and the app shows OFF |
| Serial Monitor time | Print getLocalTime once: correct local time, not UTC |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| No Automations menu | No datastream has Condition/Action enabled | Template → Automations → Edit → switch on Action → Save And Apply |
| Datastream not offered in the rule | Not exposed, or wrong automation type | Expose it with type Switch (for on/off) |
| Automation fires but relay doesn't move | Handler on a different virtual pin, or device offline | Check BLYNK_WRITE(V0) and the Online badge |
| Fires at the wrong hour | Automation time zone differs from yours | Set the time zone in the automation; check the account's time zone |
| Local rule fires at the wrong time | Wrong TZ_INFO or sign | Remember POSIX signs are inverted (India = IST-5:30) |
| Local rule never fires | No NTP time (no internet since boot) | Needs one successful NTP sync; add an RTC for offline boots |
| Device State rule fires repeatedly | Value stays above the threshold | Use the limit period, or a hysteresis band |
| Double switching after reconnect | Same rule in Blynk and in firmware | One 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
- Blynk relay control with safe boot and state sync: the relay side of these automations.
- ESP32 plant watering with Blynk: a schedule plus a soil-moisture Device State rule.
- Blynk PIR motion notifications: events and alerts.
- Blynk 2.0 complete guide.
- Self-hosted alternative: Node-RED scheduler and context.
Official references: Blynk Automations and Blynk documentation.




