https://covidi.mccreight.com/shelly_scripts/shelly_plug_delayed_turn_on.notes.txt McCreight 09 Sep 2023 last updated 13 Sep 2023 These notes are meant to accompany most recent tested version of the Shelly Plus Plug S script https://covidi.mccreight.com/shelly_scripts/shelly_plug_delayed_turn_on_3.js.txt Overview The script is intended to auto-execute immediately after restoration of power following a power outage. The script was originally intended to wait for a fixed length of time and then turn the relay on. Feature creep has added optional conditions on the initial wait, optional text notifications about the power outage and recovery, and local status reporting via the Plug's lighted ring. Detailed Operation 1) Power is restored after a power outage. 2) The relay is turned off, just in case. (The Plug should also be configured to power-up with its relay off.) 3) There is 10-second delay, during which the lighted ring informs normally about IP address acquisition or other conditions. 4) Starting at the same time as step 3), there is a main delay, during which the lighted ring flashes between green and dark. The initial delay consists of a sequence of three configurable parts. The first part is of specified duration. The second part is also of specified duration, but ends when we receive a successful response from an Internet server. A specification of 10000 seconds or more for the second part means we wait for a successful response from an Internet server as long as necessary. The third part is also of specified duration. Then ... 5) Configurably, the relay is set to on, to off, or to its last setting before the power outage began. 6) The lighted ring changes so that "on" is signified by solid green; "off" by solid red. 7) Optionally, and if not already done once successfully, details about the power outage and subsequent recovery are posted to ntfy.sh for push notification to an unlimited number of smartphones or computers anywhere in the world. The details include the MAC address of the Plug, the last known operating time (in that Plug's configured time zone) and relay state before the power outage began (might be up to 5 minutes before the actual outage began), and the time and relay state after the recovery occurred. The kvs_rev field is the total number of KVS writes in the history of the Plug, to monitor for flash erase fatigue. There's no concern until that number exceeds a million. 8) If step 7) failed, wait five seconds and return to step 7). *) At any time during recovery or afterward, the relay can be toggled by the small button on the side of the Plug, or externally by RPC over WiFi. If that happens, besides the relay toggling, script control moves directly to step 6). **) The script should also work on a variety of other Shelly Plus/Pro devices. It simply omits lighted ring visual feedback if the device is not a Plus Plug. Script details The script has been tested with Shelly Plus/Pro firmware v1.0.3 (stable). The script uses two Timers (of its available five), two concurrent Shelly.call()'s (of its available five), a peak memory usage of 14,500 bytes (of a total of 25000 for all scripts), and has a text script length of less than 11,000 bytes (of its allowable 15,000 bytes). If desired, a few thousand bytes could be squeezed out of the text script length, and a bit less out of peak memory usage, by suppressing comments and excising low-priority console logging (using a host-based script).