Zigbee Colour LED configuration

  • Platform information:
    • Hardware: Pi 4
    • OS: Openhabian
    • Java Runtime Environment: Java 17
    • openHAB version: 4.2.2
  • Issue of the topic: I’ve purchased a number of these: Oz Smart Things DL41-03-10-R-ZB control via MQTT | Zigbee2MQTT and can control them via zigbee2mqqt. I have other zigbee devices (switches and sensors) that are all working fine.
  • I need to expose these to Google Home so they can be voice controlled.
  • I can configure a ‘Generic MQTT Thing’ to get the relevant channels, and create items from these, but I’m having trouble with the colour channel, and working out which item to tag as ga.
  • I have scrapped everything, and am hoping for a kind soul to walk me through setting this up

I have Ikea color lamps with the same description in z2m.

My working thing code tab is:

  - id: color_rgb
    channelTypeUID: mqtt:color
    label: Farbe
    configuration:
      retained: false
      postCommand: false
      formatBeforePublish: '{"color":{"r":%1$d,"g":%2$d,"b":%3$d}}'
      commandTopic: zigbee2mqtt/IkeaColorlampe/set
      colorMode: RGB
      stateTopic: zigbee2mqtt/IkeaColorlampe/get
      off: "0"
      on: "1"

Link to a color item it should work.
With Google home I can’t help you.
Greets.
PS Maybe the stateTopic is wrong. I change the color only with openhab… So I don’t need the stateTopic part.

the .../get topic is to request a specific state, so this topic is most likely not to be correct at all.
MQTT Explorer should show the correct stateTopic (payload should be json with complete state).

I would expect something like

{"state":"ON","brightness":100,{"color":{"x":0.123,"y":0.123}}}

(be aware of x/y value instead of rgb). If that’s correct, then the configuration should be something like this:

- id: color_rgb
    channelTypeUID: mqtt:color
    label: Farbe
    configuration:
      retained: false
      postCommand: false
      formatBeforePublish: '{"color":{"x":%1$d,"y":%2$d},"brightness":%3$d}'
      commandTopic: zigbee2mqtt/IkeaColorlampe/set
      colorMode: XYY
      transformationPattern: JS:|in=JSON.parse(input); in.color.x+','+in.color.y+','+in.brightness;
      stateTopic: zigbee2mqtt/IkeaColorlampe
      on: ON
      off: OFF

Thanks, still banging my head against a wall, but the effort is appreciated.

Thanks for the help. The status topic looks like this:

{
    "brightness": 10,
    "color": {
        "x": 0.1355,
        "y": 0.0399
    },
    "color_mode": "xy",
    "color_power_on_behavior": null,
    "color_temp": 500,
    "do_not_disturb": false,
    "linkquality": 131,
    "state": "ON"
}

My thing now looks like this:

UID: mqtt:topic:c0a82307a5:af6ca82f6a
label: OzLight1
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:c0a82307a5
channels:
  - id: OzLight1_Colour
    channelTypeUID: mqtt:color
    label: Colour
    description: Test Colour Channel
    configuration:
      retained: false
      postCommand: false
      formatBeforePublish: '{"color":{"x":%1$d,"y":%2$d},"brightness":%3$d}'
      commandTopic: zigbee2mqtt/OzLight1/set
      colorMode: XYY
      transformationPattern: JS:|in=JSON.parse()input; in.color.x+','+in.color.y+','+in.brightness;
      stateTopic: zigbee2mqtt/OzLight1
      off: OFF
      on: ON

And attached a Color item to it, but changing the sliders doesn’t change the light, but it does send MQTT : In MQTT Explorer I see messages published to zigbee2mqtt/OzLight1/set but just 3 numbers (0.359299,0.336995,24.00). I’m thinking that the formatBeforePublish isn’t working?

Some progress!

formatBeforePublish: '{"color":{"x":%1$f,"y":%2$f},"brightness":%3$f}'

works, but brightness goes from 0 - 100, where the light expects 0-254

That’s a bit tricky… but no real problem :slight_smile:

- id: color_rgb
    channelTypeUID: mqtt:color
    label: Farbe
    configuration:
      retained: false
      postCommand: false
      commandTopic: zigbee2mqtt/IkeaColorlampe/set
      colorMode: XYY
      transformationPatternOut: JS:|in=input.split(",");'{"color":{"x":'+in[0]+',"y":'+in[1]+'},"brightness":'+(in[2]*2.55)+'}';
      transformationPattern: JS:|in=JSON.parse(input); in.color.x+','+in.color.y+','+in.brightness/2.55;
      stateTopic: zigbee2mqtt/IkeaColorlampe
      on: ON
      off: OFF

Please be aware that I changed from formatBeforePublish to transformationPatternOut and also changed transformationPattern slightly, as there was a typo (I changed my posting above)
I did not test :slight_smile:

Thanks, wiil try it when I get the chance

Sadly didn’t work. Back to banging my head!

Any messages in openhab.log?

Hmm, logging crashed 2 days ago - need to sort that out first! Frontail is showing blank lines popping up, but both openhab.log and events.log last entry was from 3rd Dec

when changing the colorpicker item:

2024-12-05 10:10:16.412 [ERROR] [javascript.transformation.|835486551] - Failed to execute script: SyntaxError: <eval>:1:0 Expected an operand but found in
in=input.split(",");'{"color":{"x":'+in[0]+',"y":'+in[1]+'},"brightness":'+(in[2]*2.55)+'}';
^
<eval>:1:37 Expected an operand but found in
in=input.split(",");'{"color":{"x":'+in[0]+',"y":'+in[1]+'},"brightness":'+(in[2]*2.55)+'}';
                                     ^
        at org.graalvm.polyglot.Context.eval(Context.java:399)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426)
        at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)
        at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:52)
        ... 29 more
2024-12-05 10:10:16.413 [WARN ] [t.generic.ChannelStateTransformation] - Executing the JS-transformation failed: Failed to execute script.. Pattern: '|in=input.split(",");'{"color":{"x":'+in[0]+',"y":'+in[1]+'},"brightness":'+(in[2]*2.55)+'}';'. Value: '0.186793,0.272278,56.00'
2024-12-05 10:10:16.610 [ERROR] [javascript.transformation.|835486551] - Failed to execute script: SyntaxError: <eval>:1:0 Expected an operand but found in
in=input.split(",");'{"color":{"x":'+in[0]+',"y":'+in[1]+'},"brightness":'+(in[2]*2.55)+'}';
^
<eval>:1:37 Expected an operand but found in
in=input.split(",");'{"color":{"x":'+in[0]+',"y":'+in[1]+'},"brightness":'+(in[2]*2.55)+'}';
                                     ^
        at org.graalvm.polyglot.Context.eval(Context.java:399)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426)
        at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)
        at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:52)
        ... 29 more
2024-12-05 10:10:16.612 [WARN ] [t.generic.ChannelStateTransformation] - Executing the JS-transformation failed: Failed to execute script.. Pattern: '|in=input.split(",");'{"color":{"x":'+in[0]+',"y":'+in[1]+'},"brightness":'+(in[2]*2.55)+'}';'. Value: '0.172284,0.261031,56.00'
==> /var/log/openhab/events.log <==
2024-12-05 10:10:16.395 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'OzLight1_Colour' received command 198,61,56
2024-12-05 10:10:16.398 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'OzLight1_Colour' predicted to become 198,61,56
2024-12-05 10:10:16.399 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'OzLight1_Colour' changed from 198,60,56 to 198,61,56
2024-12-05 10:10:16.593 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'OzLight1_Colour' received command 198,70,56
2024-12-05 10:10:16.595 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'OzLight1_Colour' predicted to become 198,70,56
2024-12-05 10:10:16.599 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'OzLight1_Colour' changed from 198,61,56 to 198,70,56```



When changing via zigbee2mqtt:

2024-12-05 10:14:39.889 [ERROR] [avascript.transformation.|1011883812] - Failed to execute script: SyntaxError: <eval>:1:0 Expected an operand but found in
in=JSON.parse(input); in.color.x+','+in.color.y+','+in.brightness/2.55;
^
<eval>:1:22 Expected an operand but found in
in=JSON.parse(input); in.color.x+','+in.color.y+','+in.brightness/2.55;
                      ^
        at org.graalvm.polyglot.Context.eval(Context.java:399)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426)
        at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)
        at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:52)
        ... 31 more
2024-12-05 10:14:39.890 [WARN ] [t.generic.ChannelStateTransformation] - Executing the JS-transformation failed: Failed to execute script.. Pattern: '|in=JSON.parse(input); in.color.x+','+in.color.y+','+in.brightness/2.55;'. Value: '{"brightness":142,"color":{"h":308,"hue":308,"s":100,"saturation":100,"x":0.4066,"y":0.1643},"color_mode":"xy","color_options":{"execute_if_off":false},"color_power_on_behavior":null,"color_temp":153,"do_not_disturb":false,"linkquality":116,"state":"ON"}'
2024-12-05 10:14:40.351 [ERROR] [avascript.transformation.|1011883812] - Failed to execute script: SyntaxError: <eval>:1:0 Expected an operand but found in
in=JSON.parse(input); in.color.x+','+in.color.y+','+in.brightness/2.55;
^
<eval>:1:22 Expected an operand but found in
in=JSON.parse(input); in.color.x+','+in.color.y+','+in.brightness/2.55;
                      ^
        at org.graalvm.polyglot.Context.eval(Context.java:399)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426)
        at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)
        at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:52)
        ... 31 more
2024-12-05 10:14:40.352 [WARN ] [t.generic.ChannelStateTransformation] - Executing the JS-transformation failed: Failed to execute script.. Pattern: '|in=JSON.parse(input); in.color.x+','+in.color.y+','+in.brightness/2.55;'. Value: '{"brightness":142,"color":{"h":308,"hue":308,"s":100,"saturation":100,"x":0.4066,"y":0.1643},"color_mode":"xy","color_options":{"execute_if_off":false},"color_power_on_behavior":null,"color_temp":153,"do_not_disturb":false,"linkquality":120,"state":"ON"}'
2024-12-05 10:14:40.378 [ERROR] [avascript.transformation.|1011883812] - Failed to execute script: SyntaxError: <eval>:1:0 Expected an operand but found in
in=JSON.parse(input); in.color.x+','+in.color.y+','+in.brightness/2.55;
^
<eval>:1:22 Expected an operand but found in
in=JSON.parse(input); in.color.x+','+in.color.y+','+in.brightness/2.55;
                      ^
        at org.graalvm.polyglot.Context.eval(Context.java:399)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426)
        at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)
        at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:52)
        ... 31 more
2024-12-05 10:14:40.379 [WARN ] [t.generic.ChannelStateTransformation] - Executing the JS-transformation failed: Failed to execute script.. Pattern: '|in=JSON.parse(input); in.color.x+','+in.color.y+','+in.brightness/2.55;'. Value: '{"brightness":142,"color":{"h":308,"hue":308,"s":100,"saturation":100,"x":0.4066,"y":0.1643},"color_mode":"xy","color_options":{"execute_if_off":false},"color_power_on_behavior":null,"color_temp":153,"do_not_disturb":false,"linkquality":112,"state":"ON"}'

Current code for the Thing:

UID: mqtt:topic:c0a82307a5:b05828ee3b
label: OzLight1
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:c0a82307a5
channels:
  - id: OzLight1_Colour
    channelTypeUID: mqtt:color
    label: Colour
    description: Test Colour Channel
    configuration:
      postCommand: false
      retained: false
      transformationPatternOut: JS:|in=input.split(",");'{"color":{"x":'+in[0]+',"y":'+in[1]+'},"brightness":'+(in[2]*2.55)+'}';
      commandTopic: zigbee2mqtt/OzLight1/set
      colorMode: XYY
      transformationPattern: JS:|in=JSON.parse(input);
        in.color.x+','+in.color.y+','+in.brightness/2.55;
      stateTopic: zigbee2mqtt/OzLight1
      off: OFF
      on: ON
  - id: brightness
    channelTypeUID: mqtt:dimmer
    label: Brightness
    description: ""
    configuration:
      commandTopic: zigbee2mqtt/OzLight1/set/brightness
      min: 0
      stateTopic: zigbee2mqtt/OzLight1/get
      max: 254

Turns out, JS inline doesn’t like variables…
Please try

      transformationPatternOut:
        - JS:| '{"color":{"x":' + input.split(',')[0] + ',"y":' +
          input.split(',')[1] + '},"brightness":' + (input.split(',')[2]*2.55) +
          '}';
      formatBeforePublish: "%s"
      commandTopic: zigbee2mqtt/OzLight1/set
      colorMode: XYY
      transformationPattern:
        - JS:| JSON.parse(input).color.x + ',' + JSON.parse(input).color.y + ','
          + JSON.parse(input).brightness/2.55;
      stateTopic: zigbee2mqtt/OzLight1

Edit: :slight_smile: in seems to be a key word in JS…

      transformationPatternOut:
        - JS:|data = input.split(','); '{"color":{"x":' + data[0] + ',"y":' +
          data[1] + '},"brightness":' + (data[2]*2.55) + '}';
      transformationPattern:
        - JS:|data = JSON.parse(input); data.color.x + ',' + data.color.y + ','
          + data.brightness/2.55;
1 Like

@Udo_Hartmann

many Thx!

finally my status from my RGB Stripes is also synchronized in OH

Woot! Thanks Udo, that did it
Now on with my journey of connecting it to Google Assistant!

Dammit spoke too soon. Is there a simple way to round the x,y values to 3 decimal places?

My guess: (myValue * 1000).intValue)/1000 could work.

Sadly not. breaking it down by parts:

(input.split(',')[0])*1000)

gives the expected result, but

((input.split(',')[0])*1000).intValue

is undefined. the MQTT formed is

{"color":{"x":undefined,"y":0.089357},"brightness":162.56}

I’m still not convinced that this is my current problem. The lights are supposed to enter color_temp mode automatically when set to an appropriate value, but that isn’t happening. Not sure if it isn’t close enough (due to no rounding), or if I have a problem with the color_temp channel. I’ll play around for a while & see where I get. Might spin up home assistant to control an identical light and see what’s different on the MQTT side.

Thanks for your help so far. If I ever get it working(!), I’ll put together a write up.

1 Like

Another option:

Math.round(input.split(',')[0],3)

where 3 is the amount of decimal places to round to.

Ugh: chasing the wrong issue!

I spanup a Home Assistant instance to compare, used the existing Zigbee2MQTT instance. Connected an identical light and exposed it to Google (very simple: it just detected it automatically). In Google Home, put them in the same room and used the grouped controls to play around with them while I watched MQTT Explorer.

Changing to (non white) colours is fine, changing brightness is fine, but changing to a white colour (ie “Ivory” or “Ghost White” or “Cool White”) does not work for the OpenHab controlled light.

Changing from “Blue” to “Ivory” the Home Assistant controlled light gets:

{"state":"ON","color_temp":166}

The OpenHab controlled light gets:

{"color":{"x":0.332707,"y":0.344436},"brightness":254}

Which is not Ivory (or even close!)

I’m guessing I have an issue with the Google Assistant metadata

value: SpecialColorLight
config:
  colorUnit: mired
  colorTemperatureRange: 130,500
  roomHint: Lounge