How to evaluate a variable’s string value

So let’s say I have a driver preference variable1 that I set to “response.data.value”. In my code I want to take that value and evaluate it so if I do this call an HttpGet I can then say:

httpGet(params1) { response -> ...... }

And set a variable base on the evaluation of my preference:

def tempVar = variable1

I thought I could do this but it’s not working:

def tempVar = this[variable1]

Found this on my quest but it didn’t really help: grails - Is is possible to have strings evaluate as variables in groovy? - Stack Overflow

Thanks!

Are you trying to set a variable equal to the response or equal to the preference or are you trying to map a response to a preference? It's not clear how the HTTP get ties into what you are trying to do. Your variables are not being based on the response. Where is "value"?

After the httpget call is made response.data.value then contains data. I want to access that data by setting variable1 to “response.data.value” in my preference. So then in the code I want to evaluate variable1 (response.data.value) so that I can get the value of response.data.value in my code. Sorry, I know it’s confusing. I don’t want to hard code the json return strings in the code as I would rather know what strings I’m looking for and put those in my prefs.

I'm also confused. If you simply need to store the value from the response somewhere so you can reference it later in your code, then you can save it in a state variable. This will let you access the value anywhere in your app/driver.

state.savedVariable = response.data.value
log.debug state.savedVariable
1 Like

I’m doing a poor job of explaining. Maybe this will help.

I want to evaluate the string in these variables. If I print out variable1, it gives me the string representation and gives me “resp1.data.stats.current.poolTemp” not the value that resp1.data.stats.current.poolTemp represents after the httpget.

Ok I think I understand but it leads me to another question. Why do you need to have the resp.data.etc listed in a text input? There must be a better way to accomplish what you need. If you can describe the purpose of the inputs we might be able to provide a simpler solution. For instance, what do you mean by "local sensor"?

If you absolutely need to setup the inputs the way you have it then:

  • since they seem to have the same prefix, you should simplify the input and just enter "poolTemp" or "crawlHumidity"
  • then in the response of your GET request,
    def variable1 = response.data.$inputName

@brianwilson
Brian
have you checked to see the response data contains the correct variable that you need?
The easiest way is to use something like this.

log.info "Response data: ${response.data}"
Just after your http call

I've noticed with weewx that you often get 'sub folders' in the response so the value of your required data could be an extra level down.
You need to find the correct 'path' to the data you want.

as an example..
To get a 'country' name from weewx you would use:
state.country = response.data.current_observation.display_location.country

response
data
current_observations
display_location
country - This would appear with a value of 'UK' in my case

Andy

1 Like

Local sensor is the local capability. For instance, I want to query a value and set the local capability on the device to be that value. So then I can make RM actions based on the temp of my pool, or humidity of outside, or if it’s rained.

As cobra mentioned below, I if I limit everything up to current on the json, then I can’t access things outside of that json container, like rain since midnight.

"hardware":"SDR",
"uptime":"0 days, 0 hours, 48 minutes",
"serverUptime":"0 days, 0 hours, 55 minutes",
"weewxVersion":"3.9.1",
"stats": {
"current": {
"outTemp":"78.8°F",
"windchill":"78.8°F",
"heatIndex":"78.8°F",
"dewpoint":"74.1°F",
"humidity":"86%",
"insideHumidity":"51%",
"barometer":"30.504 inHg",
"barometerTrendDelta":"3 hours",
"barometerTrendData":"-0.270 inHg",
"windSpeed":"1 mph",
"windDir":"237°",
"windDirText":"WSW",
"windGust":"4 mph",
"windGustDir":" N/A",
"rainRate":"0.00 in/hr",
"poolTemp":"86.9°F",
"crawlTemp":"86.9°F",
"crawlHumidity":"51%",
"insideTemp":"71.6°F"

},

"sinceMidnight": {
"rainSum":"0.00 in",
"maxtemptoday":"95.5°F",
"mintemptoday":"73.2°F",
"maxinsidetemptoday":"73.6°F",
"mininsidetemptoday":"70.9°F"
}

Wouldn't be easier to store these values in attributes rather than transposing them to input values?

1 Like

Ok.. there are a couple of ways to do this in the driver.

add it to a variable..

state.crawlHumidity = response.stats.current.crawlHumidity

Directly set it to an attribute

sendEvent(name: "attributeName", value: response.stats.current.crawlHumidity)

Don't forget that the output from the json file is a string in this case (because of the %)


For since midnight stuff...
Just go up a level to break out of 'current'

state.rainSum = response.stats.sinceMidnight.rainSum

or

sendEvent(name: "attributeName", value: response.stats.sinceMidnight.rainSum)

1 Like

I still haven't figured this out. So the use case is that if someone wants to set the device driver "temperature" to Weewx's outdoor temp (outTemp) or inside temp (insideTemp), I want them to input the weewx field to set the local device's temp.

Thanks, I think I did try that at one point. I just tried this:

if i set settings.temp = data.stats.current.outTemp

then try to obtain the value of (response.data.stats.current.outTemp) by doing this:

def temp=response."${settings.temp}"

I get this error.
something went wrong: groovy.lang.MissingPropertyException: No such property: data.stats.current.outTemp for class: groovyx.net.http.HttpResponseDecorator

accessing resp1.data.stats.current.outTemp directly - without part of it being a settings variable - it works fine.

I'm not sure how attributes help this situation.