Integration guide
Alerts from a Shelly
A Shelly Gen2 or Gen3 relay has an input for a switch or sensor, an output that turns things on and off, and runs its own scripts. With a short script, the Shelly calls the Pingway API directly when something happens in the physical world: the alert reaches the person in charge, who confirms with one tap, and it is logged. No server in the middle and no app needs to be open.
API reference →What you can do
- Call button: reception, building entrance, assistance at a till or a room.
- Dry-contact sensors: door or window open, tank float switch, output of an alarm panel or a water leak detector.
- Power outage: alert when power comes back at a store, a cold room or a server room.
- Relay state: know when a pump, motor or gate was switched on, wherever the command came from.
- Power use (PM models): alert when a machine finishes its cycle.
- Light or siren until confirmed: the relay turns on a beacon and turns it off once someone confirms the alert on their phone.
What you need
- A Shelly Gen2 or Gen3 with script support (for example, Mini 1 Gen3 or Plus 1), on a Wi‑Fi network with internet. For the power-use recipe, a PM model (Mini 1PM Gen3, Plus 1PM, Plus Plug).
- A Pingway API key, created in the Console. Use one per device: if one is lost, you revoke that key without touching the others.
- The devices that will receive the alert, paired and sharing a tag (for example,
front-desk), or the ID of a single device.
How it fits together
Each script has two parts: the base block, always the same, which knows how to send the alert, and a recipe below it that decides when to send it. You can combine recipes that use different things (for example, "power is back" with any other), but not two that listen to the same input.
- Open the Shelly web UI (
http://<shelly-ip>, or from the Shelly app) and go to Scripts (the{ }icon) → Create script. - Paste the base block, fill in
API_KEYand the target (TAGSorDEVICE_ID), and paste the recipe you want below it. - Save, enable Run on startup and press Start.
Shelly "Actions" (webhooks) do not work for this: they only send GET requests without headers, so the API key would have to go in the URL. The script sends it in the Authorization header, which is what the API expects.
Base block
// ===== Pingway: base block =====
let API_KEY = "pw_acme_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// Target: if TAGS is not empty, every device with any of those tags
// gets it. If TAGS is empty ([]), only DEVICE_ID gets it.
let TAGS = ["front-desk"];
let DEVICE_ID = "";
let REQUIRE_ACK = true;
let HUB_URL = "https://hub.pingway.io/api/notifications";
let HEADERS = {
"Authorization": "Bearer " + API_KEY,
"Content-Type": "application/json"
};
// Sends an alert. done(ok, ids) is optional: ids are the IDs of the
// notifications created (one per device).
function notify(title, body, done) {
let payload = { title: title, body: body, requireAck: REQUIRE_ACK };
let url = HUB_URL;
if (TAGS.length > 0) {
url = HUB_URL + "/broadcast";
payload.tags = TAGS;
} else {
payload.toDeviceId = DEVICE_ID;
}
Shelly.call(
"HTTP.Request",
{ method: "POST", url: url, headers: HEADERS, body: JSON.stringify(payload), timeout: 10 },
function (res, errCode, errMsg) {
let ok = errCode === 0 && res.code >= 200 && res.code < 300;
if (!ok) {
print("Pingway error:", errCode, errMsg, res ? res.code : "");
if (done) done(false, []);
return;
}
print("Pingway", res.code, res.body);
if (!done) return;
let data = JSON.parse(res.body).data;
let ids = [];
if (data.id) {
ids.push(data.id);
} else {
for (let i = 0; i < data.notifications.length; i++) {
ids.push(data.notifications[i].notificationId);
}
}
done(true, ids);
}
);
}- With
TAGS, every device with at least one of those tags (up to 10) gets it, once per device. Each one confirms on its own. - With
TAGS = []andDEVICE_ID, only that device gets it. - If no device has those tags, the API answers
200withcount: 0and nothing is sent: check the tag is spelled exactly as in the Console.
Recipe: button or switch
For a reception, entrance or assistance call: every time someone presses the switch, the alert goes out.
- Input mode:
Buttonif it springs back (like a doorbell), orSwitchif it is a regular light switch. The recipe handles both. - Turn on Detached (or "Relay disconnected") so the relay does not toggle on every press.
// ===== Recipe: button or switch =====
let COOLDOWN_MS = 5000; // ignore repeated presses for 5 s
let lastSent = 0;
Shelly.addEventHandler(function (event) {
if (event.component !== "input:0") return;
// single_push: Button mode / toggle: Switch mode
let ev = event.info.event;
if (ev !== "single_push" && ev !== "toggle") return;
let now = Date.now();
if (now - lastSent < COOLDOWN_MS) return;
lastSent = now;
notify("Call at reception", "The button was pressed");
});Recipe: contact sensor
Any sensor that works as a contact that opens or closes: a magnetic door or window sensor, a tank float switch, the relay output of an alarm panel or a water leak detector.
- Input mode:
Switch, with Detached turned on. - The sensor must be a dry contact (no voltage of its own). If it outputs voltage, it needs a different kind of input.
// ===== Recipe: contact sensor (door, window, level, alarm) =====
let SENSOR = "Storage room door";
// Depending on whether the sensor is NO or NC, you may need to swap these.
let TEXT_ON = "Opened";
let TEXT_OFF = "Closed";
let NOTIFY_OFF = false; // true to also alert when it closes again
Shelly.addStatusHandler(function (status) {
if (status.component !== "input:0") return;
if (typeof status.delta.state !== "boolean") return;
let on = status.delta.state;
if (!on && !NOTIFY_OFF) return;
notify(SENSOR, on ? TEXT_ON : TEXT_OFF);
});Recipe: power is back
Works on any Shelly, with no extra wiring. When power comes back the Shelly boots, the script runs and sends the alert. Useful at stores, cold rooms or equipment rooms, where a long outage matters even if nobody is watching.
// ===== Recipe: power is back =====
// Runs when the Shelly boots. Retries until Wi‑Fi and internet are up.
let MAX_TRIES = 30;
let tries = 0;
function sendPowerBack() {
tries++;
notify("Power restored", "The site panel was without power", function (ok) {
if (!ok && tries < MAX_TRIES) Timer.set(10000, false, sendPowerBack);
});
}
Timer.set(5000, false, sendPowerBack);The Shelly cannot alert at the moment of the outage, because it loses power too. It alerts when power returns. Requires Run on startup.
Recipe: the relay changed state
To know when whatever is wired to the relay (a pump, a motor, a gate) turns on or off, whether from the app, a schedule or another automation. Every switch-on is logged.
// ===== Recipe: the relay changed state =====
let LOAD = "Water pump";
Shelly.addStatusHandler(function (status) {
if (status.component !== "switch:0") return;
if (typeof status.delta.output !== "boolean") return;
notify(LOAD, status.delta.output ? "Turned on" : "Turned off");
});If the relay switches often, set REQUIRE_ACK = false in the base block: it is informational and does not need each one confirmed.
Recipe: cycle finished
Only for power-metering (PM) models. Detects that a machine was running and then stopped drawing power for a few minutes: a washing machine, an oven, a production machine.
- Tune
RUNNING_Wby checking how much the machine draws idle and running (shown in the Shelly app).
// ===== Recipe: cycle finished (power-metering models) =====
let MACHINE = "Washing machine";
let RUNNING_W = 20; // above this, it is running
let IDLE_MS = 3 * 60 * 1000; // how long it must stay below to count as finished
let running = false;
let idleSince = 0;
Timer.set(30000, true, function () {
let w = Shelly.getComponentStatus("switch:0").apower;
if (w > RUNNING_W) {
running = true;
idleSince = 0;
return;
}
if (!running) return;
if (idleSince === 0) idleSince = Date.now();
if (Date.now() - idleSince < IDLE_MS) return;
running = false;
idleSince = 0;
notify(MACHINE, "Cycle finished");
});Recipe: light or siren until someone confirms
When the switch is pressed, the relay turns on a beacon or siren and the alert goes out. The beacon stays on until someone confirms on their phone, then turns off by itself, so people on site can see the call was handled.
- Input mode:
ButtonorSwitch, with Detached turned on: the script drives the relay. - The beacon or siren is wired to the relay output.
// ===== Recipe: light or siren on until someone confirms =====
// Requires REQUIRE_ACK = true in the base block.
let POLL_MS = 10000;
let MAX_ON_MS = 10 * 60 * 1000; // turns off anyway after 10 min
let pending = [];
let pollIdx = 0;
let pollTimer = null;
let startedAt = 0;
function stop(reason) {
if (pollTimer) Timer.clear(pollTimer);
pollTimer = null;
pending = [];
Shelly.call("Switch.Set", { id: 0, on: false });
print("Siren off:", reason);
}
// Checks one notification at a time to keep the Shelly light.
function poll() {
if (Date.now() - startedAt > MAX_ON_MS) return stop("timeout");
let id = pending[pollIdx % pending.length];
pollIdx++;
Shelly.call(
"HTTP.Request",
{ method: "GET", url: HUB_URL + "/" + id, headers: HEADERS, timeout: 10 },
function (res, errCode) {
if (errCode !== 0 || res.code !== 200) return;
if (JSON.parse(res.body).data.status === "ack") stop("ack");
}
);
}
Shelly.addEventHandler(function (event) {
if (event.component !== "input:0") return;
let ev = event.info.event;
if (ev !== "single_push" && ev !== "toggle") return;
if (pollTimer) return; // an alert is already in progress
Shelly.call("Switch.Set", { id: 0, on: true });
notify("Assistance at till 2", "Confirm to turn the light off", function (ok, ids) {
if (!ok || ids.length === 0) return stop("could not send");
pending = ids;
pollIdx = 0;
startedAt = Date.now();
pollTimer = Timer.set(POLL_MS, true, poll);
});
});With TAGS, it turns off as soon as any recipient confirms. If nobody does, it turns off anyway after 10 minutes.
Test it
With the script page open, trigger the event. The console should print Pingway 201 followed by the API response, and the alert reaches the devices. Pingway error means the Shelly could not reach the API: check its internet connection. A 401 means a wrong API key.
Day to day
- The phone or app you used to set up the Shelly plays no part: it can be off. The Shelly only needs power, Wi‑Fi and internet.
- If a receiving device is off or out of signal, the alert stays pending and is delivered when it reconnects, within 24 hours.
- If internet drops right when the event happens, that alert is lost (except in the "power is back" recipe, which retries).
Security
The API key is stored on the Shelly. Without a password, anyone on the same Wi‑Fi can open the web UI and read the script. Turn on authentication in Settings → Authentication, and use an API key dedicated to each device.