Mastering a power outage in the smart home

Another Sensor

On Amazon, I found another Z-Wave sensor by seven7express (Figure 10) that reports power outages very efficiently. It is implemented as a door sensor and reports closed if the power is on and open if not. It is connected to a power outlet via a charger/adapter, whereas the sensor itself has an internal battery that allows it to send signals to the hub via Z-Wave if the wall socket stops supplying power.

Figure 10: The battery-powered power outage sensor by seven7express shows the user whether or not power is available as a door sensor.

The device worked right off the bat. The Groovy script in Listing 2 implements the logic for controlling it. In the preferences section, the hub looks for sensors with the capability.contactSensor property and offers the user the newly installed power sensor, among other things, to select from when you launch the app. Line 26 obtains a subscription to the contact event, which the hub triggers each time the sensor transitions from open to closed and vice versa. The script thus notices power outages with only one- or two-second delays and logs them in lines 31 and 33.

Listing 2



Hello User!

Once the SmartApp determines that the power has gone, it uses the Prowl Web API, as described in a previous article [7], to notify the user who has installed the Prowl app on their cell phone. Listing 3 plants the API key required by Prowl, which registered users can pick up from the website [8] after purchasing the app for $3.

Listing 3



The prowl() function expects a message (Power outage! or Power back!) passed in, sets the event field with it, and adds the name of the sending app and a short explanation so the receiver knows where the message came from. In the try block, httpGet then issues the web request and checks whether the outcome is good or bad, and log.debug logs the message for later error analysis. Prowl is a simple and efficient service that works both on iOS and Android (Figure 11).

Figure 11: The text message tells the user about a power outage at home.

Web Access

If you enable the slightly hidden OAuth section when setting up the SmartApp on the SmartThings website, you will receive an OAuth token after an OAuth token dance (e.g., via the CPAN OAuth::Cmdline module; Figure 12); you can then access the running SmartApp from the Internet via a web API. The mappings section of the SmartApp code in Listing 4 defines the entry points of the web API and the actions assigned to them. The checkPower() function defined in lines 9-13 then reads the current state of the sensor on demand and returns the result as a map, which the web API then returns to the web client in JSON format.

Listing 4



Figure 12: The user can allow web access to selected Z-Wave devices.

Figure 13 shows the query using a curl client at the command line. First, it asks for the location of the endpoints stating the API token (i.e., the complete URL under which a registered user can find their SmartApps). Armed with this URL, the client can access the entry points defined in the code (in this example, /power) and receive JSON-formatted output in return (e.g., the value in the power field is ok or not ok).

Figure 13: Curl at the command line using the web API to access the power outage sensor.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • AcpiTool and PowerTOP

    AcpiTool and PowerTOP ensure a sustainable battery lifetime and guarantee low energy consumption of laptops running Linux by identifying power hogs and maintaining the battery.

  • Smart Cities

    Making the city of Messina, Italy, smarter with open source and IoT.

  • Wildlife Photo Trap

    Armed with no more than a Raspberry Pi photo trap, you can discover who pays a visit to your garden at night.

  • Z-Wave Home Assistant

    Z-Wave components, a RaZberry module, and the free Home Assistant software make the Raspberry Pi a powerful smart home control center.

  • Perl: Uninterruptible Power Supply

    An uninterruptible power supply can help get you through a short power outage without losing data or damaging hardware. A Nagios script written in Perl checks UPS health and initiates a controlled powerdown if the unit exhausts its battery capacity.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More