How to use Hub Variables in apps

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.

14 Likes

Thanks Bruce, this will come in handy.

1 Like

I love the direction things are going.

1 Like

Current I think Hub Variables still require a connector to be used with Hub Mesh. Will this still be required going forward?

Would be easy enough to write an app (or a set of rules) to mirror variables across, but at this time I think you are correct.

If you create a variable in a user app, say at the initialization stage. What happens if that variable already exists? Which I can see happening if you modify the app.

Also, it appears you would still need a connector to access the variable in a dashboard? At least for now?

I'll test later tonight when I get home, but I would expect 1 of 2 things to happen:

  1. the 2nd create errors out
    or
  2. you get a pointer to the variable that already exists

Either could be valid, but if you try to create the second instance as a different variable type, i.e. Date/Time instead of BigDecimal, that could be an issue so the first option may be preferable.

Definitely need to add that to my test scenarios tonight.

I have a prototype of this working in webcore, so webcore can reference and use hub variables the same way as it does today with globals (and globals still work also)

2 Likes

We intend to get Hub Mesh to pass variables.

Same here, we will move to direct access for Dashboards.

5 Likes

Did a test. No errors. Variable was created and apparently it knew it already existed so no errors or anything when it executed the create the second time.

1 Like

We are removing creation and deletion of Hub Variables from the available methods for user apps. This should only be done in the Hub Variables setting.

5 Likes

Probably best as I can see me accidently creating a race condition that spits out a few million new variables if I haven't had enough coffee one morning. :coffee: = :sunglasses:

3 Likes

So hub variables are still not directly settable via user apps without a connector?

They are settable.

You cannot create them from a user app.

1 Like

You can most certainly set and read hub variables in apps now, just can't create our delete without using the normal interface.

I am too stupid for words

setGlobalVar(String name, Object value)

Obviously name needed to be enclosed in quotes!! Now working :slight_smile:

1 Like

Quotes have tripped me up so many times over the years....

Sorry to revive this old topic...but I just had to point out how absurdly difficult it was to actually find this thread. I googled so many permutations getting nowhere before finally googling "hub variables site:community.hubitat.com" and just clicking on every single link hoping for even a tiniest tidbit of information. Fortunately this thread was actually in the list several links down. Why is there no real developer documentation at all for apps/drivers/etc?

1 Like

This is not true:

https://docs.hubitat.com/index.php?title=Developer_Documentation

That being said, hub variables are yet to be documented there. There are some changes coming to the documentation in general at some point, and this may change after that. If not, sometime anyway, then...

In the meantime, lots of people are happy to point you in the right direction if you just ask. Googling is fine, too, but if you're struggling, people here tend to be friendly, so I wouldn't be worried. :slight_smile: Glad you found what you wanted, in any case!

3 Likes

How do I get the user to input which hub variable they wish the app to use, from the app setup screen? I know how to let them choose a connector...