Custom SmartThings Widgets

Post date: Feb 15, 2015 10:20 PM

In this tutorial, we will show how to use SharpTools and Tasker to create a custom widget that displays Thing state in Android. If you are looking for a quick and easy way to control your Things without Tasker and without showing the current state, the built-in widget 'A Thing' is ready-to-go right out of the box.

You might also be interested in the following SharpTools articles:

Requirements

    • Tasker - automation for Android

High-Level Approach

    1. Add a 'Task' widget to the Android homescreen

    2. Configure the action to perform when the widget is tapped

    3. Subscribe to an attribute of the Thing

    4. Change the icon displayed on the widget when the Thing state changes

Instructions

Create the Widget + Action

    1. Follow the steps for your launcher to pick a Widget to be added to your homescreen

    2. Note: Many launchers display the list of widgets after a long press of the homescreen or within the app drawer. Consult the manual for your homescreen or install the Google Now Launcher.

    3. Select the 'Task' widget

    4. Select the 'New Task' option

    5. Enter the name 'Light Toggle' for the Task and click OK to continue.

    6. Note: You must enter a name here as we will use it later to change the Widget icon based on the Thing state changing.

    7. Tap the '+' icon at the bottom of the screen to add a new Action

    8. Select Plugin > SharpTools > A Thing

    9. Click the pencil icon on the Configuration line

    10. Select the device type, device, and command you would like to perform when the widget is tapped. For this example, we are going to use the 'toggle' command of a light.

    11. Tap apply to save the 'A Thing' settings

    12. Press the back button of your Android device to save the Action

    13. In the bottom-right corner of the Widget configuration, select the 'icon-grid' button to select an icon to be displayed on the widget.

    14. Select 'Built-In Icon' and select the Panda Bear.

    15. Press the back button of your Android device to save the Widget

At this point, you should now see a Panda Bear widget on your homescreen and if you tap this icon, the selected light should toggle on and off.

Subscribe to an Attribute

    1. Launch SharpTools

    2. Scroll down to your desired 'Thing'

    3. Note: In this example, we are using the Upstairs Hall light.

    4. Long press on the Thing name and a Subscription menu should pop up

  1. Subscribe to the desired attribute by tapping it

  2. Note: In this example, we are interested in when the light turns on and off, so we will use the 'Subscribe to switch' option.

  3. The Thing should now show a star in the top-right corner of the card

Create the State-change Context

    1. Launch Tasker

    2. From the Profiles tab, click the '+' icon at the bottom of the screen

    3. Select Event > Plugin > SharpTools > Thing State

    4. In the Thing Name field, enter the name of the subscribed Thing

    5. In the Attribute Name field, enter the name of the subscribed attribute

    6. Click Apply to save the Thing State settings

    7. Note: several variables are saved in Tasker whenever the Thing State event is triggered. We will use these variables in the following steps to determine how to change the widget icon.

    8. Press the back button on your Android device to save the event

    9. Select the 'New Task' option when prompted

    10. Optionally provide a Task name and tap the checkmark to continue

    11. Tap the '+' icon at the bottom of the screen to add a new action

    12. Select Tasker > Set Widget Icon

    13. Tap the search icon (magnifying glass) and the select the widget 'Light Toggle' that we created earlier

    14. Tap the 'icon-grid' button to select an icon to be used for the 'on' condition

    15. For demonstration purposes, we used the colored Tasker icon from the bottom of the 'Built-in Icons' list.

    16. Tap the '+' button in the If section to add a conditional

    17. Set the conditional to: %st_attr_value ~ on

    18. Note: The left hand side of the conditional if the variable which we will evaluate. This is the 'Attribute Value' variable from step 6. The '~' indicates we will be performing a 'Matches' condition. And the 'on' indicates we will look for the word 'on' in the attribute value - make sure this is all lowercase.

    19. Press the back button on your Android device to save the Action

    20. Tap the '+' icon at the bottom of the screen to add another action

    21. Select Tasker > Set Widget Icon

    22. Tap the search icon (magnifying glass) and the select the widget 'Light Toggle' that we created earlier

    23. Tap the 'icon-grid' button to select an icon to be used for the 'off' condition

    24. For demonstration purposes, we used the grayed out Tasker icon from the bottom of the 'Built-in Icons' list.

    25. Tap the '+' button in the If section to add a conditional

    26. Set the conditional to: %st_attr_value ~ off

    27. Press the back button on your Android device to save the Action

    28. Press the back button on your Android device again to save the Task

    29. Press back one more time to exit Tasker and activate the profile

Closing Comments

In this article, we demonstrated how to setup a 'Task' widget to toggle a light then how to subscribe to an attribute in SharpTools and react to that change in Tasker. Putting all this together, we now have a widget that toggles a light and displays the current state of the device.

In the example, we used a somewhat boring built-in Tasker icons, but I implore everyone to get creative with your use of widgets. Perhaps you want to have an actual picture of the light showing the status of the device; Or perhaps you might use a custom graphic or icon pack; Or perhaps even use KWGT to create a completely custom Widget.

Troubleshooting

    • Make sure you have subscribed to the appropriate attribute of the right device inside the main SharpTools app. You can tell if you are subscribed to a device as a star will show up in the top-right corner of the card.

    • Double check the data and spelling in the 'Thing State' event.

      • For troubleshooting purposes, try clearing the Thing Name and Attribute Name fields which will make the plugin match all subscribed attribute events.

    • If you receive the message "Error: The disclaimer has not been accepted" when trying to add a Task widget, launch the Tasker app and accept the disclaimer that is shown.