Remembering To Close My Garage Door With Twine and Webscript

I just got my Twine in the mail this weekend. Twine is a neat little battery-driven device from Supermechanical that can monitor its environment and report changes via WiFi to the corporate mother ship. On their website, you can choose events to watch for and you can choose from a variety of simple consequences. Fortunately, one consequence is a webhook call to the URL of your choice. For my application, I created a Webscript handler, of course.

Every so often, I leave the garage door up. Each time it happens, I wish that there were something that would alert me after the door has been up for more than a few minutes. With a Twine and Webscript, this is a simple job.

To determine if the garage door is up or down, I attached the Twine to my garage door and configured the Twine to alert me when it changes orientation. Duct tape did the trick for attaching the Twine:

/images/twine-photo.jpg

Twine supports a template-driven URL specification, so I include the new orientation in a query parameter: http://twine.webscript.io/change?orientation=[orientation]. (Don't forget the "http://" or Twine will not like your URL.)

This configuration is easily done on the Twine website:

/images/twine-screen-shot.png

I created one rule for each of the six Twine orientations, but they all target the same URL.

That's all I needed to do with the Twine. The rest is all Webscript's responsibility.

I use two scripts: one to handle the change events from the Twine, and another one to periodically check the status and report if the door has been up for too long.

The script for orientation change is quite simple. The script simply records the new orientation (passed as a query parameter) and the time of the change. The script also clears a flag to determine if an alert has already been sent for this orientation.

twine.webscript.io/change

storage.orientation = request.query.orientation
storage.alerted = "false"
storage.time = os.time()
return "ok"

The script that determines if the door has been up for too long checks to see how long the Twine has been in the "bottom" orientation. This means that the "bottom" of the Twine is up, which is how I taped the Twine to the door. An alert is only sent if the orientation is correct, it's been in that orientation for too long, and no alert for this has already been sent.

twine.webscript.io/check

local THRESHOLD = 5 * 60 -- 5 minutes

local orientation = storage.orientation
local alerted = storage.alerted
local since = tonumber(storage.time or 0)
local toolong = os.time() - since > THRESHOLD
if orientation == "bottom" and alerted == "false" and toolong then
        alert.sms("Garage door is up!")
        alert.email("Garage door is up!")
        storage.alerted = "true"
end
return { orientation=orientation, alerted=alerted, since=since }

I use Webscript's cron facility to run this checking routine periodically.

I am also using Webscript's new "alert" feature, which gives users with paid accounts the ability to send emails and SMS messages to themselves without using an external service.

Because return values are logged automatically by Webscript, I have chosen a verbose return value to help with any needed debugging.

I think the Twine is a really cool device and am very glad to have one. I also think there are many fun and interesting ways to pair a Twine and Webscript. Please let us know what you come up with. As always, we can be reached at support@webscript.io.