A Smart-Button to Rule Them All

How to use a Flic Button to control your SmartThings Home Automation system.

A Smart-Button to Rule Them All

A really useful Home-Automation (HA) system should be invisible, predictive and allow people to seamlessly interact with their homes. While few notable companies are working on HA systems that rely on voice recognition and Artificial Intelligence (AI Home Assistants), most of the available solutions still depend on using smartphone Apps.

That is the case of my SmartThings (ST) setup and also the source of most of my problems. I don’t carry my phone with me at home, hence interacting with my digital home entails finding my iPhone, unlocking it, searching for the SmartThings App and finally activating the right smart device. This requires a lot of time and interactions. While this is not a problem when I remotely operate my garage (I'm the proud owner of a Smartphone-Controlled Garage Door), it is way too complicated for turning on/off a simple light bulb. Few months ago I installed a ST SmartPower Outlet in my bedroom to operate a floor lamp. However I soon grew frustrated with having to use my phone and I stopped using it. This blog post talks about how I solved this problem.

The Flic is a Smart-Button that connects to smartphones via Bluetooth. It has its own battery, it is quite small and comes in different colors. Pressing the Flic Button activates the Flic smartphone App which, in turn, can talk to different Apps and do things for you. If you want to know more about it, check out the Flic website.

On paper, operating my floor lamp using a Flic Button looked easy, but I was proven wrong. The first issue that I ran into was that the Flic and SmartThings Apps cannot directly talk to each other. To make them communicate I had to use IF-This-Then-That (IFTTT), a third-party service. The second issue I found was that the ST Outlet does not allow toggle operations, but it only understands ON/OFF commands. Because IFTTT is unable to read anything from a ST Outlet there was no way to know whether pressing the Flic Button should correspond to an ON or OFF command.

Long story short… to get the system to work, I had to concatenate five different devices/services: the Flic Button triggers IFTTT which pushes a SmartThings “virtual” push-button, a software-only device that has no counterpart in the physical world. The virtual push-button triggers a ST SmartApp which reads the status of the ST Outlet and issue the right ON/OFF command to perform the toggle operation. Below are the 5 steps required to replicate my solution.

Five different services need to be concatenated to be able to operate my SmartThings Outlet using a Flic button.

1. Create a SmartThings Virtual Momentary Switch

Login on http://developer.smartthings.com/ and click on “My Devices”. Create a new device of type “Momentary Button Tile”. Choose a device network id of your liking, use “Published” for the Version field and select the appropriate Location and Hub.

Add the virtual button you just created in the SmartThings service linked to your IFTTT account, then create a new IFTTT Applet that switches ON the SmartThings virtual Push-Button when the Flic Button is pressed.

3. Create a new SmartThings SmartApp

The virtual push-button is linked to the real outlet switch via a custom ST SmartApp. From within the ST developer website click on “My SmartApps” and select “New SmartApp”. Select the “From Code” tab and paste the code from my GitHub repository or from the embedded code window below. Click “Create” then “Publish” then ”For Me”.

/**
 *  Toggles a Smart-Outlet with a Virtual Button
 *
 *  Copyright 2016 Fabrizio Guerrieri
 *
 */

 // General info about the app (name, author, description, icons...)

definition(
    name: "toggles-smart-outlet-w-virtual-button",
    namespace: "fabrguer",
    author: "Fabrizio Guerrieri",
    description: "Toggles a Smart-Outlet with a Virtual Button",
    category: "",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")

// Instantiates two objects used to represents a Trigger device (the virtual push-button that is "pushed" 
// by the Flic button) and an Actuator (the real SmartThings smart-outlet switch commanding my floor lamp).

preferences {
    section("Trigger") {
        input "virtButton", "capability.momentary", title: "Which Virtual Button?", required: true
    }
    section("Actuator") {
        input "realSwitch", "capability.switch", title: "Which Real Switch?", required: true
    }
}

// Executed once when the app is installed

def installed() {
    initialize()
}

// Executed anytime the app is updated

def updated() {
    log.debug "Updated with settings: ${settings}"

    unsubscribe()
    initialize()
}

// When the virtual button is pushed, the callback function virtButtonHandler is called

def initialize() {
    subscribe(virtButton, "momentary.pushed", virtButtonHandler)
}

// Toggles the realSwitch (reads its current state and inverts it)

def virtButtonHandler(evt) {
    if (realSwitch.currentState("switch")?.value == "on") {
        realSwitch.off()
    } else {
        realSwitch.on()
    }
}

4. Connect the SmartApp to your SmartThings devices

Open the SmartThings App on your smartphone. Click the “Automation” icon on the bottom of the screen, select “Add a SmartApp”. Find your SmartApp in the “My Apps” category. If you haven’t changed any of the code, the App will be called “ToggleSwitch”. After tapping the App, you will have to connect a trigger (the virtual push-button) and an actuator (the SmartThings Outlet) to the App. Click “Done” to complete.

5. You are all set!

You should now be able toggle ON/OFF your SmartThings Outlet using the Flic Button.

Overall I’m happy by the final result as it considerably improves my “experience” of turning ON/OFF my bedside floor lamp. Three issues are worth noting. The first is the lag: considering all the services I had to chain over the cloud to make this works, it is not surprising to see a delay of the response in the order of 5 to 10 seconds. The second inconvenience is that the automation works only if my phone is within Bluetooth range of the Flic Button. This is not a major problem for me. Lastly I wish the Flic Button was a little cheaper: at $34 each, it pushes my “light-bulb automation system" cost to almost $100… This makes it prohibitive to adopt the same approach for more than a handful of devices in the house.