Page 1 of 1

setRGB not working correctly with variables

Posted: Friday 08 March 2019 22:24
by waltervl
I have this simple script and I want to change the Red value of an RGB bulb with a remote. I want to change more but as it did not work I got back to basic :)

Code: Select all

return {
	on = { devices = { 109 } }, -- remote
	data = { GrpRed = { initial = 128 } },
	execute = function(dz, device)
		dz.log('Device ' .. device.name .. ' was changed', dz.LOG_INFO)
		if dz.devices(109).state == 'Left_click' then -- left click
	        dz.data.GrpRed = dz.data.GrpRed-10
	        dz.log('Color red ' .. dz.data.GrpRed .. ' was changed', dz.LOG_INFO)
	        dz.devices(105).setRGB(dz.data.GrpRed, 255, 255)  -- set bulb color
        end
        if dz.devices(109).state == 'Right_click' then -- right click
            dz.data.GrpRed = dz.data.GrpRed+10
            dz.log('Color red ' .. dz.data.GrpRed .. ' was changed', dz.LOG_INFO)
            dz.devices(105).setRGB(dz.data.GrpRed, 255, 255) -- set bulb color
        end
    end
}
But when clicking the calculation seems to get all mixed up.
Left click should be RGB Hex d0ffff but getting 00ffff, also in debug i see " &hue=180&brightness=100&iswhite=true"
Debug Log:
Spoiler: show
2019-03-08 22:04:33.466 Status: dzVents: Debug: Event triggers:
2019-03-08 22:04:33.466 Status: dzVents: Debug: - Device: ZiGate - Ikea_Round_5b-90fd9ffffef166a6-01
2019-03-08 22:04:33.504 Status: dzVents: Info: Handling events for: "ZiGate - Ikea_Round_5b-90fd9ffffef166a6-01", value: "Left_click"
2019-03-08 22:04:33.504 Status: dzVents: Info: ------ Start internal script: Set_color_woonkamer: Device: "ZiGate - Ikea_Round_5b-90fd9ffffef166a6-01 (ZiGate)", Index: 109
2019-03-08 22:04:33.505 Status: dzVents: Info: Device ZiGate - Ikea_Round_5b-90fd9ffffef166a6-01 was changed
2019-03-08 22:04:33.505 Status: dzVents: Info: Color red 208 was changed
2019-03-08 22:04:33.507 Status: dzVents: Debug: Processing device-adapter for ZiGate - Schemerlamp TV: RGB(W) device adapter
2019-03-08 22:04:33.507 Status: dzVents: Debug: Processing device-adapter for ZiGate - Schemerlamp TV: Switch device adapter
2019-03-08 22:04:33.507 Status: dzVents: Debug: OpenURL: url = http://127.0.0.1:8080/json.htm?param=se ... white=true
2019-03-08 22:04:33.507 Status: dzVents: Debug: OpenURL: method = GET
2019-03-08 22:04:33.507 Status: dzVents: Debug: OpenURL: post data = nil
2019-03-08 22:04:33.507 Status: dzVents: Debug: OpenURL: headers = nil
2019-03-08 22:04:33.507 Status: dzVents: Debug: OpenURL: callback = nil
2019-03-08 22:04:33.507 Status: dzVents: Info: ------ Finished Set_color_woonkamer
2019-03-08 22:04:33.507 Status: dzVents: Debug: Commands sent to Domoticz:
2019-03-08 22:04:33.508 Status: dzVents: Debug: - OpenURL = {["URL"]="http://127.0.0.1:8080/json.htm?param=se ... white=true", ["method"]="GET"}
2019-03-08 22:04:33.508 Status: dzVents: Debug: =====================================================
2019-03-08 22:04:33.613 Status: EventSystem: Script event triggered: /home/udoox86/domoticz/dzVents/runtime/dzVents.lua
2019-03-08 22:04:33.626 Status: setcolbrightnessvalue: ID: 69, bri: 100, color: '{m: 1, RGB: 00ffff, CWWW: 0000, CT: 0}'
Right button click, should be RGB hex daffff but getting 00ffff and also see the same &hue=180&brightness=100&iswhite=true
Debug Log:
Spoiler: show
2019-03-08 22:07:19.748 Status: dzVents: Info: Device ZiGate - Ikea_Round_5b-90fd9ffffef166a6-01 was changed
2019-03-08 22:07:19.748 Status: dzVents: Info: Color red 218 was changed
2019-03-08 22:07:19.749 Status: dzVents: Debug: Processing device-adapter for ZiGate - Schemerlamp TV: RGB(W) device adapter
2019-03-08 22:07:19.749 Status: dzVents: Debug: Processing device-adapter for ZiGate - Schemerlamp TV: Switch device adapter
2019-03-08 22:07:19.749 Status: dzVents: Debug: OpenURL: url = http://127.0.0.1:8080/json.htm?param=se ... white=true
2019-03-08 22:07:19.749 Status: dzVents: Debug: OpenURL: method = GET
2019-03-08 22:07:19.749 Status: dzVents: Debug: OpenURL: post data = nil
2019-03-08 22:07:19.749 Status: dzVents: Debug: OpenURL: headers = nil
2019-03-08 22:07:19.750 Status: dzVents: Debug: OpenURL: callback = nil
2019-03-08 22:07:19.750 Status: dzVents: Info: ------ Finished Set_color_woonkamer
2019-03-08 22:07:19.750 Status: dzVents: Debug: Commands sent to Domoticz:
2019-03-08 22:07:19.751 Status: dzVents: Debug: - OpenURL = {["URL"]="http://127.0.0.1:8080/json.htm?param=se ... white=true", ["method"]="GET"}
2019-03-08 22:07:19.751 Status: dzVents: Debug: =====================================================
2019-03-08 22:07:19.867 Status: EventSystem: Script event triggered: /home/udoox86/domoticz/dzVents/runtime/dzVents.lua
2019-03-08 22:07:19.898 Status: setcolbrightnessvalue: ID: 69, bri: 100, color: '{m: 1, RGB: 00ffff, CWWW: 0000, CT: 0}'
It looks like RGB is transferred to HSV without setting the Saturation

Re: setRGB not working correctly with variables

Posted: Saturday 09 March 2019 14:25
by waaren
waltervl wrote: Friday 08 March 2019 22:24 I have this simple script and I want to change the Red value of an RGB bulb with a remote. I want to change more but as it did not work I got back to basic :)
It looks like RGB is transferred to HSV without setting the Saturation
Can you please test this script and report back ?
If this answers better to your requirement I will try to implement this as a native dzVents command.

Thanks

Code: Select all

return {
    on = { devices = { 109 } }, -- remote
    data = { GrpRed = { initial = 128 } },
    
    execute = function(dz, device)
    
        local function setColor(colorDevice,r,g,b)
            colors = dz.utils.fromJSON(colorDevice.color) 
            local url = dz.settings['Domoticz url'] ..  "/json.htm?type=command&param=setcolbrightnessvalue" ..
                       '&idx='                  .. colorDevice.id   ..
                        '&color={"m":3,"t":'    .. colors.t         ..
                        ',"cw":'                .. colors.cw        ..
                        ',"ww":'                .. colors.ww        ..
                        ',"r":'                 .. r                ..
                        ',"g":'                 .. g                ..
                        ',"b":'                 .. b                ..
                        '}' 
            dz.openURL(url)
        end
    
    
        dz.log('Device ' .. device.name .. ' was changed', dz.LOG_INFO)
        if dz.devices(109).state == 'Left_click' then -- left click
            dz.data.GrpRed = dz.data.GrpRed-10
            dz.log('Color red ' .. dz.data.GrpRed .. ' was changed', dz.LOG_INFO)
            setColor(dz.devices(105),dz.data.GrpRed, 255, 255)  -- set bulb color
        end
        if dz.devices(109).state == 'Right_click' then -- right click
            dz.data.GrpRed = dz.data.GrpRed+10
            dz.log('Color red ' .. dz.data.GrpRed .. ' was changed', dz.LOG_INFO)
            setColor(dz.devices(105),dz.data.GrpRed, 255, 255) -- set bulb color
        end
    end
}

Re: setRGB not working correctly with variables

Posted: Sunday 10 March 2019 23:52
by waltervl
Thanks, this works for 1 bulb but not for usage when used for a group. I use the ZiGate plugin so I have to check further.

Is it not possible to just implement the setcolorbrightness function in dzVents? Just as it is now in the API?

Re: setRGB not working correctly with variables

Posted: Monday 11 March 2019 6:33
by waaren
waltervl wrote: Sunday 10 March 2019 23:52 Thanks, this works for 1 bulb but not for usage when used for a group. I use the ZiGate plugin so I have to check further.
Is it not possible to just implement the setcolorbrightness function in dzVents? Just as it is now in the API?
Not sure I understand your remark and question. Please clarify

What else should this to be implemented function do, apart from what the setColor function in my example script ?
Do you have an example JSON / API that works on a group ?

Re: setRGB not working correctly with variables

Posted: Wednesday 13 March 2019 22:48
by waltervl
To clarify, when using the group I get an error in on line "colors = dz.utils.fromJSON(colorDevice.color) " so it looks like the group color definition by the plugin is different than a normal color bulb. I think I have to address that to the plugin writer.
But the line is not necessary if just the RGB API/JSON is followed. No need to read the JSON first.
The command below is working for the group and for the separate bulbs.
/json.htm?type=command&param=setcolbrightnessvalue&idx=110&hex=ff00ff&brightness=50

Looking at the Domoticz API/JSON there are 3 modes for setcolorbrightness: HUE, RGB and JSON color object and brightness.
dzVents now has a function setRGB what apparently does setHUE. So perhaps make dzVents functions setHUE(H,Br), setRGB(R,G,B,Br) and for the die hards setColorBrightness(M,T,R,G,B,CW,WW) according the API/JASON definition.

But just my 2 cents, I don't know what would be the impact of the changed definition of setRGB()

Re: setRGB not working correctly with variables

Posted: Thursday 14 March 2019 0:01
by waltervl
As a test I did this and it seems to work just what I need.

Code: Select all

return {
    on = { devices = { 109 } }, -- remote
    data = { GrpRed = { initial = 128 } },
    
    execute = function(dz, device)
        
        local function tohex(num)
            local charset = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}
            local tmp = {}
            repeat
               table.insert(tmp,1,charset[num%16+1])
               num = math.floor(num/16)
            until num==0
            return table.concat(tmp)
        end    
        local function setColor(colorDevice,r,g,b)
            local url = dz.settings['Domoticz url'] ..  "/json.htm?type=command&param=setcolbrightnessvalue" ..
                       '&idx='   .. colorDevice.id   ..
                        '&hex='  .. tohex(r) .. tohex(g) .. tohex(b) 
            dz.openURL(url)
            dz.log('URL: ' .. url .. ' send', dz.LOG_INFO)
        end

        dz.log('Device ' .. device.name .. ' was changed', dz.LOG_INFO)
        if dz.devices(109).state == 'Left_click' then -- left click
            dz.data.GrpRed = dz.data.GrpRed-20
            dz.log('Color red ' .. dz.data.GrpRed .. ' was changed', dz.LOG_INFO)
            setColor(dz.devices(110),dz.data.GrpRed, 128, 128)  -- set bulb color
        end
        if dz.devices(109).state == 'Right_click' then -- right click
            dz.data.GrpRed = dz.data.GrpRed+20
            dz.log('Color red ' .. dz.data.GrpRed .. ' was changed', dz.LOG_INFO)
            setColor(dz.devices(110),dz.data.GrpRed, 128, 128) -- set bulb color
        end
    end
}

Re: setRGB not working correctly with variables  [SOLVED]

Posted: Thursday 14 March 2019 0:33
by waaren
waltervl wrote: Thursday 14 March 2019 0:01 As a test I did this and it seems to work just what I need.
Thx,
will have a look at your example and proposal.