Page 1 of 1

local variable not updating until after script\working with json data

Posted: Sunday 23 December 2018 18:53
by Arsenius
I am trying to get the RGB value of a dummy_color_switch.
The json value looks like this:
Color "{\"b\":255,\"cw\":0,\"g\":247,\"m\":3,\"r\":199,\"t\":0,\"ww\":0}"
in my dzvent script i would like to have 3 variables for Red\Green\Blue.
So from the wiki i get that i need to use domoticz.utils.fromJSON() and that i can then assign these 3 values so i made this script:

Code: Select all

return {
	on = {
		devices = {
			'color_switch'
		}
	},
	execute = function(domoticz, device)
	    myColor = domoticz.utils.fromJSON(device.color) -- also tried this with 'local myColor'
	    
	    print(myColor.r)
	    print(myColor.g)
	    print(myColor.b)
	    end
When i change the color on the switch, i dont see the new values but i see the old values from the previous change.
Is this the right way to get specific values from a json value?
How do i make this so these values are from the made change.

Re: local variable not updating until after script\working with json data

Posted: Sunday 23 December 2018 22:14
by waaren
Arsenius wrote: Sunday 23 December 2018 18:53 I am trying to get the RGB value of a dummy_color_switch.
When i change the color on the switch, i dont see the new values but i see the old values from the previous change.
Is this the right way to get specific values from a json value?
How do i make this so these values are from the made change.
It is the right way to get values from a JSON but here the problem is that domoticz does not update the color values on the first pass. (don't know enough of the domoticz internals to explain why)
You can get the RGB values but you will have to use an explicit API call and therefor you need to use the workaround as in the script below.

Code: Select all

local callBack    = "getColorFromDevice"
local colorSwitch = 839

return {
    
    on =    {  
                devices         = { colorSwitch },
                httpResponses   = { callBack    },
            },
            
	execute = function(dz, item)

        local function getDeviceStatus(idx)
            local url         = dz.settings['Domoticz url'] .. "/json.htm?type=devices&rid=" .. idx 
            dz.openURL  ({ url = url, method = "GET", callback = callBack })
        end

        if item.isDevice then 
            getDeviceStatus(item.id) 
        else  
            myColor = item.json
            myColor = dz.utils.fromJSON((myColor.result[1].Color))  
            dz.log("Red   value: " .. myColor["r"],dz.LOG_FORCE)
            dz.log("Green value: " .. myColor["g"],dz.LOG_FORCE)
            dz.log("Blue  value: " .. myColor["b"],dz.LOG_FORCE)
        end
	end
} 

Re: local variable not updating until after script\working with json data

Posted: Monday 24 December 2018 10:07
by Arsenius
Thank you, it works.