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

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
Arsenius
Posts: 8
Joined: Thursday 23 April 2015 7:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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
} 
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Arsenius
Posts: 8
Joined: Thursday 23 April 2015 7:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by Arsenius »

Thank you, it works.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest