A smart plug as a grid sensor: from the local protocol to a cloud pipeline
How we turned an off-the-shelf smart plug into a low-voltage network sensor and a customer energy app: local discovery, a Flutter onboarding flow and a Pub/Sub to BigQuery pipeline.
In 2019 the cheapest voltage sensor we could find on an electricity network wasn't a sensor at all. It was a smart plug of the kind people buy at a hardware store for about thirty dollars to switch a heater on and off from their phone. Plug it into a wall and it measures the voltage at that wall, which is exactly what a distributor can't normally see.
So we built two things on top of it for Essential Energy. For customers, a Flutter app to set up the plug, switch it, schedule it and see what their appliances cost to run. For the network, a pipeline that turns every plug's readings into a low-voltage data point, tagged with the substation and feeder it hangs off. Both ran with real customers. This post is how the two fit together.
The problem
The low-voltage network, the last stretch of wire from the street transformer to the house, has historically been dark. Utilities know what leaves the substation. They know far less about what reaches each home, and rooftop solar and batteries now push power both ways along those wires.
Installing new sensors at scale is slow and expensive. Customers, meanwhile, already own devices that measure voltage and power and will report them on request. The engineering question was whether we could use those devices responsibly: get them online without a support call, read them reliably, and put each reading in the right place on the network.
Architecture
Flutter app --(local network: discovery + setup)--> smart plug
| HTTPS + signed-in user token
v
API (App Engine) ---- Firestore: schedules, latest reading per device
^
cron every 5 min: query each plug --> Pub/Sub topic (one message per plug)
cron every 1 min: run on/off schedules |
v
Cloud Function subscriber: convert units,
look up premise -> substation, feeder, region
| |
BigQuery (history) Firestore (latest) - The phone talks to the plug directly during setup, over the home network.
- The cloud talks to the plugs on a schedule after that, through the vendor's cloud.
- Every reading becomes one message. A small function enriches it and writes it to both a warehouse and a live store.
Talking to the plug
The plug has a local protocol: the app sends small JSON commands, such as "tell me about yourself" or "turn the relay on", and the plug answers in JSON. The messages are wrapped in a lightweight obfuscation, not encryption. It keeps casual eyes off the wire and nothing more. Anyone on the same network who knows the scheme can read and send commands.
We implemented the protocol in Dart, in a single service class in the app, so the phone could find and configure a plug without any cloud round trip. Three lessons from that work apply to almost any consumer device:
- Prototype the protocol before the product. We proved discovery and control in small scripts first, then moved the working version into the app with tests around it. Device protocols are full of surprises, and you want to meet them in a script, not in an onboarding screen.
- Treat the local network as hostile. Obfuscation is not authentication. Assume anything on the local network can see or spoof device traffic, keep anything sensitive off it, and decide who owns a device from the signed-in user in your own backend, not from whoever answers on the network.
- Discovery is a user-experience problem. Finding a device by broadcasting on the local network works until it doesn't: guest networks, mesh routers and phones on mobile data all get in the way. The app needs a calm explanation and a retry, not a spinner.
That last lesson became a seven-step illustrated setup flow: connect, find the plug, name it, link it to your meter and tariff, done. Each step did one thing and explained why. Onboarding is where consumer IoT projects lose people, so it got as much design attention as the pipeline.
Carrying the meter ID
A reading is only useful to the network if you know where it came from. The plug knows nothing about meters, but it does have a user-editable name. During setup the app set that name to a convention of the form <name>-<meter ID>, so every reading from the vendor's cloud arrived already carrying the customer's meter identifier.
It is a small trick, and it saved a mapping table and a whole class of "orphan device" bugs. It also has an obvious weakness: a customer can rename the plug. The subscriber therefore refused any reading whose meter ID it couldn't find, and logged it, instead of guessing.
The pipeline
Every five minutes, a cron service asks the vendor's cloud for each plug's latest power reading. It does that with bounded concurrency, at most ten queries in flight, so a growing fleet doesn't turn into a burst that trips the vendor's rate limits. Each result is published to Pub/Sub as one JSON message per plug.
A Cloud Function subscribes to the topic. It parses the message, takes the meter ID from the device name, looks up the premise, and builds a reading in the same shape every other feed in the platform uses:
const reading = {
timestamp: devicePowerUsage.timestamp,
nmi: nmi,
geolocation: `POINT(${premises[0].lng} ${premises[0].lat})`,
voltagephase1: !devicePowerUsage.powerUsageVoltage_mv ? 0 : devicePowerUsage.powerUsageVoltage_mv / 1000.0,
powerphase1: !devicePowerUsage.powerUsagePower_mw ? 0 : (devicePowerUsage.powerUsagePower_mw / 1000.0) / 1000.0, // was in millwatts, we want kilowatts
devicetype: 'tplinkhs110',
deviceid: devicePowerUsage.deviceId,
substation: premises[0].substation || 'Unknown',
feeder: premises[0].feeder || 'Unknown',
region: premises[0].region || 'Unknown',
} Two things in that excerpt matter more than they look. Units are converted at the edge of the platform, millivolts to volts and milliwatts to kilowatts, so nothing downstream has to remember which vendor reports what. And every reading carries its network context: substation, feeder, town, depot and region. Analysts never need to join a reading back to the asset register; it arrives already placed.
The function then writes twice: an append to BigQuery for history, and an overwrite of a single "latest reading" document per device in Firestore, with a geohash for map queries. One store answers "what happened over the last year", the other answers "what is happening on this street now".
Schedules on the server
Customers could set on/off schedules, such as "heater on at 6 am on weekdays". We ran those on the server, not on the plug: a cron service every minute converts the time to Sydney local time and fires any rules due. Keeping the schedule in our own store meant the app showed exactly what would happen, and a schedule change was one write, not a conversation with a device that might be offline.
What we'd do differently
The premise lookup in the subscriber built its SQL by string interpolation. The meter ID came from a device name a customer could edit, which makes it untrusted input. Today we would parameterise that query on day one and validate the ID format before it reaches the database. The general rule: any value that passed through a consumer device is user input, however it got to you.
Where AI fits
There was no model in this project. What it produced was the raw material for one: time-stamped voltage and power readings, placed on the network, in a warehouse. Metering data in a BigQuery warehouse like this one is what our later demand forecasting models trained on. The lesson we carry into AI work is that most of the effort is here, in getting clean, well-labelled, correctly located data. A model is only as good as the pipeline under it.
What it taught us
This was a small build with a lot of product thinking in it. We designed the onboarding screens and illustrations alongside the code, wrote a deploy README for every service, and kept each service small: an API, three cron jobs and a handful of functions, each doing one job. When the same ingestion pattern was needed for other feeds in the network visibility platform, it was already shaped to be reused.
Why it still matters
Plenty of organisations are surrounded by devices that already measure something useful: plugs, inverters, batteries, vehicles, wearables. The engineering is in reaching them safely, attaching the right context to every reading and landing it where people can use it. If that sounds like your problem, see how we approach real-time, IoT and data platforms.