This is a brief introduction to how to use Hub Variables in user apps.
Please note: expect for Variable Connectors to be phased out in favor of direct access to Hub Variables. Existing connectors will, of course, continue to work. But, the ability to create new ones will be deprecated at some point.
Hub Variables Methods
GlobalVariable (Object) getGlobalVar(String name)
GlobalVariable: [name: xx, type: xx, value: xx, deviceId: xx, attribute: xx]
Note: value is already cast to created datatype, Integer || BigDecimal || Boolean || String || DateTime
deviceId and attribute will be deprecated as connectors are phased out
Boolean setGlobalVar(String name, Object value)
Boolean addValueToGlobalVar(String name, Object value)
Note: global var type must be integer or bigdecimal
Map getGlobalVarsByType(String type)
where type is: "integer" || "bigdecimal" || "boolean" || "string" || "datetime"
Map getAllGlobalVars()
Note: map result set for getGlobalVarsByType and getAllGlobalVars is
[ globalVarName:[type:xx, value:xx, deviceId,xx] ,... ]
deviceId will be deprecated as connectors are phased out
There are two more methods that allow an app to be shown in the In Use By part of the Hub Variables settings page, and for a warning to be given before deletion if still in use by an app. The hub marks the app that makes these calls as the relevant app:
Boolean addInUseGlobalVar(String name)
Boolean addInUseGlobalVar(List names)
Note: adds calling app to in use list for variable or list of variables
Boolean removeInUseGlobalVar(String name)
Boolean removeAllInUseGlobalVar()
Note: removes calling app from in use list for variable, or all variables
The Boolean value method returns designates success or failure of those methods.
Hub Variable Events
Hub Variable Events are sent via Location Events. These events are logged on the Location Events tab of the Logs page (see below for examples). These events can be subscribed to in two ways:
subcribe(location, "variable:variable-name", handler)
subcribe(location, "variable:variable-name.value", handler)
The first example subscribes to all events for "variable-name". The second example subscribes to events for "variable-name" that have "value" as the event value.
Here are two log examples:
DateTime Variables
DateTime variables are strings in this date format: "yyyy-MM-dd'T'HH:mm:ss.sssXX", for example: 2021-10-13T14:25:09.009-0700
If the date is blank, the string is like this: 9999-99-99T14:25:09.009-0700. Built-in app UIs would display this as just a time. Code should check for these to not blow up time methods. If time is blank (i.e., a date), the string is like 2021-10-13T99:99:99:999-9999. Built-in app UIs would display this as just a date. Generally you can test for these with s.startsWith("9999") or s.endsWith("9999"). The app UI should render these time strings to the appropriate presentation.