getGlobalVar not found on location

Hi Guys,

A noob question. I am trying to get the value of a global variable. In order to do so I am trying to use the Hub Variable API and the getGlobalVar (String) method as described here: Hub Variable API | Hubitat Documentation

The app script compiles ok but I am getting the following error:

Method getGlobalVar not found on location object: No signature of method: com.hubitat.hub.domain.Location.getGlobalVar() is applicable for argument types (java.lang.String) values: [outside_current_temp_gv]

Is 'Method getGlobalVar not found on location' indicating that there is no method called getGlobalVar ?

What am I missing? What am I doing wrong?

Cheers,

The parts of the app code related to getting the global variable:

def initialize() {
log.debug "Initializing"
// Fetch a hub variable
fetchHubVariable()
// Fetch immediately on initialization
schedule("0 * * * * ?", fetchHubVariable) // Fetch every minute for demonstration purposes

// Method to fetch the value of the static hub variable and log it
def fetchHubVariable() {
def outsideTempGV = getGlobalVar("outside_current_temp_gv")
if (outsideTempGV != null) {
state.currentValueOutsideTempGV = outsideTempGV.value
log.debug "Current outside temperature from hub variable is: ${state.currentValueOutsideTempGV}"
} else {
log.warn "Global variable 'outside_current_temp_gv' not found."
}
}

// Helper method to get the value of the global variable by name
def getGlobalVar(varName) {
def globalVar = location.getGlobalVar(varName)
log.debug "Global variable fetched: ${globalVar}"
return globalVar
}

Is this an app or a driver? It's not available on drivers.

Hi,
Sorry, It's an app.

The getGlobalVar API is not part of the location object. You call it directly. Remove your bottom helper function and it should work as is.

1 Like

It did! Thank you, thank you, thank you @ritchierich !! :smiley:

1 Like

@ritchierich ,

Can I ask you a follow up question?

When the code was working fine with the solution you suggested, I first added a second parameter in the same method, thinking I could fetch anotherone. But that did not work, I got an error. So I then removed the second variable code and copied the working code and created a basically identical method with only the variable names replaced. Much to my surprice, the second method gets an error. That was strange I thought. Any ideas on why that happened?

(the only difference between the two hub variables was that the first one was of type 'Decimal' and the second one 'Number'. But I changed that and replaced the number one with a decimal one. I don't think it matters.)

Log file:

Code:

Hubvariables:
image
image

The value property should not have an uppercase V (nor do conventional property names in Groovy, though it won't stop you....).

2 Likes

Thank you @bertabcd1234 ! I did not know that. That did the trick and now it works!

Very much appreciated! Again, Thank you!

image

1 Like