MiLight Items, JSON, Dimmers and Colours (An FYI)

I write this hopefully to help someone else but mostly so I have somewhere to store it :slight_smile:

I have for a while now had a clunky setup with MiLight and rules and getting them to work. Today, in a hungover state I spent some time cleaning this up.

This relies on the excellent gateway provided by Chris Mullins

I have items for each group.
I use transforms to change the mode and brightness
I have a rule to change the colour (WIP to a transform if I can)

Group:Dimmer g_Wetroom_Dimmer "Wetroom Dimmer"
Group:Color g_Wetroom_Color "Wetroom Color"

Color MiLight_Item_Color_Group_1 "Wetroom Shower - Color"   <colorlight>  (g_Wetroom_Color)
Color MiLight_Item_Color_Group_2 "Wetroom Main - Color"     <colorlight>  (g_Wetroom_Color)
Color MiLight_Item_Color_Group_3 "Bathroom Main - Color"    <colorlight>
Color MiLight_Item_Color_Group_4 "Spare - Color"            <colorlight>

Dimmer MiLight_Item_Dimmer_Group_1 "Wetroom Shower - Dimmer"    <light> (g_Wetroom_Dimmer) {mqtt=">[mqtt:home/lights/0x9B55/RGBW/1:command:*:JS(milight.dimmer.map.js)]"}
Dimmer MiLight_Item_Dimmer_Group_2 "Wetroom Main - Dimmer"      <light> (g_Wetroom_Dimmer) {mqtt=">[mqtt:home/lights/0x9B55/RGBW/2:command:*:JS(milight.dimmer.map.js)]"}
Dimmer MiLight_Item_Dimmer_Group_3 "Bathroom Main - Dimmer"     <light>  {mqtt=">[mqtt:home/lights/0x9B55/RGBW/3:command:*:JS(milight.dimmer.map.js)]"}
Dimmer MiLight_Item_Dimmer_Group_4 "Spare - Dimmer"             <light>  {mqtt=">[mqtt:home/lights/0x9B55/RGBW/4:command:*:JS(milight.dimmer.map.js)]"}

String MiLight_Item_Mode_Group_1 "Wetroom Shower - Mode"    {mqtt=">[mqtt:home/lights/0x9B55/RGBW/1:command:*:MAP(milights.map)]"}
String MiLight_Item_Mode_Group_2 "Wetroom Main - Mode"      {mqtt=">[mqtt:home/lights/0x9B55/RGBW/2:command:*:MAP(milights.map)]"}
String MiLight_Item_Mode_Group_3 "Bathroom Main - Mode"     {mqtt=">[mqtt:home/lights/0x9B55/RGBW/3:command:*:MAP(milights.map)]"}
String MiLight_Item_Mode_Group_4 "Spare - Mode"             {mqtt=">[mqtt:home/lights/0x9B55/RGBW/4:command:*:MAP(milights.map)]"}

Colour Rules. Hoping to refactor this into a transform.

import org.eclipse.xtext.xbase.lib.Functions

val Functions$Function3 sendColor = [
	GenericItem 	item,
	String			controller,
	String			group
	| 

	var light = (item.state as HSBType)
	var RGBRed = (light.red * 2.55 as int).toString("%.3f")
	var RGBGreen = (light.green * 2.55 as int).toString.toString("%.3f")
	var RGBBlue = (light.blue * 2.55 as int).toString.toString("%.3f")
	var Brightness = (light.brightness * 2.55 as int).toString.toString("%.3f")
	
	val packet = "{\"brightness\":" + Brightness + ",\"bulb_mode\":\"color\",\"color\":{\"r\":" + RGBRed + ",\"g\":" + RGBGreen + ",\"b\":" + RGBBlue + "}}"
	publish("mqtt","home/lights/" + controller + "/RGBW/" + group, packet)
]

rule "milight group 1"
when
	Item MiLight_Item_Color_Group_1 received command
then
	sendColor.apply(MiLight_Item_Color_Group_1, "0x9B55", "1")
end

rule "milight group 2"
when
	Item MiLight_Item_Color_Group_2 received command
then
	sendColor.apply(MiLight_Item_Color_Group_2, "0x9B55", "2")	
end

rule "milight group 3"
when
	Item MiLight_Item_Color_Group_3 received command
then
	sendColor.apply(MiLight_Item_Color_Group_3, "0x9B55", "3")
end

rule "milight color group 4"
when
	Item MiLight_Item_Color_Group_4 received command
then
	sendColor.apply(MiLight_Item_Color_Group_4, "0x9B55", "4")	
end


milight.dimmer.map.js (Don’t forget you need to install the JS transform from the paper UI.

(function(i) {
    var retVal = 0

    if (input == "ON"){
        retVal = 255
    }
    else if (input == "OFF"){
        retVal = 0
    }
    else if(input >= 0)
    {
	    retVal = input / 100 * 255;
    }
    else
    {
	    retVal = 0;
    }

    return "{\"brightness\":\"" + Math.floor(retVal) + "\"}"
})(input)

milight.map. This is for the various modes.

1={"status":"ON"}
0={"status":"OFF"}
SET_WHITE_MODE={"command":"set_white"}
SET_NIGHT_MODE={"command":"night_mode"}
SET_RGB_MODE={"command":"color"}
SET_SCENE_MODE={"command":"scene"}
SET_DISCO_MODE={"mode":"8"}
SET_FLASH_MODE={"mode":"3"}

The above replaces reams and reams of rules and lambdas which really killed the RPi 3 I am running this on. Doing anything to them was very slow and caused people to doubt the click and go clicky-click-click which helped nothing…

While I post it here in the hopes that it helps someone else, I would appreciate feedback in how I might make it simpler.

thanks.
Crispin

Why not try the binding found here. I use a raspberry pi2 and it not only runs openhab but also mosquito and I have 23 milight globes all running great with individual control from the one hub.