Convert HSB to RGB for MQTT (group solution)

Topic is How to convert HSB color ( as it comes from Alexa or ColorPicker) Toe a RGB Value which is undersoud by zigbee2mqtt for my ikea and tint Blubs.
Second was the idee to use a group of collor lamps instad a a rule for each lamp.

items:

Group gZBColor "Color"
Group gZBColorRGB "ColorRGB"

Group  Bulbhansolo        "hansolo"                {alexa="Endpoint.Light"}
Dimmer   ZB_Lampe_007__LevelControl   "helligkeit"    (Bulbhansolo)      {alexa="BrightnessController.brightness,PowerController.powerState",channel="mqtt:topic:blub_Color1_thg:dimmer"}
Dimmer   ZB_Lampe_007_Colortemp   "farbtemp"    (Bulbhansolo)      {alexa="ColorTemperatureController.colorTemperatureInKelvin",channel="mqtt:topic:blub_Color1_thg:colortemp"}
String    ZB_Lampe_007_colorRGB_String   "farbe"         (Bulbhansolo,gZBColorRGB) {channel="mqtt:topic:blub_Color1_thg:lampcolor"} 
Color   ZB_Lampe_007__color   "farbe"         (Bulbhansolo,gZBColor) {alexa="ColorController.color"} 
Number  ZB_Lampe_007__Linkquality   "Linkquality"  (gZBLinkQuality) {channel="mqtt:topic:blub_Color1_thg:linkquality"}

Group   BulbChewbacca       "Chewbacca"                {alexa="Endpoint.Light"}
Dimmer   ZB_Lampe_008_LevelControl   "helligkeit"    (BulbChewbacca)      {alexa="BrightnessController.brightness,PowerController.powerState",channel="mqtt:topic:blub_Color2_thg:dimmer"}
Dimmer   ZB_Lampe_008_Colortemp   "farbtemp"    (BulbChewbacca)      {alexa="ColorTemperatureController.colorTemperatureInKelvin",channel="mqtt:topic:blub_Color2_thg:colortemp"}
String    ZB_Lampe_008_colorRGB_String   "farbe"         (BulbChewbacca,gZBColorRGB) {channel="mqtt:topic:blub_Color2_thg:lampcolor"} 
Color    ZB_Lampe_008_color   "farbe"         (BulbChewbacca,gZBColor) {alexa="ColorController.color"} 
Number   ZB_Lampe_008_Linkquality   "Linkquality"  (gZBLinkQuality) {channel="mqtt:topic:blub_Color2_thg:linkquality"}

Rules:

rule "ZB_gruppe HSB -> RGB"
when
    Member of gZBColor received command  
then
    logInfo("GroupTest.rules", "Command received: " + receivedCommand)
    logInfo("GroupTest", "Member "+ triggeringItem.name +" to " + triggeringItem.state )
    val RGBItem= gZBColorRGB.members.findFirst[ t | t.name == triggeringItem.name+"RGB_String" ] as StringItem
    var r = ((triggeringItem.state as HSBType).getRed * 255 / 100).intValue
    var g = ((triggeringItem.state as HSBType).getGreen * 255 / 100).intValue
    var b = ((triggeringItem.state as HSBType).getBlue * 255 / 100).intValue
    //logInfo("lampcolor.rules", "Output Conversion: r" + r + " g" + g + " b" + b)
    var rgb = "{\"rgb\": \""+r+","+g+","+b+"\"}"
    //logInfo("lampcolor.rules", "Output Conversion: Member "+ RGBItem.name + " to" + rgb)
    RGBItem.sendCommand(rgb)
end

Feel free to give hints… :wink:

Don’t convert the values to ints (i.e. get rid of the calls to .intValue). You don’t need them as int and forcing them to be int can add minutes to your .rules file loading times.

You also don’t need to cast RGBItem to a StringItem. You can sendCommand to any Item type. I think the members of a Group is either of type Item or GenericItem.

In general, let the Rules DSL interpreter figure out the types of things at runtime instead of parse time and it will run faster and work better. Only force the type when you have to.

Though now that I’ve written this, it appears you are using intValue to round the result of the division. That is probably OK in this context.

correct, that was the idea for the integer :wink: