Use of getGlobalVar in device code

Really struggling with this - I can set a hub variable from my device code using setGlobalVar but cannot read the same variable into the same device code using getGlobalVar. What am I missing?

Error code is 'groovy.lang.MissingMethodException: No signature of method: user_driver__Test_Driver_for_Global_Variable_1066.getGlobalVar() is applicable for argument types: (java.lang.String) values: [evchargeamps] on line 15 (method refresh)'

Driver Code is:
metadata {
definition (
name: "Test Driver for Global Variable",
namespace: "",
author: "")

{
capability "Refresh"
attribute "evchargeamps", "real"
}
}
public void refresh() {
this.setGlobalVar("evchargeamps", 3.0)
sendEvent(name: "evchargeamps", value: 3.0)
log.debug this.getGlobalVar("evchargeamps").value
}

Appreciate your thoughts...

Thanks

Looks like this is not available in drivers, you will need to write an app to read the variable. @thebearmay has a small example a few posts above this one...

2 Likes

Thanks for the replies and I'll now try to re-architect to use an app. However any suggestions on a better way to pass data into a driver? In this example I am trying to pass values for charge rate (amps), charge level (kwh) and to initiate a charge into a Tesla driver. These values will modulate based on solar power generation and house power consumption to minimise draw or regen from/to the power grid. A driver does seem the obvious place to control such activity, with an app then used to calculate the setpoints and pass to the driver, but if there is no means of getting customised data into a driver then I am left with little choice.

Many Thanks

You can store data in 3 ways, but I expect an Attribute will be your best option. Others will be able to explain the reasons for choosing the alternatives. Attributes will allow you to trigger rules and read the values in conditions and to display on dashboards.

Most drivers will have attributes included so it shouldn't be hard to find an example. You just define it at the top of the driver and use it throughout the code. If you want to pass values in and store them, define a command at the top of the driver with parameters, write a method to match that definition and then use a "custom command" action in a rule to call the command, or do something similar in another app.

I can try and find some examples tomorrow if you need some, or sing out and I'm sure someone else will be able to assist.

Thanks, I'm happy with attributes and have used them before but only to pass data out of a device - so if I follow you correctly I should be able to set a device attribute externally (say from an app or RM), and then use this value in the device code. I'll give it a go.

Regards

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.