Mastering a power outage in the smart home
Construct
But how does a sensor discover that the power line is down and the emergency generator has started up? It would be obvious to ask the controller itself, and in fact its web interface (Figure 3) shows whether it is running on emergency battery power or if the mains voltage is available. Unfortunately, SmartThings does not offer this information in the developer API [5].
I thus dreamed up a solution inspired by the master of complicated technical solutions, Rube Goldberg: What about an opaque distribution box from the hardware store, into which I route a power cable to power an LED nightlight inside consuming only 0.3W, as well as a battery-powered light sensor that checks whether the light, and thus the power, is on (Figure 4)? My choice of sensor was a device known as a Zooz (Figure 5), which acts as a motion detector and measures the room temperature and light exposure in lux.
The SmartThings hub pretty much integrates every device that speaks the wireless Z-Wave or ZigBee protocols; thus, integrating the sensor with the hub in the mobile phone app was easy as pie (Figure 6).
SmartApps
The SmartThings app for the mobile phone (iOS and Android) reads the sensor data at regular intervals or simply uses a subscription to be notified when predefined events occur. The Dashboard overview (Figure 7) shows the current state of all devices. Switches can also be flipped using an app – and the app displays their final state.
Any needs beyond this must be programmed by the user. Developers can paste together Groovy code to create so-called SmartApps, which query sensors, initiate actions with Z-Wave actuators, or fire off external web requests. SmartThings is tight-lipped when users ask where these SmartApps actually run: on the hub or in the cloud? Depending on the load, the company reserves the right to perform the necessary computational steps here or there.
A simulator in the IDE helps eradicate potential final teething troubles, and when you press Publish | For Me (Figure 8), the phone app installs the code previously edited in the desktop browser on the hub. Magic!
The preferences
section of the Groovy code in Listing 1 (lines 8-14) [6] narrows down the selection of sensors from which the user must choose after launching the newly installed SmartApp in Marketplace | My Apps. The script only expresses interest in devices with a motionSensor
capability, and the SmartThings app queries the hub for all devices with this property and presents a list of choices to the user.
Listing 1
zooz.groovy
If you select the newly added Zooz Z-Wave Plus Motion/Temp Sensor, the SmartApp launches, and the stunned developer can track the log output of the app running somewhere in the cloud in an any browser window (Figure 9).
Fixed Events
The code itself is event driven; the functions installed()
and updated()
are required entry points that the hub jumps to after the user reinstalls or updates the SmartApp. Listing 1 funnels both events into the initialize()
function in lines 24-27, which creates a cron entry that jumps to the handler()
function defined in line 29 every 42nd second of each minute. Before creating the new cron entry, unschedule()
deletes all previously created entries, just in case, to avoid an avalanche of new entries on frequent installs or updates.
The currentIlluminance
method of the sensor object in line 31 reads the light exposure value from the sensor, whose name it picked up previously in line 10. The fact that a string in line 10 generates an object in line 31 without so much as a by-your-leave is what is known in professional circles as "spooky action at a distance." Experienced programmers fear this effect like the devil fears holy water, but the SmartThings developer API is full of such folly, unfortunately.
As the log output in Figure 9 shows, it turns out that the hub only reads out the Zooz sensor every few minutes. Also the Zooz device does not seem to support a subscription mode offered by other devices, in which the hub immediately jumps to a callback when a sensor value changes.
When power fails, it can thus take five minutes until the code notices the event and therefore can't initiate actions immediately, such as notifying users via text message. This is not the end of the world, but there is a better way.
« 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
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.