RGB switch readout with lua

Moderator: leecollings

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

RGB switch readout with lua

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

Re: RGB switch readout with lua

Post 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)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
bitjeverkeerd
Posts: 30
Joined: Monday 13 April 2015 20:39
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: RGB switch readout with lua

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest