HomeAssistant MQTT Component: RGBW Color channel set to trigger

Hello Thomas,
it took me quite some time to figure it out. It’s a combination of thing and item configuration as well as configuration of the RGBW controller. This should work on your light as well.

The JSON for my RGBW strip is the same as the one for your floodlight, so i think you can just copy & paste my code.

Step one is to make sure, that the color_temp attribute is not linked to the color attribute in your device. This is, because the color_temp values range from 150 to 500, but if these attributes are linked, the color_temp value can exceed this range. I have no idea why this is, but if you do not unlink them, the item for the color temperature will throw errors (because it gets sent values lower than 150 or higher than 500). So, go to the zigbee2mqtt webinterface and view the details of your device (Devices → Floodlight" → Settings (specific)) and deactivate color_sync.

Now you can add the thing. The definition misses the “effect” attribute for the light effects like blinking and fading. I didn’t need that so i skipped it. Here is the YAML code:

UID: mqtt:topic:RGBW3
label: RGBW3
thingTypeUID: mqtt:topic
configuration:
  payloadNotAvailable: offline
  payloadAvailable: online
  availibilityTopic: zigbee2mqtt/RGBW3/availability
  transformationPattern: JSONPATH:$.state
bridgeUID: mqtt:broker:e62cc6b6d3
channels:
  - id: status
    channelTypeUID: mqtt:switch
    label: State
    description: null
    configuration:
      retained: false
      postCommand: false
      formatBeforePublish: '{"state": "%s"}'
      commandTopic: zigbee2mqtt/RGBW3/set
      stateTopic: zigbee2mqtt/RGBW3
      transformationPattern: JSONPATH:$.state
      off: OFF
      on: ON
  - id: color
    channelTypeUID: mqtt:color
    label: Color
    description: null
    configuration:
      retained: false
      postCommand: false
      transformationPatternOut: JS:colorcsv2json.js
      formatBeforePublish: "%s"
      commandTopic: zigbee2mqtt/RGBW3/set
      colorMode: XYY
      stateTopic: zigbee2mqtt/RGBW3
      transformationPattern: JS:colorjson2csv.js
      off: "0"
      on: "1"
      onBrightness: 10
  - id: brightness
    channelTypeUID: mqtt:dimmer
    label: Brightness
    description: null
    configuration:
      retained: false
      postCommand: false
      min: 0
      formatBeforePublish: '{"brightness": %s}'
      max: 255
      commandTopic: zigbee2mqtt/RGBW3/set
      step: 1
      stateTopic: zigbee2mqtt/RGBW3
      transformationPattern: JSONPATH:$.brightness
      off: "0"
      on: "1"
  - id: color_temperature
    channelTypeUID: mqtt:dimmer
    label: Color Temperature
    description: null
    configuration:
      retained: false
      postCommand: false
      min: 150
      formatBeforePublish: '{"color_temp": %s}'
      max: 500
      commandTopic: zigbee2mqtt/RGBW3/set
      step: 1
      stateTopic: zigbee2mqtt/RGBW3
      transformationPattern: JSONPATH:$.color_temp
      off: "0"
      on: "1"
  - id: link_quality
    channelTypeUID: mqtt:number
    label: Link Quality
    description: null
    configuration:
      retained: false
      postCommand: false
      unit: "%"
      min: 0
      formatBeforePublish: "%s"
      max: 100
      step: 1
      stateTopic: zigbee2mqtt/RGBW3
      transformationPattern: JSONPATH:$.linkquality∩JS:lqi2percent.js

Well you have to change the bridge UID and MQTT topics to fit your devices, but otherwise it should be complete. And I convert the link quality to percentage, because everything else, at least in my installation, uses percentage but this attribute ranges from 0 to 255.

For completion you’re missing three files:

  • colorjson2csv.js
  • colorcsv2json.js
  • lqi2percent.js

These files have to be created in the directory <openhab_home_dir>/conf/transform
All scripts have a variable “logger” which you can use to log stuff to the openhab logfiles. My code could be shortened a lot but well, it works.

colorjson2csv.js

(function(i) {
        var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.transform.colorjson2csv');
        var colorFromMqtt = JSON.parse(i);
        var result = colorFromMqtt.color.x + "," + colorFromMqtt.color.y + "," + (colorFromMqtt.brightness/254*100);
        return result;
})(input)

colorcsv2json.js

(function(i) {
        var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.transform.colorcsv2json');
        var parts = i.split(',');
        var x = parseFloat(parts[0]);
        var y = parseFloat(parts[1]);
        var b = parseFloat(parts[2]);
        b = Math.floor(b/100 * 254);

        var response = { "color": { "x": x, "y":y }, "brightness":b };
        var result = JSON.stringify(response);
        return result;
})(input)

lqi2percent.js

(function(lqi) {
    return Math.round(((lqi / 255) * 100));
})(input)

Now after you have defined the MQTT thing, you need to create all items. That is straight forward and does not need any additional configuration or transformation, as everything is done by the thing itself.

If you have any questions, feel free to ask.

Regards
Thomas