I need to set custom color on RGB bulb, i have 3 virtual dimmer, which levels represent the RGB components
how to convert the level decimals into corresponding RGB string ?
def setColor(value) {
if ((debugEnable) || (descLog)) log.info "setColor() : ${value}"
def myred
def mygreen
def myblue
def hexValue
def cmds =
if ( value.level == 1 && value.saturation > 20) {
def rgb = huesatToRGB(value.hue as Integer, 100)
myred = rgb[0] >=128 ? 255 : 0
mygreen = rgb[1] >=128 ? 255 : 0
myblue = rgb[2] >=128 ? 255 : 0
}
else if ( value.level > 1 ) {
def rgb = huesatToRGB(value.hue as Integer, value.saturation as Integer)
myred = rgb[0] >=128 ? 255 : 0
mygreen = rgb[1] >=128 ? 255 : 0
myblue = rgb[2] >=128 ? 255 : 0
}
else if (value.hex) {
def rgb = value.hex.findAll(/[0-9a-fA-F]{2}/).collect { Integer.parseInt(it, 16) }
myred = rgb[0] >=128 ? 255 : 0
mygreen = rgb[1] >=128 ? 255 : 0
myblue = rgb[2] >=128 ? 255 : 0
}
else {
myred=value.red >=128 ? 255 : 0
mygreen=value.green >=128 ? 255 : 0
myblue=value.blue>=128 ? 255 : 0
}
cmds << "200100"
// cmds << " delay 250"
if (myred!=0) {
cmds << "33060002FF"
//cmds << "delay 150"
cmds << "330702"
}
if (mygreen!=0) {
cmds << "33060003FF"
//cmds << "delay 150"
cmds << "330703"
}
if (myblue!=0) {
cmds << "3306000400"
//cmds << "delay 150"
cmds << "330704"
}
// cmds << " delay 100"
cmds << zwave.basicV1.basicGet()
sendEvent(name:"hue", value: value.hue)
sendEvent(name:"saturation", value: value.saturation)
sendEvent(name:"level", value: value.level)
state.hue = value.hue
state.saturation = value.saturation
state.level = value.level
hexValue = rgbToHex([r:myred, g:mygreen, b:myblue])
if(hexValue) sendEvent(name: "color", value: hexValue, displayed: true)
state.color = hexValue
commands(cmds)
}
def huesatToRGB(float hue, float sat) {
while(hue >= 100) hue -= 100
int h = (int)(hue / 100 * 6)
float f = hue / 100 * 6 - h
int p = Math.round(255 * (1 - (sat / 100)))
int q = Math.round(255 * (1 - (sat / 100) * f))
int t = Math.round(255 * (1 - (sat / 100) * (1 - f)))
switch (h) {
case 0: return [255, t, p]
case 1: return [q, 255, p]
case 2: return [p, 255, t]
case 3: return [p, q, 255]
case 4: return [t, p, 255]
case 5: return [255, p, q]
}
}
def rgbToHex(rgb) {
def r = hex(rgb.r)
def g = hex(rgb.g)
def b = hex(rgb.b)
def hexColor = "#${r}${g}${b}"
hexColor
}
private hex(value, width=2) {
def s = new BigInteger(Math.round(value).toString()).toString(16)
while (s.size() < width) {
s = "0" + s
}
s
}
Thank you, but where in the rule machine do i add the code please?
you didnt ask for rule machine (sorry didnt see that was the rule machine forum) that is code for a device driver.
what commands does your device support (post the device panel) this will determine what you can do from rule machine.
if it only has level it sounds like the device driver you are using (maybe the wrong one) does not support rgb
yeah, my bad, i ASSume too much the driver allows for rgb setting as #RRGGBB hexa string and i have 3x level number :-/ i wonder if i could somehow shoehorn the code into RM custom action invocation, tried to avoid creating app ; appreciate the help!
Using Advanced Zigbee RGBW Bulb on physical Gleedo controller that controls 3 independent strips, and to control it i am trying to use 3 virtual dimmers