Accessing dzvents JSON Data Table

Moderator: leecollings

Post Reply
Wolfgang
Posts: 22
Joined: Saturday 28 October 2017 21:37
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Accessing dzvents JSON Data Table

Post by Wolfgang »

Hello ,
I've got two problems with the dzvents data format .

I'm reading data from a website which returns the data in json format like this :

{
"8700": {
"name": "Aussentemperatur",
"value": "7.8",
"unit": "°C",
"desc": "",
"dataType": 0
}
}

I'm replacing the "8700" item with "Temp" in the script because I cannot access the field named completely numeric .
But how can I get the value of the field "value" into a variable to fill it into a virtual sensor ?
Currently there is always a nil value seen in the log.

Current script snippet as follows :

Code: Select all

execute = function(domoticz, item)
		if (item.isTimer) then
			domoticz.openURL({
				url = 'http://192.168.252.226/JQ=8700',
				method = 'GET',
				callback = 'trigger', -- see httpResponses above.
			})

		end

		if (item.isHTTPResponse) then
			if (item.statusCode == 200) then
				if (item.isJSON) then

					local HeizungAussentemperaturString = domoticz.utils.toJSON(item.data)
					HeizungAussentemperaturString = string.gsub(HeizungAussentemperaturString,"8700","Temp")
					domoticz.log('-------------------------------------------------------------------------')
					domoticz.log(HeizungAussentemperaturString,domoticz.LOG_INFO)
					domoticz.log('-------------------------------------------------------------------------')
					local HeizungAussentemperaturJSON = domoticz.utils.fromJSON(HeizungAussentemperaturString)
					domoticz.log(HeizungAussentemperaturJSON,domoticz.LOG_INFO)
					domoticz.log('------------------------------------')
					local HeizungAussentemperatur = HeizungAussentemperaturJSON.Temp.value
					domoticz.log(HeizungAussentemperatur,domoticz.LOG_INFO)
					--domoticz.log('Aussentemperatur : ' .. HeizungAussentemperatur)
					-- update some device in Domoticz
					domoticz.devices('Heizung Aussentemperatur').updateTemperature(HeizungAussentemperatur)
				end
so currently the HeizungAussentemperatur variable has always a nil value in the debug log .

Any help on getting the temperature value to the variable would be fine ...
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Accessing dzvents JSON Data Table

Post by waaren »

Wolfgang wrote: Sunday 09 December 2018 17:06 I've got two problems with the dzvents data format .
I'm reading data from a website which returns the data in json format like this :
{
"8700": {
"name": "Aussentemperatur",
"value": "7.8",
"unit": "°C",
"desc": "",
"dataType": 0
}
}
Any help on getting the temperature value to the variable would be fine ...
Hope this example help.

Code: Select all

return  {

    on       = {    timer           =   { "every minute"    }}, 
    
    execute = function(dz)
        
        function deepdump( tbl )
            local checklist = {}
            
            local function innerdump( tbl, indent )
                checklist[ tostring(tbl) ] = true
                for k,v in pairs(tbl) do
                        print (indent .. tostring(k) .. " ===> value: ".. tostring(v)  )
                    if (type(v) == "table" and not checklist[ tostring(v) ]) then 
                        innerdump(v,indent .. tostring(k) .. ".") 
                    end
                end
            end
            checklist[ tostring(tbl) ] = true
            innerdump( tbl, "Key: " )
         end 

        
        local testdata  ='{"8700": {"name": "Aussentemperatur","value": "7.8","unit": "°C","desc": "","dataType": 0}}'
        local myTable       = dz.utils.fromJSON(testdata) 
        deepdump(myTable) -- use this function call to see the structure of your table
        
        
        print (myTable["8700"].value) -- myTable.8700.value does not work; this sugar syntax  can only be used with strings 
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Wolfgang
Posts: 22
Joined: Saturday 28 October 2017 21:37
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Accessing dzvents JSON Data Table

Post by Wolfgang »

thanks for the clarifying code. This was the hint I was searching for , it works now !
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests