Act on it: your first alert and your first command
On this page
Your device now sends numbers, and you can read them from anywhere. That is half of the job. A tank that overflows at 2am does not care that the chart was correct. This lesson is about the other half: what happens when a number goes wrong, and who or what does something about it.
Three places an action can live
Every action you want belongs in one of three places. Choosing the right one is the whole skill.
- Firmware is for anything that must work when the network is down: a safety cutoff, a limit, a control loop.
- A rule on the platform is for what one device cannot know: another device's reading, the time of day across a site, a limit you want to change without a reflash.
- A person is for a judgement: is this a fault or a visitor, do we send a technician tonight or tomorrow.
The tank is at 94% and rising. The Wi-Fi router loses power. Which protection still stops the pump?
An alert a person will answer
An alert is the simplest action: when a number crosses a limit, tell someone. It is also the easiest to get wrong, because an alert that fires every night is ignored on the night that matters. Four choices decide whether yours is answered.
Your tank sends its level every 30 seconds. You want to be told when it is about to run dry. What exactly do you set?
What you know: the pump must not run below 10%. A refill takes about 20 minutes. Water sloshes, so one reading can jump by a few percent.
Show the worked solution, 4 steps
- Choose the field and the limit
The field is the level. The limit is not 10%: at 10% it is already too late to act. With a 20 minute refill, alert at 25%, so there is time to do something.
- Say how long it must last
One low reading is a wave, not a problem. Ask for the level to stay below 25% for 2 minutes, which is 4 readings in a row. This one choice removes most false alarms.
- Say who is told, and who is told next
The person who can refill the tank is first. If they do not confirm, someone else must be told. An alert that reaches nobody is the same as no alert.
- Test it on purpose
Lower the level, or publish a low value by hand, and watch the alert arrive. An alert that has never fired is not known to work.
Any monitoring setup asks for these four choices. On a server you run, a rule engine such as Node-RED holds them; the alerts and rule engines lesson builds one. On siliconwit.io, our monitoring service and the worked example of this course, you set them on the device's Automation tab and on the Alerts page.
A command the device obeys and confirms
You already know that your device publishes to d/<id>/t. Commands travel the other way, on d/<id>/c. The device subscribes to that topic, does what it is asked, and then says so.
The command arrives as JSON with a command name, an optional value, and a session that identifies this one command. siliconwit.io waits 30 seconds for the reply on d/<id>/k/<session>; a server of your own can use the same three topics. No reply means nobody can say the action happened, and the command shows as not confirmed.
// 1. The safety cutoff lives here, in firmware. It needs no network.
void loop() {
float level = readTankLevel(); // percent
if (level >= 95.0) pumpOff(); // always works, online or not
mqtt.loop(); // then let commands arrive
}
// 2. Listen for commands on your own command topic.
void onConnect() {
String cmdTopic = String("d/") + DEVICE_ID + "/c";
mqtt.subscribe(cmdTopic.c_str());
}
// 3. Obey, then confirm on the reply topic within 30 seconds.
void onMessage(char* topic, byte* body, unsigned int len) {
JsonDocument cmd; deserializeJson(cmd, body, len);
const char* name = cmd["command"]; // for example "pump"
const char* session = cmd["session"]; // identifies this one command
if (strcmp(name, "pump") == 0) {
if (cmd["value"] == "off") pumpOff(); else if (readTankLevel() < 95.0) pumpOn();
}
String reply = String("d/") + DEVICE_ID + "/k/" + session;
mqtt.publish(reply.c_str(), "{\"ok\":true}");
} Read the code in the order of its three comments. The cutoff is first and does not depend on anything below it. The command handler refuses to start the pump when the tank is already full: a device should never trust a command more than its own sensor. The reply fields siliconwit.io accepts are listed in the siliconwit.io documentation; the sketch above sends the shortest one.
Your device stops sending at 3am because its power supply failed. No limit was crossed, so no alert fired. What would have told you, and where does that check have to live?
Show the answer, 2 steps
- It cannot live on the device
A device that has lost power cannot report that it has lost power. This is the one check firmware can never do for itself.
- The platform checks for silence
Set an alert on "no data for 5 minutes". With a reading every 30 seconds, 5 minutes is ten missed readings, which is not a short Wi-Fi fault. This is the clearest case where the cloud earns its place.
Check yourself
A heater must switch off at 80 °C or the element is destroyed. Where does that limit go?
Your alert fires five times a night because the reading jumps for a few seconds. What do you change first?
The platform shows a "pump off" command as sent, and no reply came back. What do you know?
- An action lives in firmware, in a platform rule, or with a person. Anything that must work with the network down lives in firmware.
- An alert needs four choices: the field, a limit that leaves time to act, how long it must last, and who is told next.
- Commands come down on
d/<id>/c. The device replies ond/<id>/k/<session>within 30 seconds. - A command with no reply is "sent", not "done". A silent device can only be caught from outside it.
You can now measure, and act on what you measure. What remains is the record: showing, at the end of a month, what happened and what it saved. That is what the monthly record is for.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.