How to get ON or OFF state from a dimmer item


I am trying in a rule to test if a dimming light is ON or OFF. The item is of type Dimmer.

Here is the code:

    var OnOffType lampeState = LampeChambre.state as OnOffType
    if (lampeState == OFF) {

It does not work. Here is the error I get when the rule is run:

2016-02-18 19:46:01.565 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Detection mouvement chambre': Cannot cast org.openhab.core.library.types.PercentType to org.openhab.core.library.types.OnOffType

What’s the solution ?

try with integer. >0 = ON
or create a new switch item binded to your dimmer.

While you can send ON and OFF to a Dimmer, its state is actually a PercentType. So you have to cast it to a PercentType and use if(lampeState == 0) to test for OFF and if(lampeState !=0) to test for ON.

Ok thank you guys, this code is working:

if (LampeChambre.state == 0) {


if (LampeChambre.state > 0) {

Now for a hue light, Kai’s adviced me few weeks ago to only create a Color item and then I can use ON and OFF command. But is there a way in a rule, when having only a Color item, to know if the light is ON or OFF ? Or should I keep an additional Switch item in this case ?

I think (you will need to verify) that an HSB (the state type of Color item) of “0,0,0” indicates off.

I found the solution in another topic:

            var hsbValue = LampeConsoleColor.state as HSBType
            if (hsbValue.getBrightness() == 0) {

Although a very old thread, just for completeness:

It’s possible to get OnOffType from a Dimmer Item:


I tried to use this from a javascript rule, but it doesn’t work.
Rule log is in DEBUG level, no errors.

var tst = ir.getItem("Light_Dimmer").getStateAs(OnOffType);

You have to import OnOffType and use it as a class

1 Like

Thanks! It’s working for me even without:

var OnOffType = Java.type("org.openhab.core.library.types.OnOffType");

Maybe because of this: Default Preset ?

This is enough:

var tst = ir.getItem("Light_Dimmer").getStateAs(OnOffType.class);
1 Like

I needed this ON/OFF functionality and display in my floor plan page, where I couldn’t get the hint to display other than the percent value.
So I linked another item to my lights dimmer channel (z-wave binding), just with type: switch, and that is working fine, and I can use it both in (javascript) rules and on the floor plan also.