How do I parse battery percentage from a HTTP GET response?

Once you out it in a variable it should be data.Blinds1.battery

Ie

{
httpGet(params) {
response ->
logDebug "Request was successful, $response.status"
//logInfo "data = $response.data"
logDebug "ld = $response.data.latestData"

        def curTemp = response.data.latestData.uiData.DispTemperature
        def fanMode = response.data.latestData.fanData.fanMode
        def switchPos = response.data.latestData.uiData.SystemSwitchPosition
        def coolSetPoint = response.data.latestData.uiData.CoolSetpoint
        def heatSetPoint = response.data

Yeh… you lost me.

I was piggy backing on the code above :blush: trying to adapt it but that didn’t work as it was/is splitting (obviously at <br>) and the output of the split logic does not match my data.

try making sure you data type is json.. if that doesnt work you will have to use split on a certain charcter and use debugging to figure out whice elements of the array contain the data you nmeed

def params = [
uri: "https://mytotalconnectcomfort.com/portal/Device/CheckDataSession/${settings.honeywelldevice}",
headers: [
'Accept': '/',
'DNT': '1',
'Cache': 'false',
'dataType': 'json',
'Accept-Encoding': 'plain',
'Cache-Control': 'max-age=0',
'Accept-Language': 'en-US,en,q=0.8',
'Connection': 'keep-alive',
'Referer': 'Honeywell Home - My Total Connect Comfort',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36',
'Cookie': device.data.cookiess
],
using split i would split on the colon and then agaion maybe on blank to isolate you stuff

the first issue if using split is get rid of the double quotes as it is ffing up the result.. see example here

metadata {
definition (name: "split test", namespace: "lgkapps", author: "larry kahn kahn@lgk.com") {

capability "Actuator"
capability "Configuration"

}

}

def installed() {
initialize()
}

def updated() {
initialize()
}

def configure()
{
def String input = 'data: {' +
'"Blinds1": [' +
'{' +
'"battery": 64,' +
'"light": 0.0,' +
'macaddr": "1:1:2:1",' +
'"position": 100' +
'}' +
'],' +
'"status": "OK"' +
'}'

def input2 = input.replace("\"", "");

log.debug "stripped string = $input2"

def split1 = input2.split("battery:")

def p1 = split1[0]
def p2 = split1[1]
//def p3 = split1[2]

log.debug "1= $p1"
log.debug "2 = $p2"
//log.debug "3 = $p3"

def split2 = p2.split(",")

def p1a = split2[0]
def p2a = split2[1]

log.debug "1a = $p1a"
log.debug "2a = $p2a"

def Integer batteryval = p1a.toInteger()

log.debug "battery val = $batteryval"

}

1 Like

if you are getting text / a string you can do:

    def data = """
        {
        "Blinds1": [
            {
            "battery": 64,
            "light": 0.0,
            "macaddr": "xx:xx:xx:xx:xx:xx",
            "position": 100
            }
        ],
        "status": "OK"
        }"""

    def json_data = new groovy.json.JsonSlurper().parseText(data)
    assert json_data["Blinds1"][0]["battery"] == 64
}

in case you are performing a http request, make sure you set
contentType: "application/json". Then resp.data already contains the "json object" with data.

(Parsing text screams regular expressions to me)

1 Like

My hub is pretty busy. I gave up on hub level automation and used bash pushing to maker API.

1 Like

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