Shelly Logo Squared

Script Shelly scripts

Phone alert when your fridge or freezer stops cooling

A Shelly that measures power watches the compressor switch on and off, and messages your phone through ntfy when it stops starting or won't stop running.

Updated v1.1

  • Gen2
  • Gen3
  • Gen4
  • Pro
fridge-stopped-alert.js · 149 lines Download .js
// Fridge or freezer stopped alert: watches the compressor's power and messages your phone// through ntfy when it stops starting, or runs for hours without a break.// Needs a Shelly relay that measures power, left on with the fridge plugged into it. let CONFIG = {  applianceName: "Fridge",  switchId: 0,  runningWatts: 30,  maxOffHours: 3,  maxRunHours: 4,  ntfyTopic: "",}; // Power is read this often. A compressor runs for minutes at a time, so this is plenty.let CHECK_SEC = 30;// Readings in a row before the compressor counts as started or stopped (1 minute),// so a door opening or a defrost blip doesn't count as a cycle.let CONFIRM_READINGS = 2; let running = false;let since = 0;let pendingN = 0;let longAlerted = false;let offAlerted = false;let queue = [];let sending = false; function onFlip(nowRunning, now) {  let lasted = now - since;  if (nowRunning) {    print(CONFIG.applianceName + ": compressor started after " + minutes(lasted) + " min off.");    if (longAlerted) alert(CONFIG.applianceName + ": the compressor has started again.");  } else {    print(CONFIG.applianceName + ": compressor stopped after running " + minutes(lasted) + " min.");    if (longAlerted) alert(CONFIG.applianceName + ": the compressor has stopped for a break again.");  }  running = nowRunning;  since = now;  longAlerted = false;} function onLoad() {  let st = readOutput();  if (st === null) {    print("There's no output " + CONFIG.switchId + " on this Shelly. Check switchId.");    return;  }  if (typeof st.apower !== "number") {    print("This output doesn't measure power, so the script can't watch the compressor.");    return;  }  if (CONFIG.ntfyTopic === "") print("No ntfy topic set. Alerts show in this console only.");  running = st.output && st.apower >= CONFIG.runningWatts;  since = uptime();  Timer.set(CHECK_SEC * 1000, true, check_a011);  print("Fridge alert started for " + CONFIG.applianceName + ". Compressor is " +    (running ? "running" : "off") + " now (" + Math.round(st.apower) + " W).");} function readOutput() {  return Shelly.getComponentStatus("switch", CONFIG.switchId);} // "45 min" under an hour, "3.5 h" from then on.function spanWords(sec) {  if (sec < 3600) return minutes(sec) + " min";  return (Math.round(sec / 360) / 10) + " h";} function sendNext() {  if (sending || queue.length === 0 || CONFIG.ntfyTopic === "") return;  sending = true;  Shelly.call("HTTP.Request", {    method: "POST",    url: "https://ntfy.sh/" + CONFIG.ntfyTopic,    body: queue[0],    headers: { "Content-Type": "text/plain" },    timeout: 15,  }, onSent);} function uptime() {  return Shelly.getComponentStatus("sys").uptime;} function minutes(sec) {  return Math.round(sec / 60);} function onSent(res, err) {  sending = false;  if (err === 0 && res.code === 200) {    queue.splice(0, 1);    sendNext();  } else {    print("Couldn't reach ntfy. Will try again after the next check.");  }} function alert(msg) {  print(msg);  if (queue.length >= 3) queue.splice(0, 1);  queue.push(msg);} function check_a011() {  let st = readOutput();  let now = uptime();  if (!st.output) {    // The relay itself is off, so the fridge has no power at all.    if (!offAlerted) {      alert(CONFIG.applianceName + ": the Shelly has switched its power off. Turn it back on in the Shelly app.");      offAlerted = true;    }    since = now;    pendingN = 0;    longAlerted = false;    sendNext();    return;  }  if (offAlerted) {    print(CONFIG.applianceName + ": power is back on.");    offAlerted = false;  }  let nowRunning = st.apower >= CONFIG.runningWatts;  if (nowRunning !== running) {    pendingN++;    if (pendingN >= CONFIRM_READINGS) {      pendingN = 0;      onFlip(nowRunning, now);    }  } else {    pendingN = 0;  }  let lasted = now - since;  if (!longAlerted && running && lasted >= CONFIG.maxRunHours * 3600) {    alert(CONFIG.applianceName + ": the compressor has run for " + spanWords(lasted) +      " without a break. Check the door is shut. If it is, the fridge may not be cooling.");    longAlerted = true;  }  if (!longAlerted && !running && lasted >= CONFIG.maxOffHours * 3600) {    alert(CONFIG.applianceName + ": the compressor hasn't started for " + spanWords(lasted) +      " (drawing " + Math.round(st.apower) + " W). Check the fridge is still cold.");    longAlerted = true;  }  sendNext();} onLoad();

What it does

A fridge's compressor switches on and off all day, and the Shelly sees each start as a jump in power. The script times each run and each break. If the compressor hasn't started for three hours, your phone gets a message such as "Garage freezer: the compressor hasn't started for 3 h. Check the fridge is still cold." If it has run for four hours without stopping, the message says to check the door is shut, as a door left ajar or a fridge that has stopped cooling both keep it running. When the compressor goes back to its normal pattern, you're told that too.

Each start and stop has to hold for a minute before it counts, so opening the door or a defrost cycle doesn't set it off. The script never switches the fridge off. If the relay itself gets switched off, from the app or a schedule, it tells you straight away. A power cut stops the Shelly as well, so the power outage report script is the one for that. An inverter fridge runs its compressor slowly for long stretches, so raise maxRunHours for one, after a day of watching the console.

Install it on your Shelly

View the full install guide

Before you install

  1. Set the relay to come back on after a blackout

    In the Shelly app, open the relay's settings and set its power-on default to On, so the fridge restarts when the power returns.

  2. Find the running and idle draw

    Watch the relay's power reading in the app for half an hour. The compressor shows as a jump to a steady figure, often 50 to 150 W, and the idle figure is a few watts for the light and controls. Set runningWatts about halfway between.

  3. Subscribe in ntfy

    Install the ntfy app and subscribe to a topic name nobody would guess, such as smith-freezer-4821.

Install the script

Install it on the relay the fridge is wired to.

Shelly Smart Control

  1. Open the device and tap Scripts

    In Shelly Smart Control, open the room, then the device. Tap Scripts, the { } icon in the menu down the left.

  2. Tap Create new script

    If the device has no scripts yet, the screen says No scripts created.

  3. Name it, paste the code, then tap Save

    Name it fridge-stopped-alert. Tap in the code area, paste the script you copied above, then tap Save (the disk icon at the top right).

  4. Tap the play button

    The play button lights up once the script is saved. Tap it and a message says the script has been started.

  5. Close the editor and turn on Run on startup

    Back on the Scripts screen, turn on Run on startup next to your script, so it starts again after a power cut or a firmware update.

Direct access via IP

  1. Open the device's web page and click Scripts

    Find the IP address in the Shelly app under Device settings, then Device information, and type it into a browser on the same Wi-Fi. Click Scripts in the menu on the left.

  2. Click Create script

    If the device has no scripts yet, the list says so.

  3. Name it, paste the code, then click Save

    Name it fridge-stopped-alert. Click in the code area, paste the script you copied above, then click Save (the disk icon above the code).

  4. Click Run and check the console

    Run appears once the script is saved. The console under the code then prints what the script is doing.

  5. Turn on Run on startup

    Click Scripts again and turn on Run on startup next to your script, so it starts again after a power cut or a firmware update.

After you install

  1. Check it sees the compressor

    On the device's web page, the console prints a line such as Garage freezer: compressor started each time it starts, and stopped with how long it ran. If it never prints one, runningWatts is set too high.

  2. Test the alert

    Stop the script, set maxRunHours to 0.05, save it and run it again. Within five minutes of the compressor starting, your phone gets the long-run message. Set it back afterwards.

Changelog

v1.1 Tidied the internal function names. It works exactly as before.
v1.0 First release.

Still need a hand?

We answer most messages within one business day. If your question is about an existing order, have your order number ready. It speeds things up.

Contact us