RGB switch readout with lua

Post Reply
User avatar
bitjeverkeerd
Posts: 14
Joined: Monday 13 April 2015 20:39
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

RGB switch readout with lua

Post by bitjeverkeerd » 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?
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

User avatar
waaren
Posts: 930
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: RGB switch readout with lua

Post by waaren » Wednesday 15 August 2018 14:45

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)
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

User avatar
bitjeverkeerd
Posts: 14
Joined: Monday 13 April 2015 20:39
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: RGB switch readout with lua

Post by bitjeverkeerd » Wednesday 22 August 2018 12:20

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

Post Reply

Who is online

Users browsing this forum: megamarco83 and 2 guests