How to use Hub Variables in apps

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...

You will need to create an input yourself, whether it be a free text box for the user to type the name or one of type: enum populated with the result you get from getGlobalVarsByType() or a similar method (I normally think the latter is a better user experience, but your app may differ). For an even better user experience, don't forget to register the variable as in use and un-register if not. :slight_smile:

Here's an app I wrote that uses the second approach, though it's not exactly a minimal example: https://github.com/RMoRobert/Hubitat/blob/master/apps/DimmerButtonController/DimmerButtonController4.groovy

1 Like
List vars = []
getAllGlobalVars().each{vars += it.key}
input "whichVar", "enum", title: "Choose a variable", options: vars
2 Likes