Save Status from Thing State Events

Post date: Aug 04, 2015 9:7:47 PM

This example shows how to save the state of a device for later use in Tasker. If you are subscribed to multiple devices, you might find that you want to have a Tasker profile react to the current state of many devices at once. In order to do this, you can save the device status off to a global Tasker variable, so it is available for use in other profiles and tasks.

Note: This same approach can be used with the action "Thing: Get Attribute" within a profile. Immediately after triggering the Thing: Get Attribute action, you would setup a Variable Set action to copy data into a global variable.

When the 'Thing State' event triggers in Tasker, there are four variables that are set. Using a Porch Light as an example:

%st_attr_name = "switch"

%st_attr_value = "on"

%st_thing_name = "Porch Light"

%st_thing_id = "xxxx-xxxxx-xxxx-xxxxx" (some long ID)

Reference: http://sharptools.boshdirect.com/examples/event-thing-state

In order to make this data available in other profiles, you need to copy the data from the %st_attr_value to your desired permanent variable. For example:

Set Variable %PORCHLIGHT to %st_attr_value

There are a couple of options on how to do this conditionally:

Option 1: One option is to setup several "Thing State" events each with the configuration filters set to react to each individual thing and then have each Task set the desired variable (eg. %PORCHSTATUS) to the value from %st_attr_value. At the end of running through your variable copying tasks, you could then trigger the main tasks which would evaluate all the global variables you have set.

Option 2: Another option is to have one main "Thing State" event that does not have any filters applied to the plugin so it catches everything... then use conditional actions to transfer the data from the %st_attr_value to the appropriate variable. So like in the above scenario, you would use Tasker's "Variable Set" %PORCHSTATUS to %st_attr_value but only IF the %st_thing_name matched "porch light". You would setup an action for each thing that you want to transfer the variable for (eg. IF %st_thing_name matched "hall light" then set %HALLSTATUS to %st_attr_value, then at the end of the task, you would trigger the main tasks which would evaluate all the global variables you have set.

Keep in mind that local variables are only available during the execution of a task, so you should global variables so the data will be retained. From the Tasker variable guide:

Variables which have an all-lower-case name (e.g. %fruit_bar) are local, meaning that their value is specific to the task or scene in which they are used.

Variables which have one or more capital letters in their name (e.g. %Car, %WIFI) are global, meaning that wherever they are accessed from the same value is returned.