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.
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
power-sensor.groovy
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
prowl.groovy
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).
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
webapi.groovy
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
).
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.