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

Figure 3: The hub indicates whether it is running on mains voltage or on battery power during an outage.

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.

Figure 4: An LED nightlight is lit as long as the power is on, and the sensor indicates that the juice is flowing.
Figure 5: The battery-powered light-motion-temperature sensor by Zooz speaks Z-Wave with the controller.

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

Figure 6: The hub often finds new Z-Wave components automatically in inclusion mode.

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.

Figure 7: The SmartThings app displays the state of all devices and controls them individually.

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!

Figure 8: Users can write their own applications for the SmartThings hub in an IDE in Groovy and then install them by pressing Publish.

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

Figure 9: The hub queries the light sensor every four minutes, whereas events on the contact sensor appear immediately.

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.

Buy this article as PDF

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

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
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

News