Dear community!
A few days ago I got my new Shelly RGBW2 and fighted to get it working with the MQTT binding in openHAB 2.4
Here the result! I hope its helpful for others. But I am also interested to get feedback, if others solved it in more simple way.
The MQTT API can be found here: RGBW2 MQTT API
After experimenting around, I finally created state topics for all parameters in the JSON string send by
shellies/shellyrgbw2-5A36B7/color/0/status
and use the command
shellies/shellyrgbw2-/color/0/set
to change the settings. But this mean, I need to build the complete JSON string always completly.
So, I created the following thing:
Thing topic WohnzimmerTisch "WohnzimmerTisch" @ "Wohnzimmer" {
Channels:
Type string : ison "IsOn" [ stateTopic="shellies/shellyrgbw2-5A36B7/color/0/status", transformationPattern="JSONPATH:$.ison"]
Type string : overpower "OverPower" [ stateTopic="shellies/shellyrgbw2-5A36B7/color/0/status", transformationPattern="JSONPATH:$.overpower"]
Type string : mode "Mode" [ stateTopic="shellies/shellyrgbw2-5A36B7/color/0/status", transformationPattern="JSONPATH:$.mode"]
Type number : gain "Gain" [ stateTopic="shellies/shellyrgbw2-5A36B7/color/0/status", transformationPattern="JSONPATH:$.gain"]
Type number : red "Red" [ stateTopic="shellies/shellyrgbw2-5A36B7/color/0/status", transformationPattern="JSONPATH:$.red"]
Type number : green "Green" [ stateTopic="shellies/shellyrgbw2-5A36B7/color/0/status", transformationPattern="JSONPATH:$.green"]
Type number : blue "Blue" [ stateTopic="shellies/shellyrgbw2-5A36B7/color/0/status", transformationPattern="JSONPATH:$.blue"]
Type number : white "White" [ stateTopic="shellies/shellyrgbw2-5A36B7/color/0/status", transformationPattern="JSONPATH:$.white"]
Type number : effect "Effect" [ stateTopic="shellies/shellyrgbw2-5A36B7/color/0/status", transformationPattern="JSONPATH:$.effect"]
Type number : power "Power" [ stateTopic="shellies/shellyrgbw2-5A36B7/color/0/status", transformationPattern="JSONPATH:$.power"]
Type string : cmd "Command" [ commandTopic="shellies/shellyrgbw2-5A36B7/color/0/set" ]
}
To have all the values available, I connected the following items to this channels:
//
// Wohnzimmertisch
//
// Status
Number Gain_WohnzimmerTisch "Wohnzimmertisch [%d %%]" { channel="mqtt:topic:mosquitto:WohnzimmerTisch:gain" }
String IsOn_WohnzimmerTisch "Wohnzimmertisch [%s]" { channel="mqtt:topic:mosquitto:WohnzimmerTisch:ison" }
String OverPower_WohnzimmerTisch "Wohnzimmertischoverpower [%s]" { channel="mqtt:topic:mosquitto:WohnzimmerTisch:overpower" }
String Mode_WohnzimmerTisch "Wohnzimmertischmode [%s]" { channel="mqtt:topic:mosquitto:WohnzimmerTisch:mode" }
Number Effect_WohnzimmerTisch "Wohnzimmertischeffekt [%s]" { channel="mqtt:topic:mosquitto:WohnzimmerTisch:effect" }
Number Red_WohnzimmerTisch "Wohnzimmertisch rot [%d]" { channel="mqtt:topic:mosquitto:WohnzimmerTisch:red" }
Number Green_WohnzimmerTisch "Wohnzimmertisch blau [%d]" { channel="mqtt:topic:mosquitto:WohnzimmerTisch:green" }
Number Blue_WohnzimmerTisch "Wohnzimmertisch grĂĽn [%d]" { channel="mqtt:topic:mosquitto:WohnzimmerTisch:blue" }
Number White_WohnzimmerTisch "Wohnzimmertisch weiss [%d]" { channel="mqtt:topic:mosquitto:WohnzimmerTisch:white" }
Number Power_WohnzimmerTisch "Wohnzimmertischverbrauch [%.2f W]" { channel="mqtt:topic:mosquitto:WohnzimmerTisch:power" }
String Command_WohnzimmerTisch "Wohnzimmertischkommando [%s]" { channel="mqtt:topic:mosquitto:WohnzimmerTisch:cmd" }
To control the brightness and color with rules, I created additionally a Color and Dimmer item
// Control
Dimmer Dimmer_WohnzimmerTisch "Wohnzimmertisch [%d %%]" [ "Lighting" ]
Color Color_Wohnzimmertisch "Wohnzimmer Tisch" <colorlight> (gWohnzimmer)
For changing the color, I wrote a script which reacts on changes of the Color item.
It extracts the new color and builds a complete JSON string which is than send to the command item.
rule "Set Shelly RGBW2 LEDs from Color item using ColorPicker"
when
Item Color_Wohnzimmertisch changed
then
var jsonString = "{"
val int red = (Color_Wohnzimmertisch.state as HSBType).red.intValue
val int green = (Color_Wohnzimmertisch.state as HSBType).green.intValue
val int blue = (Color_Wohnzimmertisch.state as HSBType).blue.intValue
var int b = (Color_Wohnzimmertisch.state as HSBType).getBrightness.intValue
if (b > 0)
jsonString = jsonString + "\"turn\":" + "\"on\""
else
jsonString = jsonString + "\"turn\":" + "\"off\""
jsonString = jsonString + ",\"red\":" + red.toString
jsonString = jsonString + ",\"green\":" + green.toString
jsonString = jsonString + ",\"blue\":" + blue.toString
jsonString = jsonString + ",\"white\":" + White_WohnzimmerTisch.state.toString
jsonString = jsonString + ",\"gain\":" + b.toString
jsonString = jsonString + ",\"effect\":" + Effect_WohnzimmerTisch.state.toString
jsonString = jsonString + "}"
sendCommand(Command_WohnzimmerTisch, jsonString)
//logInfo("LED_RGB_Values.rules", "R:" + red + " G:" + green + " B:" + blue)
//logInfo("LED_RGB_Values.rules", "Brightness:" + b)
end
Finally I created two scripts for the Dimmer item. One if the dimmer value is changed by openHAB and one if the dimmer value is changes by the Shelly App from outside:
/* Shelly RGBW2 Dimmer update */
rule "WohnzimmerTisch_DimEvent"
when
Item Dimmer_WohnzimmerTisch changed
then
var jsonString = "{"
val Number dimValue = Dimmer_WohnzimmerTisch.state as DecimalType;
if (dimValue > 0)
jsonString = jsonString + "\"turn\":" + "\"on\""
else
jsonString = jsonString + "\"turn\":" + "\"off\""
jsonString = jsonString + ",\"red\":" + Red_WohnzimmerTisch.state.toString
jsonString = jsonString + ",\"green\":" + Green_WohnzimmerTisch.state.toString
jsonString = jsonString + ",\"blue\":" + Blue_WohnzimmerTisch.state.toString
jsonString = jsonString + ",\"white\":" + White_WohnzimmerTisch.state.toString
jsonString = jsonString + ",\"gain\":" + dimValue.toString
jsonString = jsonString + ",\"effect\":" + Effect_WohnzimmerTisch.state.toString
jsonString = jsonString + "}"
sendCommand(Command_WohnzimmerTisch, jsonString)
end
/* Shelly RGBW2 value update */
rule "WohnzimmerTisch_GainEvent"
when
Item Gain_WohnzimmerTisch changed
then
val Number dimValue = Gain_WohnzimmerTisch.state as DecimalType;
sendCommand(Dimmer_WohnzimmerTisch, dimValue)
end
It works fine now!
I can control the brightness and state over Alexa (hue emulation) and with sitemap:
Slider item=Dimmer_WohnzimmerTisch switchSupport icon="slider"
Colorpicker item=Color_Wohnzimmertisch icon="colorpicker"
Text item=Power_WohnzimmerTisch icon=""
also HABpanel works fine.
But I think, it is a lot of code for such a simple thing.
I also did not mange to update the Color Item in OpenHAB when the color is changed over the Shelly app. I did’t find any way to update only the rgb values of the color item. Does anyone have a hint?
This code is done for a rgb led stripe. For rgbw it must be adapted. Hope it helps someone…