Page 1 of 1

Read "data" out of "result", when doing HTTP

Posted: Thursday 22 August 2019 23:34
by hoeby
I am stuck on this one.
I thoughed i made something that looked like it and modify it, but it doesn't work.

I want to look at the word that is behind "data".
So i can make an if/elsif, that runs a script when data is changing.

Code: Select all

"result" : [
      {
         "AddjMulti" : 1.0,
         "AddjMulti2" : 1.0,
         "AddjValue" : 0.0,
         "AddjValue2" : 0.0,
         "BatteryLevel" : 255,
         "CustomImage" : 108,
         "Data" : "Charging",
         "Description" : "",
my code,
response "item OK" is printed. But after that i am stuck.

Code: Select all

return {
    on = {
	    --devices = {'Xiaomi Mi Robot Vacuum - Status',
		devices = {'Slide2'},
		httpResponses   = { "VacState"}
	},
    
   
    
    execute = function(dz, item)
        
        if item.isHTTPResponse then
         print("HTTPReponse found")
            if item.ok then
                print("item OK")
                local myArr = item.json.result
                local state = myArr.data
           
                print (state)
            end
        else
            -- logWrite("Timer or Device triggered event")
                dz.openURL(
                            {
                                url         = "http://xxx.xxx.xxx.xxx:xxx/json.htm?type=devices&rid=350",
                                method      = "GET",
                                callback    = "VacState", 
                            })
        end
    end
}

Re: Read "data" out of "result", when doing HTTP

Posted: Friday 23 August 2019 9:02
by waaren

hoeby wrote: I want to look at the word that is behind "data".
So i can make an if/elsif, that runs a script when data is changing.

Code: Select all

         "Data" : "Charging",
}
The key you are looking for is "Data'
Case matters
But cant dzVents gets this data in a more direct way?





Re: Read "data" out of "result", when doing HTTP

Posted: Friday 23 August 2019 14:22
by hoeby
Tried this code, to work without the HTTP request.
But get a nihil value.

Also Case changing in the previous way to gey the information, didn't do the trick.

Code: Select all

local Vacuum = dz.devices("devicename")
local VacuumState = Vacuum.Data
print (VacuumState)

Re: Read

Posted: Friday 23 August 2019 14:32
by waaren
Try

local myArr = item.json.result[1]
local state = myArr.Data

Re: Read "data" out of "result", when doing HTTP

Posted: Friday 23 August 2019 16:54
by hoeby
Your great Waaren.
The [1] adding worked.

Yesterday I did try [0] to add, but that gives a nihil value back.
Reason why [0], is that i run a simalar script in HTML, which works with the [0]

Re: Read "data" out of "result", when doing HTTP

Posted: Friday 23 August 2019 19:30
by waaren
hoeby wrote: Friday 23 August 2019 16:54 Tried

Code: Select all

local Vacuum = dz.devices("devicename")
local VacuumState = Vacuum.Data
print (VacuumState)
And what do you see with this script ?

Code: Select all

return {
    on = {
		devices = {'Slide2'},
	},

    execute = function(dz, item)
        dz.log(item.rawData,dz.LOG_FORCE)
    end
}


Re: Read "data" out of "result", when doing HTTP

Posted: Friday 23 August 2019 23:15
by hoeby
waaren wrote: Friday 23 August 2019 19:30 And what do you see with this script ?

Code: Select all

return {
    on = {
		devices = {'Slide2'},
	},

    execute = function(dz, item)
        dz.log(item.rawData,dz.LOG_FORCE)
    end
}

This is the output

Code: Select all

dzVents: !Info: {"0"}

Re: Read "data" out of "result", when doing HTTP

Posted: Friday 23 August 2019 23:21
by waaren
hoeby wrote: Friday 23 August 2019 23:15 This is the output

Code: Select all

dzVents: !Info: {"0"}
Is this the same device that showed "charging" in the data field ?

if not please try this.

Code: Select all

return {
    on = {
		devices = {'Slide2'},
	},

    execute = function(dz, item)
        dz.log(dz.devices(350).rawData,dz.LOG_FORCE)
    end
}

Re: Read "data" out of "result", when doing HTTP  [Solved]

Posted: Friday 23 August 2019 23:29
by hoeby
waaren wrote: Friday 23 August 2019 23:21 Is this the same device that showed "charging" in the data field ?
No it was not.
It is the vacuumcleaner, which is not slide2, which i use as trigger.
The vacuumcleaner is to noisy at this time moment.

I tried the script it returns:

Code: Select all

{"Charging"}
Added the [1] to the script

Code: Select all

return {
    on = {
		devices = {'Slide2'},
	},

    execute = function(dz, device)
      dz.log(dz.devices(350).rawData[1],dz.LOG_FORCE)
      
    end
}
Than the reply is

Code: Select all

Charging
Will try tomorow to change my script, so it doesn't need the http request anymore.
Thanks Waaren