Help with setting / accessing global variable in app

I'm simply trying to read and write some hub variables in an app. I'm getting this error:

2021-12-20 02:17:00.060 pm errororg.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'null' with class 'null' to class 'int'. Try 'java.lang.Integer' instead on line 92 (method midnightTask)

I have set the variable to a value in the Hub Variables page. I must have something very basic wrong. Code below. I have commented where line 92 is. I appreciate any help with this, or with anything you see that could be done better! Thanks!



preferences {
page(name: "pageConfig")

def pageConfig() {
dynamicPage(name: "", title: "", install: true, uninstall: true, refreshInterval:0) {

section("Inputs") {
    input "pauseSchedule", "capability.switch", title: "Pause Schedule", required: true
    input "inShopToday", "capability.switch", title: "In Shop Today", required: true
    input "inOfficeToday", "capability.switch", title: "In Office Today", required: true
    input "runningNightShift", "capability.switch", title: "Running Night Shift", required: true
    input "daysToResume", "capability.variable", title: "Days To Resume", required: true
    input "officeThermostat", "capability.thermostat", title: "Office Thermostat", required: true
    input "eastShopThermostat", "capability.thermostat", title: "East Shop Thermostat", required: true
    input "westShopThermostat", "capability.thermostat", title: "West Shop Thermostat", required: true
    input "outsideTemperature", "capability.temperatureMeasurement", title: "Outside Temperature", required: true


def installed(switchInstance) {
log.debug "installed"
def updated() {
log.debug "updated"
def initialize() {
log.debug "initialize"

subscribe(location, "mode", modeEvent)
schedule("0 * * ? * *", midnightTask)

//Refresh devices

def uninstalled() {
log.debug "uninstalled"

// Schedule refresh
def modeEvent(evt) { "New mode at " + now() + " is " + location.mode
// Force Refresh now!

def checkDevices() {
log.debug "checkDevices " + pauseSchedule.currentValue("switch")
res = officeThermostat.currentValue("heatingSetpoint")
log.debug "office setpoint " + res
log.debug "outside temp " + outsideTemperature.currentValue("temperature")
log.debug "runningNightShift = " + runningNightShift.currentValue("switch")
log.debug "daysToResume = " + daysToResume.currentValue("variable")

def midnightTask() {
log.debug "midnight task start"
int res = daysToResume.currentValue("variable")
log.debug "res = " + res //+ daysToResume.currentValue("variable")
if (res > 0) {
res = res - 1
daysToResume = res //.setVariable(res)

def refresh() {
log.debug "refresh"

def runRefresh() {
log.debug "runRefresh"

1 Like
Integer res = (Integer) this.getGlobalVar("daysToResume").value

Thanks for the reply. Now I'm getting a null returned:

Integer res = (Integer) this.getGlobalVar("daysToResume").value
log.debug "res = " + res

2021-12-20 03:14:00.039 pm errorjava.lang.NullPointerException: Cannot get property 'value' on null object on line 92 (method midnightTask)

In response to this code:

res = this.getGlobalVar("daysToResume")
log.debug "res = " + res

I get this log:
2021-12-20 03:18:00.039 pm debug res = null

My bad, capitalize DaysToResume....

Thank you! I just hit on that as well!


What about actual Hub variables. Do they still need a connecter to be used in this way

From an App you can directly reference Hub Variables without a connector.

From the app directly yes, but I was hoping to allow user selection via preferences. The hub variables are not being offered when I try capability.variable for the input type

Have to use getAllGlobalVars... Take a look at:

about line 94

Thanks I see how you did it now, but tbh that's more convoluted than adding a connector. Hopefully one day the hub variables will be directly accessible by capability.hubvariable or something like that :smiley: