Rule: Want to do sth when RGB light (Color) is turned off

Hi

I have that piece of code:

rule "LED-Strom automatisch schalten"
when
    Item rgb_wozi received update
then
    if (rgb_wozi.state != "0.0,0.0,0.0") {
        sendCommand(sw_ledcontroller, ON)
        logDebug("light", "WZ RGB-State: " + rgb_wozi.state)
    } else {
        sendCommand(sw_ledcontroller, OFF)
    }
end

but that does not work correctly. The If-statement always is returning as true…
I want that the power (sw_ledcotroller) is switched off when the off-button is used on the Colorpicker…

Items:
Color rgb_wozi “Licht Wohnzimmer” (g_rgb_main, g_rgb) {dmx=“CHANNEL[4/3:500]”}
Switch sw_ledcontroller “Stromzufuhr LED-Controller”

Log:
2015-11-19 12:10:33.540 [INFO ] [runtime.busevents ] - rgb_wozi received command OFF
2015-11-19 12:10:33.555 [INFO ] [runtime.busevents ] - sw_ledcontroller received command ON
2015-11-19 12:10:33.559 [INFO ] [runtime.busevents ] - rgb_wozi state updated to 0.0,0.0,0.0
2015-11-19 12:10:33.575 [DEBUG] [org.openhab.model.script.light] - WZ RGB-State: 0.0,0.0,0.0
2015-11-19 12:10:33.595 [INFO ] [runtime.busevents ] - sw_ledcontroller received command ON
2015-11-19 12:10:33.606 [DEBUG] [org.openhab.model.script.light] - WZ RGB-State: 0.0,0.0,0.0

Hi Alex

On first sight I would say that the rgb_wozi state is not of type String, but of RGBType(? or HSBType, if there is no RGBType class). At least I know that the ColorPicker item goes with HSBType…

So your if-statement can’t become true…

I’ve done something similar with Hue lights which have a HSBType state and go to the brightness (b) as a brightness of 0% means off (at least from my point of view).

Regards
Dieter

      rule "LED-Strom automatisch schalten"

when
Item rgb_ess1 received update
then
if (rgb_ess1.getStateAs(typeof(OnOffType)).equals(OFF)) {
sendCommand(sw_test, OFF)
logDebug(“light”, "WZ RGB-State: " + rgb_wozi.state)
} else {
sendCommand(sw_test, ON)
}
end
2015-11-24
08:07:19.783 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during
the execution of rule ‘LED-Strom automatisch schalten’: OnOffType

Also tried
if (rgb_ess1.getStateAs(typeof(OnOffType)) == OFF) {
if (rgb_ess1.state.getBrightness() == 0) {
if (rgb_ess1.state.b == 0) {
and every of those combinations with .equals(0)
None working…
2015-11-24
07:48:14.349 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during
the execution of rule ‘LED-Strom automatisch schalten’: Cannot cast
org.openhab.core.library.types.HSBType to void

Can you provide mme with an example how you solved it?

Hi Alex
You think much to complicate, you are not in Java …

Try instead if (rgb_ess1.state == OFF) {[quote=“darkalex, post:4, topic:4340”]
if (rgb_ess1.state.getBrightness() == 0) {
[/quote]
or if (rgb_ess1.state.getBrightness() == PercentType::ZERO) {

That should work.

Regards
Dieter

Hi Alex, me again

I was just only on my tablet which makes things mor complicated than I thought…

Here you find the part of my “Hue Scenes in OpenHab” rule:

    items.keySet.forEach [ key |
      var OnOffType sw
      var HSBType hsb

      var PercentType value = items.get(key)
      if (value instanceof HSBType) {
        logDebug("org.openhab.model.script.lights", "value for key " + key + " is of type HSBType")

        hsb = value as HSBType
        logDebug("org.openhab.model.script.lights", "hsb.getBrightness() for key " + key + ": " + hsb.getBrightness())

        if(hsb.getBrightness() == PercentType::ZERO) { sw = OFF } else { sw = ON }
      } else {
        logDebug("org.openhab.model.script.lights", "value for key " + key + " is NOT of type HSBType")
        logDebug("org.openhab.model.script.lights", "value for key " + key + ": " + value)

        if(value == PercentType::ZERO) { sw = OFF } else { sw = ON }
      }

      logDebug("org.openhab.model.script.lights", "switch for key " + key + " :" + sw)

      var String item
      var String state

      if (sw == OFF) {
        item = "SWT_" + key
        state = sw.toString
      } else if (value instanceof HSBType) {
        item = "CLR_" + key
        state = hsb.toString
      } else {
        item = "DIM_" + key + "_Dimm"
        state = value.toString
      }

      logInfo("org.openhab.model.script.lights", "execute: sendCommand(" + item + ", " + state + ")")
      sendCommand(item, state)

        try {
        Thread::sleep(250)
        } catch (InterruptedException e) {
          // Ignore this
      }
    ]

In “items” you have a Map with the name of the light as key and either the color or the brightness of the lamp depending whether it’s a RGB or white lamp. My items are named SWT_<lightname> for ON/OFF Switch, CLR_<lightname> for Colorpicker und DIM_<lightname>_DIMM for dimmer items.
It’s not exactly what you do (I’m still thinking about how to implement a similar (generic) rule to identify Hue scenes in OpenHab) but it’s the other way round so I hope this will give you an idea.

Regards
Dieter

Thanks, Dieter!
You lead me to the right solution. PercentType did not work for me eighter but that does:

rule "LED-Strom automatisch schalten"
when
    Item rgb_wozi received update or
    Item rgb_regal received update or 
    Item rgb_wzschrank received update or
    Item rgb_ess1 received update or
    Item rgb_ess2 received update
then
    if ((rgb_wozi.state as HSBType).getBrightness() == 0 && (rgb_regal.state as HSBType).getBrightness() == 0 && (rgb_wzschrank.state as HSBType).getBrightness() == 0 && (rgb_ess1.state as HSBType).getBrightness() == 0 && (rgb_ess2.state as HSBType).getBrightness() == 0) {
        logDebug("light", "LED-Hauptbeleuchtung wird ausgeschaltet")
        sendCommand(sw_ledcontroller, OFF)
    } else {
        logDebug("light", "LED-Hauptbeleuchtung wird eingeschaltet")
        sendCommand(sw_ledcontroller, ON)
    }
end