Creating hub variables

Hi all, Is it possible to create new hub variables via API or simply to set the value?

I'm trying to create a set of hub variables using setGlobalVar but it's returning false. If I manually create the variable then it returns true.

                def varName = [variablePrefix, deviceInfo.value.roomID, evt.name ].join('.')
                success = setGlobalVar(varName, evt.value)
                log.debug "deviceHandler: setting global variable: ${varName} to: ${evt.value} returned: ${success}"  

Have you tried checking the value later/in a subsequent run of your code?

I would think it likely that the variable is updated non-atomic/synchronously and only updates when the code exits its current run. Or it may have a cached value that is not updated until next run.

So checking it immediately after setting it may not yield the result you expect. Just a guess though - someone will correct me if I'm wrong.

No, a user must create these manually from Settings > Hub Variables. You can only update the value of an existing variable.

1 Like

It's not a timing issue. success returns "false" if the variable exists and "true" if it doesn't

Side note to @bertabcd1234 A few typos on the doc page for these functions ("Varibles"):

1 Like

Blarg! I just wasted a whole bunch of time!!!

Let me expound on what I was trying to do and see if there's another way. I have several apps that are looking at similar conditions all related to rooms. e.g. is the room occupied, what is the room temp? are the room lights on?

Ideally, it would be great if the native rooms were extensible so that I could add attributes to them (e.g. isOccupied, roomTemp, etc...) but short of that, I couldn't find a way to centrally share that information.

My plan was to dynamically create a set of global variables with a naming conversion room.#.param = val but I don't want to manually create each variable.

My other option is to create a virtual device but then I'd need to give every app permission to access that device.

Any other ideas?

1 Like

Not 100% sure what you're trying to do but it appears an app can create its own location events and other apps can subscribe to them.