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
-
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.