Integrate Shelly RGBW2 with MQTT in openHab

Hi,
I’m trying to interface OH2 with a Shelly RGBW2 in white mode without success.
I’ve modified Your files to match the white mode parameters (brightness and on/ff) for channel 3 but with no success at all.
If possible could be a good plus to have a feedback of the status of the device (brightness value and on/off status) so if the shelly is turned on by an external switch in OH is on.
A plus could be to have a device online status control that enable or disable panel controls…

I’m a total noob in OH, so help is very needed.

The opensource milight hub is very similar and uses json via MQTT, so feel free to look and use this binding source code as an example. I also attempted doing it with rules which are also posted on this forum and look similar to yours.

I know ppl will recommend creating a thing in the MQTT binding, but that was not around when I created the binding and I wanted to create more in depth features like halogen emulation and other features which was better in its own binding.

It probably is a good idea to come up with a better way to handle JSON as it is very simple in home assistant to handle it so a lot of projects go that direction which creates another hurdle for Openhab especially when they use 0-255 values and we need 0-100 values.

Hey Diego, in white mode you need to rework the scripts for shure…
Did you consider the different rest API for white mode here:

https://shelly-api-docs.shelly.cloud/#rgbw2-mqtt

I would create dimmer items for each channel and than also a script for each channel to build and send the rest command strings like described abouve…

1 Like

Thank you,
I’ve tried to rework your work for command the white mode using API’s (IsOn & Brightness are the only parameters) but with no luck for now…
Now I would try to create things in PaperUI via JSONPATH tranformation and see in works…

Was a lot of try and error also in my case… Post your results!

Just for clarify in your OP, the codes have to be saved in:

1 code (Thing topic WohnzimmerTisch "WohnzimmerTisch" @ "Wohnzimmer"....): .things file
2 code (Number Gain_WohnzimmerTisch "Wohnzimmertisch [%d %%]"...): .things file
3 code (Dimmer Dimmer_WohnzimmerTisch "Wohnzimmertisch [%d %%]" [ "Lighting" ]...): .items file
4 code (rule "Set Shelly RGBW2 LEDs from Color item using ColorPicker"...): .script file
5 code (/* Shelly RGBW2 Dimmer update */...): .script file
6 code (/* Shelly RGBW2 value update */...): .script file
7 code (Slider item=Dimmer_WohnzimmerTisch switchSupport icon="slider"...): .items file

Right?

Number in also items and rules in 4,5,6 in .rules other wise it will not work!

1 Like

Hi,
I’ve modified your files , but I can’t see controls in PaperUI Controls tab nor BasicUI or ClassicUI.
I’ve read API’s doc a lot of time but I don’t understand at all the JSon string…
https://shelly-api-docs.shelly.cloud/?shell#rgbw2

I’m a bit confused…

These are my files:

Sottopensile .things:

    Thing topic Sottopensile "Sottopensile" @ "Soggiorno" {
    Channels:
        Type string : ison        "IsOn"              [ stateTopic="shellies/shellyrgbw2-661B1B/white/3/status", transformationPattern="JSONPATH:$.ison"]
        Type number : brightness  "Brightness"        [ stateTopic="shellies/shellyrgbw2-661B1B/white/3/status", transformationPattern="JSONPATH:$.brightness"]
        Type number : power       "Power"             [ stateTopic="shellies/shellyrgbw2-661B1B/white/3/status", transformationPattern="JSONPATH:$.power"]
        Type string : cmd         "Command"           [ commandTopic="shellies/shellyrgbw2-661B1B/white/3/set" ]
}

Sottopensile_Status .items:

//
// Sottopensile
//
// Status
Number Brightness_BL  "Sottopensile [%d %%]" { channel="mqtt:topic:mosquitto:Sottopensile:brightness" }
Number Power    "Consumo [%.2f W]" { channel="mqtt:topic:mosquitto:Sottopensile:power" }
String IsOn_BL    "Sottopensile [%s]" { channel="mqtt:topic:mosquitto:Sottopensile:ison" }
String Command_BL    "Comando [%s]" { channel="mqtt:topic:mosquitto:Sottopensile:cmd" }

Dimmer_BL .items:

// Controllo
Dimmer Dimmer_BL  "Sottopensile [%d %%]" [ "Intensita" ]

Dimmer_BL .rules:

/* Shelly RGBW2 Dimmer update */
rule "Sottopensile_DimEvent"
when
	Item Dimmer_BL changed
then
	var jsonString = "{"
	val Number dimValue = Dimmer_BL.state as DecimalType;
	jsonString = jsonString + ",\"brightness\":" + dimValue.toString
	
	if (dimValue > 0)
			jsonString = jsonString + "\"turn\":" + "\"on\""
	else
			jsonString = jsonString + "\"turn\":" + "\"off\""
	
	jsonString = jsonString + "}"
	sendCommand(Command_BL, jsonString)
end

/* Shelly RGBW2 value update */
rule "BL_BrightEvent"
when
	Item Brightness_BL changed
then
	val Number dimValue = Brightness_BL.state as DecimalType;
	sendCommand(Dimmer_BL, dimValue)
end

Controlli_BL .items:

Slider item=Dimmer_BL switchSupport icon="slider"
Text item=Power icon=""

Can You help me, please?

Hi Diego, sorry for late response. May be to late!
But to see something in basic ui you must put slider and text in *.sitemap files

Hi guys, please check out this thread: Shelly Binding
I’m working on an optimized binding for the complete Shelly series of devices. Testers welcome :slight_smile:

I have do it, with Javascript.

My item : Color grill_eg_terasse “Grill” (geg) {mqtt="<[sari:shellies/shellyrgbw2-B0983A/color/0/status:state:JS(rgbjson_hsv.js)],>[sari:shellies/shellyrgbw2-B0983A/color/0/command:command:ON:MAP(shellyonoff.map)],>[sari:shellies/shellyrgbw2-B0983A/color/0/command:command:OFF:MAP(shellyonoff.map)],>[sari:shellies/shellyrgbw2-B0983A/color/0/set:command:*:JS(hsv_rgbjson.js)]"}

rgbjson_hsv.js

// Wrap everything in a function
(function(i) {
var min, max, delta, r, g, b, h, s, v, v1, w, erg=“falsch”;

// r,g,b-Werte zwischen 0 und 1
// h = [0,360], s = [0,1], v = [0,1]
// Wenn s == 0, dann h = -1 (undefined)

obj=JSON.parse(i);
r=Number(obj.red)/255;
g=Number(obj.green)/255;
b=Number(obj.blue)/255;
w=Number(obj.white)
v1=Number(obj.gain)
min=g; if (r<min) {min=r;} if (b<min) {min=b;}
max=g; if (r>max) {max=r;} if (b>max) {max=b;}
delta = max - min;
if( max != 0 ) s = delta / max;
else { s = 0; h = 0; } // r = g = b = 0
if (max == min) { h = 0; s = 0; } // hier ist alles Grau
if (s>0)
{
if( r == max ) { h = ( g - b ) / delta; } // zwischen Gelb und Magenta
else if( g == max ) { h = 2 + ( b - r ) / delta; } // zwischen Cyan und Gelb
else { h = 4.0 + ( r - g ) / delta;} // zwischen Magenta und Zyan
h *= 60; // degrees
if( h < 0 ) h += 360;
}

erg=Number(h)+","+Number(s*100)+","+Number(v1);
//erg=Number(w);
return erg;
})(input)
// input variable contains data passed by openhab

and hsv_rgbjson.js

/ Wrap everything in a function
(function(i) {
var p1, p2, hi, f, p, q, t, r, g, b, h, s, v, v1, w, erg=“falsch”;

if ((i=="ON") || (i=="OFF")) {erg="";}
	
else
	{
	p1= i.indexOf(",");
	p2= i.indexOf(",",p1+1);
	h=Number(i.substring(0,p1));
	s=Number(i.substring(p1+1,p2));
	v1=Number(i.substring(p2+1,99));
	s=s/100;
	//v=v1/100;
	v=1;
	hi=Math.floor(h/60)
	f=(h/60-hi);
	p=v*(1-s);
	q=v*(1-s*f);
	t=v*(1-s*(1-f));
	if ((hi==0) || (hi==6)) {r=v; g=t; b=p; }
	else if (hi==1) {r=q; g=v; b=p;} 
	else if (hi==2) {r=p; g=v; b=t;}
	else if (hi==3) {r=p; g=q; b=v;}
	else if (hi==4) {r=t; g=p; b=v;} 
	else {r=v; g=p; b=q;}
	w=0;
	if (s<0.5) {w=(0.5-s)*2*v1/100;}
	erg="{\"red\":" + (r*255) + ",\"green\":" + (g*255) + ",\"blue\":" + (b*255) + ",\"white\":" + (w*255) + ",\"gain\":" + (v1) + "}";
	}

return erg;

})(input)
// input variable contains data passed by openhab

the onoff maps only convert upper lower case

After a saturation below 50% , I start the White Led also. At Saturation 0 the White is 100%. So I dont need so mutch Items or Swtiches to Controll the RGBW.

I know its not perfect, but it works for me.

Can you add code fences to your post, please?

does anybody have an improvement on the op setup for adding functionability for rgbw strips?
he noted his setup didnt have the things/items and rules relating to the white channel?
just ordered a shelly rgbw so will be looking to setup soon :slight_smile:

@Taffy: Meanwhile is also a shelly Binding available. I think this should make life easier. I never tried because this is my only device with original shelly firmware.

I am not sure what to do. Wether to keep stock firmware and make your setup work for me. Use the Shelly binding or flash it with tasmota!

God evening @ all
I’m trying to use the shelly rgbw with the embedded broker created via GUI. Can anybody point my how to get the - transformationPattern=“JSONPATH:$.ison” in one line with - shellies/shellyrgbw2-xxxxxx/color/0/status in the definition of the generic MQTT Thing via GUI

Thanks in advance

Ok in the mean time I tryied to help my self. I managed to read the JSON Payload adding a channel to the generic MQTT Thing
I created an item:
`

String LGTV_RGBW_Status "Sideboardstatus [%s]" { channel="mqtt:topic:82734824:shelly_RGBW_status"[transformationPattern="JSONPATH:$.ison"] }

`
on the site map a can see the whole JSON string with:

Text item=LGTV_RGBW_Status

normaly it should only show the status of “ison” but it shows the whole string.
I also tried:

String LGTV_RGBW_IsOn "IsOn [%s]" { channel="mqtt:topic:82734824:shelly_RGBW_status"[profile="transform:JSONPATH", function="$.ison"]}

found in here: https://www.openhab.org/addons/transformations/jsonpath/
Unfortunately this also does not work.
When I switch on/off the shelly via its webinterface the log says that

LGTV_RGBW_Status

changes but

LGTV_RGBW_IsOn

does not appear in the log.

Please can anyone give me a hint what I’m doing wrong.

What openHAB version are you using?

Thank you for your reply. I’m using Version 2.4 on a Raspberry with raspbian.

Solved! Forgott to install the JSON Transformation via paperUI :roll_eyes: