Page 1 of 1

RGB switch readout with lua

Posted: Wednesday 15 August 2018 13:48
by bitjeverkeerd
I use the lua script below to read out the on/off state and brightness value of a virtual rgb switch from Domoticz (and to control hardware).
This script is triggered when the value of the virtual switch is changed.
Is it also possible to read the rgb value with a similar lua script?
Spoiler: show
commandArray = {}

DomoticzDevice = 'RGBW' -- Naam van script en van schakelaar in Domoticz!!!

if devicechanged[DomoticzDevice] then
if(otherdevices[DomoticzDevice]=='Off') then
text = "RGBW = Off"
elseif(otherdevices[DomoticzDevice]=='On') then
text = "RGBW = On"
else
DomoticzValue = (otherdevices_svalues[DomoticzDevice])
text = "RGBW value = " .. (DomoticzValue) .. ""
end

print (text)
end

return commandArray

Re: RGB switch readout with lua

Posted: Wednesday 15 August 2018 14:45
by waaren
bitjeverkeerd wrote: Wednesday 15 August 2018 13:48 I use the lua script below to read out the on/off state and brightness value of a virtual rgb switch from Domoticz (and to control hardware).
This script is triggered when the value of the virtual switch is changed.
Is it also possible to read the rgb value with a similar lua script?
It is possible to read these RGB values but not very straightforward. I only know how to do this in dzVents like here.
But even in dzVents it cannot be done in a standard way as the RGB data need to be read twice. (as explained in that thread)

Re: RGB switch readout with lua

Posted: Wednesday 22 August 2018 12:20
by bitjeverkeerd
I have created the following code (maybe not the most beautiful) but it works. Maybe it is useful for others on the forum.

Code: Select all

commandArray = {}

DomoticzDevice = 'RGB'	    							-- Naam van script en van schakelaar in Domoticz!!!

if devicechanged[DomoticzDevice] then
   if (otherdevices[DomoticzDevice]=='Off') then
     text = "Dimmer = Off" 
   elseif (otherdevices[DomoticzDevice]=='On') then 
     text = "Dimmer = On"
   else
     json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
     local acolor = " "
     ab='curl "http://192.168.51.111:8080/json.htm?type=devices&rid=12"'	-- IP domoticz & IDX device
     local jsondata = assert(io.popen(ab))
     local jsondevices = jsondata:read('*all')
     jsondata:close()
     local jsonCPM = json:decode(jsondevices)
     local acolor=jsonCPM.result[1].Color
     print (acolor)

     acolorj=acolor:gsub("}",",}")
     for k,v in acolorj:gmatch('"(.-)":(.-),') do						
       --print(k,v)
       if (k == 'b') then
         rgbBlueValue = v 
       elseif (k == 'r') then
         rgbRedValue = v 
       elseif (k == 'g') then
         rgbGreenValue = v
       end 
     end 
     DimLevel = (otherdevices_svalues[DomoticzDevice])
     text = "Dimmer = " .. (DimLevel) .. " Rood = " .. (rgbRedValue) .. " Groen = " .. (rgbGreenValue) .. " Blauw =  " .. (rgbBlueValue) .. ""
   end
      
   print (text)
end

return commandArray