CCT LED Stripes: convert brightness and temperature to cold white and warm white

image

With this template you can control CCT LED stripe’s connected to an actuator like Shelly RGBW2. It converts temperature and brightness into cold white (cw) and warm white (ww) values.

Language:
Rules DSL

Usage & Requirements:
_brightness and _temperature items are proxy items. They are controled by MainUI oh-slider widgets.
You need 4 Items per stripe and add all _brightness and _temperature items to a single group, e.g.:

Group     gCCT_LED            "All CCT LEDs"
Dimmer    LED1_brightness     "Brightness LED1"             (gCCT_LED)
Dimmer    LED1_temperature    "Temperature LED1"            (gCCT_LED)
Dimmer    LED1_cw             "cold white Channel"          //do NOT add to group!
Dimmer    LED1_ww             "warm white Channel"          //do NOT add to group!
Dimmer    LED2_brightness     "Brightness LED2"             (gCCT_LED)
Dimmer    LED2_temperature    "Temperature LED2"            (gCCT_LED)
Dimmer    LED2_cw             "cold white Channel"          //do NOT add to group!
Dimmer    LED2_ww             "warm white Channel"          //do NOT add to group!

Items _cw and _ww are items linked to your actuator’s channels. Do not add them to your UI widget. Please make sure that all items follow the same syntax like in this example or change the rule’s variables in the top.

Screenshots

A simple widget that controls brightness and temperature and the rule converts their values to correct ww and cw values.

Changelog

Version 0.1

  • initial release

Resources

import org.openhab.core.model.script.ScriptServiceUtil
val String filename = "CCT_LED.rules"
val String strSuffixSeparator = "_"                     //Separator: 1 unique separator
val String strSuffixBrightness = "brightness"           //Suffix: at your choice
val String strSuffixTemperature = "temperature"         //Suffix: at your choice

rule "CCT_LED"
when
    Member of gCCT_LED changed
then
    logInfo(filename, "Item '{}' received command {}",triggeringItem.name,triggeringItem.state)
    var Number iNewCwState     //New value for cold white channel
    var Number iNewWwState     //New value for warm white channel
    val String strThing = triggeringItem.name.toString.split(strSuffixSeparator).get(0)     //Get Name of "Thing", i.e. LED1 or LED2 or...
    val String strType = triggeringItem.name.toString.split(strSuffixSeparator).get(1)      //Get Type (brightness oder temperature)

    if ((strType == strSuffixBrightness) && (triggeringItem.state as Number) == 0) {        //no math required. just switch off
        iNewCwState = 0
        iNewWwState = 0
    }
    else {
        var iBrightness = ScriptServiceUtil.getItemRegistry.getItem(strThing+strSuffixSeparator+strSuffixBrightness).state as Number
			if (iBrightness == NULL) {iBrightness = (ScriptServiceUtil.getItemRegistry.getItem(strThing+"_cw").state as Number) + (ScriptServiceUtil.getItemRegistry.getItem(strThing+"_ww").state as Number)}
		var iColor = ScriptServiceUtil.getItemRegistry.getItem(strThing+strSuffixSeparator+strSuffixTemperature).state as Number
        logInfo(filename, "Setting 'Brightness' to {} and 'White Color' to {}",iBrightness,iColor)
        iNewWwState = Math::round ((iColor / 100 * iBrightness).intValue)
        iNewCwState = iBrightness - iNewWwState
    }
    logInfo(filename, "Changing channel 'Cold White' to {} and 'Warm White' to {}",iNewCwState,iNewWwState)
    ScriptServiceUtil.getItemRegistry.getItem(strThing+"_cw").sendCommand(iNewCwState)
	ScriptServiceUtil.getItemRegistry.getItem(strThing+"_ww").sendCommand(iNewWwState)
end

Unfortunately this rule isn’t going to work as a rule template. A rule template isn’t just a regular old rule that a person can copy and edit. A rule template is actually something you can instantiate rules from, setting some parameters to customize it.

In order to support that the rules must:

  1. be based on UI rules
  2. posted in the marketplace in YAML (based on what you see in the code tab in MainUI) or JSON (bases on what you see in the JSONDB or the REST API) format

A stand alone rule like one would write in a .rules file cannot be installed through the Marketplace and only things that can be installed through MainUI belong in the Marketplace.

So either I can help you convert this to a true rule template or I can move this post to the Tutorials and Solutions section of the forum. Unfortunately, there are not a lot of tools build yet to make development of rule templates easy but I’m certain some will be built before too long. But for now it means hand writing some YAML or JSON to define the parameters.

For now I’ll remove the “published” tag so users don’t try to install it and find it not to work.