A smart power outage detector with UPS alert and Telegram notifications is an essential project for Indian homes and offices where power cuts are frequent and unpredictable. This ESP8266-based project detects when mains power fails, immediately sends a Telegram alert with battery backup status, and notifies you again when power is restored — all running independently on your inverter/UPS power. For Indian cities where power cuts can last 30 minutes to several hours without warning, this early-warning system is genuinely invaluable.
Table of Contents
- Power Outage Detection Methods
- Required Components
- Detection Circuit Design
- ESP8266 Code with Telegram
- Battery Level Monitoring
- Home Assistant Integration
- Advanced: Reconnect Alert and Duration Tracking
- Frequently Asked Questions
Power Outage Detection Methods
Method 1: Voltage Sensing (Recommended)
Use an AC voltage detection module or optocoupler circuit to sense the presence of mains AC voltage. When 230V AC disappears, the sensor output changes from HIGH to LOW, triggering the ESP8266.
Method 2: UPS USB Communication
If your UPS has a USB port, it typically sends NUT (Network UPS Tools) data over USB. A Raspberry Pi connected to the UPS USB port can monitor status and trigger alerts — more reliable than mains sensing for commercial UPS.
Method 3: Power Supply Input Voltage Monitoring
Monitor the ESP8266’s own power supply. When powered by a mains-connected 5V adapter alongside inverter power, you can detect mains failure by monitoring the adapter output voltage — it drops or fluctuates when mains fails and the inverter takes over.
Required Components
- NodeMCU ESP8266 development board
- AC voltage detection module (or 4N25 optocoupler + resistors)
- 5V power supply from inverter output (so device runs during outages)
- Voltage divider circuit for battery voltage monitoring (optional)
- SIM card for cellular backup (optional – use SIM800L for offline alerts)
Total cost: Rs 400-700 for basic version, Rs 800-1,200 with GSM backup.
Detection Circuit Design
The simplest mains sensing circuit uses an optocoupler to safely isolate the high-voltage AC side from the ESP8266’s 3.3V logic:
AC Mains Sensing Circuit (DANGEROUS - work with care):
230V Live (L) -- [100kohm 2W resistor] -- Optocoupler LED anode
Optocoupler LED cathode -- 230V Neutral (N)
(The 100k resistor limits current through the optocoupler LED
to approximately 2.3mA at 230V)
Optocoupler Transistor side:
Collector -- 3.3V via 10k pullup resistor
Emitter -- GND
Base: Not connected
ESP8266 GPIO pin connects to the collector/10k junction:
- AC PRESENT: GPIO reads LOW (optocoupler conducts, pulls to GND)
- AC ABSENT: GPIO reads HIGH (optocoupler off, pulled to 3.3V via resistor)
SAFETY: Keep AC and DC sides completely separate.
Use UL94-V0 rated enclosure.
Pre-Built AC Detection Module
Instead of building from scratch, use a pre-built AC voltage detection module (commonly available for Rs 50-100 on Amazon India). These include the optocoupler isolation and proper resistor values, outputting a clean 3.3V-compatible signal for the ESP8266.
ESP8266 Code with Telegram
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
const char* ssid = "YOUR_WIFI";
const char* password = "YOUR_PASS";
#define BOT_TOKEN "YOUR_BOT_TOKEN"
#define CHAT_ID "YOUR_CHAT_ID" // Your Telegram Chat ID
#define AC_SENSE_PIN D5 // Connected to optocoupler output
#define BATTERY_PIN A0 // For battery voltage monitoring
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
bool lastPowerState = true; // Assume power ON at startup
bool powerOutage = false;
unsigned long outageStartTime = 0;
float getBatteryVoltage() {
// Voltage divider: Battery 12V --> 10k --> A0 --> 3.3k --> GND
// At 12V: ADC reads 12 * 3.3/(10+3.3) = 2.98V = ~910/1023 * 3.3 = 2.93V
int raw = analogRead(BATTERY_PIN);
float voltage = raw * (3.3 / 1023.0) * ((10.0 + 3.3) / 3.3);
return voltage;
}
int getBatteryPercent(float voltage) {
// Lead-acid battery: 12.7V=100%, 12.0V=50%, 11.8V=20%
if (voltage >= 12.7) return 100;
if (voltage >= 12.0) return 50 + ((voltage - 12.0) / 0.7) * 50;
if (voltage >= 11.8) return 20 + ((voltage - 11.8) / 0.2) * 30;
if (voltage >= 11.5) return (voltage - 11.5) / 0.3 * 20;
return 0;
}
void sendAlert(String message) {
if (WiFi.status() == WL_CONNECTED) {
client.setTrustAnchors(&cert);
bot.sendMessage(CHAT_ID, message, "");
}
}
void setup() {
Serial.begin(115200);
pinMode(AC_SENSE_PIN, INPUT_PULLUP);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
configTime(0, 0, "pool.ntp.org"); // Time sync
sendAlert("Power Monitor Online! System ready.");
lastPowerState = (digitalRead(AC_SENSE_PIN) == LOW); // LOW = AC present
}
void loop() {
bool currentPower = (digitalRead(AC_SENSE_PIN) == LOW);
// Detect power cut
if (lastPowerState && !currentPower) {
powerOutage = true;
outageStartTime = millis();
float batt = getBatteryVoltage();
int pct = getBatteryPercent(batt);
String msg = "POWER CUT DETECTED!n";
msg += "Battery: " + String(pct) + "% (" + String(batt, 1) + "V)n";
if (pct > 60) msg += "Estimated backup: 3-4 hours";
else if (pct > 30) msg += "Estimated backup: 1-2 hours";
else msg += "CRITICAL: Battery low! Backup may be limited.";
sendAlert(msg);
}
// Detect power restoration
if (!lastPowerState && currentPower) {
powerOutage = false;
unsigned long duration = (millis() - outageStartTime) / 60000;
String msg = "Power RESTORED!n";
msg += "Outage duration: " + String(duration) + " minutes";
sendAlert(msg);
}
// Alert if battery drops during outage
static unsigned long lastBattAlert = 0;
if (powerOutage && millis() - lastBattAlert > 1800000) { // Every 30 min
int pct = getBatteryPercent(getBatteryVoltage());
if (pct < 30) {
sendAlert("WARNING: Battery at " + String(pct) + "%. Reduce loads!");
lastBattAlert = millis();
}
}
lastPowerState = currentPower;
delay(5000); // Check every 5 seconds
}
Battery Level Monitoring
Monitor your inverter battery voltage using the ESP8266’s ADC pin with a voltage divider. For a 12V lead-acid battery system (most common Indian home inverters):
Voltage Divider for 12V battery to 3.3V ADC:
Battery+ --- [10k ohm] --- A0 pin --- [3.3k ohm] --- GND
Calibration:
- At 12.6V (full): ADC reads ~933
- At 11.5V (empty): ADC reads ~851
- At 14.4V (charging): ADC reads ~1023 (if in charging state)
For 24V battery systems (common in larger inverters):
Use 33k and 10k resistors instead
ADC voltage = Battery * 10/(33+10) = Battery * 0.233
Home Assistant Integration
# Publish power status via MQTT (add to ESP8266 code)
mqtt.publish("power_monitor/status", currentPower ? "ON" : "OFF");
mqtt.publish("power_monitor/battery_pct", String(pct));
# configuration.yaml
mqtt:
binary_sensor:
- name: "Mains Power"
state_topic: "power_monitor/status"
payload_on: "ON"
payload_off: "OFF"
device_class: power
sensor:
- name: "Battery Level"
state_topic: "power_monitor/battery_pct"
unit_of_measurement: "%"
device_class: battery
# Automation: Reduce loads during outage
alias: Power Outage Load Management
trigger:
- platform: state
entity_id: binary_sensor.mains_power
to: "off"
action:
- service: switch.turn_off
entity_id:
- switch.ac_bedroom
- switch.washing_machine
Advanced: Reconnect Alert and Duration Tracking
Maintain a log of power outage events in Home Assistant’s database for analysis:
- Time of outage, duration, and battery level at start
- Monthly outage frequency per area (useful for comparing with DISCOM records)
- Total hours of outage per month (valuable for consumers claiming rebates from DISCOMs)
Frequently Asked Questions
What if WiFi goes down during a power cut?
WiFi router on inverter power stays online. If the router is on mains-only power and goes down, the ESP8266 cannot send Telegram alerts. Solutions: put your router on inverter power, add a SIM800L GSM module for SMS backup alerts, or use a Jio hotspot as a mobile data backup.
How do I know the alert was received if my phone is on airplane mode?
Telegram delivers messages when the phone reconnects. For truly urgent alerts (security-critical systems), use SMS via SIM800L as a primary alert and Telegram as secondary. SMS reaches the phone immediately when service resumes regardless of app state.
Can this system track electricity outages for DISCOM complaint?
Yes. Store each outage event (start time, end time, duration) in a spreadsheet via Google Sheets API or Home Assistant database. Many Indian states now have DISCOM consumer grievance portals that accept outage reports — this data log provides objective evidence for claiming compensation under the Supply Code.
Does this work for 3-phase power supply?
The circuit monitors one phase. For 3-phase supply (industrial/commercial), monitor all three phases with separate optocouplers, or use a commercial phase failure relay to aggregate all three phases into a single output signal for the ESP8266.
Add comment