[SOLVED] Compare Dimmer Value within an 'if' statement

Hello, I am new to OpenHab so be kind!

I am playing with some Lifx bulbs, creating some lighting scenes using a number item, with a multi state switch. I have it working as expected, but I want to change the state of the multi state switch if the value of the dimmer item does not equal a particular value.

For example, I have 4 lighting scenes: OFF, LOW, HIGH, MANUAL. I want the switch to go to MANUAL when the value of a dimmer item does not equal the value of 0 (OFF), 60 (LOW), or 100 (HIGH). So if I change the dimmer manually to 80%, the multi state switch goes to MANUAL.

See my items, sitemap, and rules below. The problem seems to be my IF statement in the second rule. It doesn’t seem to be comparing the dimmer value correctly and every time the dimmer value is changed, it always executes the switch in the rule.

default.items

//Groups

// Kitchen Switch Groups
Group:Switch:OR(ON, OFF)    gLightsKitchenCeiling
Group:Switch:OR(ON, OFF)    gLightsKitchenDown

// Kitchen Dimmer Groups
Group:Dimmer:MAX            gLightsKitchenDim
Group:Dimmer:MAX            gLightsKitchenCeilingDim    (gLightsKitchenDim)
Group:Dimmer:MAX            gLightsKitchenDownDim      (gLightsKitchenDim)


// Lighting Scenes

Number Lighting_Scenes

// Kitchen Lights

Switch  Kitchen_Light_Ceiling       "Kitchen Ceiling Light"     <light> (gLightsKitchenCeiling)  { channel="lifx:colorlight:D073D5227BD8:color" }
Switch  Kitchen_Light_Down1         "Kitchen Ceiling Light"     <light> (gLightsKitchenDown)     { channel="lifx:colorlight:D073D5265800:color" }
Switch  Kitchen_Light_Down2         "Kitchen Ceiling Light"     <light> (gLightsKitchenDown)     { channel="lifx:colorlight:D073D5262BB3:color" }
Switch  Kitchen_Light_Down3         "Kitchen Ceiling Light"     <light> (gLightsKitchenDown)     { channel="lifx:colorlight:D073D5262E07:color" }

Dimmer  Kitchen_Light_Ceiling_Dim   "Kitchen Ceiling Light Dim" <light> (gLightsKitchenCeilingDim)   { channel="lifx:colorlight:D073D5227BD8:color" }
Dimmer  Kitchen_Light_Down1Dim      "Kitchen Ceiling Light Dim" <light> (gLightsKitchenDownDim)      { channel="lifx:colorlight:D073D5265800:color" }
Dimmer  Kitchen_Light_Down2Dim      "Kitchen Ceiling Light Dim" <light> (gLightsKitchenDownDim)      { channel="lifx:colorlight:D073D5262BB3:color" }
Dimmer  Kitchen_Light_Down3Dim      "Kitchen Ceiling Light Dim" <light> (gLightsKitchenDownDim)      { channel="lifx:colorlight:D073D5262E07:color" }

default.sitemap

sitemap default label="My first sitemap"
{
    Frame label="Lighting Scenes" {
        Switch  item=Lighting_Scenes            label="Scenes"              icon="light"    mappings=[0="off", 1="Low", 2="High", 3="Manual"]
    }

    Frame label="Lighting Control" {
        Slider  item=gLightsKitchenDim           label="Kitchen Lights"      icon="light"
        Switch  item=gLightsKitchenCeiling       label="Ceiling"             icon="light"
        Text    item=gLightsKitchenCeilingDim    label="Ceiling [%d %%]"     icon="light"
        Switch  item=gLightsKitchenDown          label="Downlights"          icon="light"
        Text    item=gLightsKitchenDownDim       label="Downlights [%d %%]"  icon="light"
    }
}

default.rules

rule "Kitchen Scenes"
when
    Item Lighting_Scenes received command
then
    switch receivedCommand{
        case 0:{
            gLightsKitchenDownDim.sendCommand(0)
            gLightsKitchenCeilingDim.sendCommand(0)
        }
        case 1:{
            gLightsKitchenDownDim.sendCommand(60)
            gLightsKitchenCeilingDim.sendCommand(0)
        }
        case 2:{
            gLightsKitchenDownDim.sendCommand(100)
            gLightsKitchenCeilingDim.sendCommand(100)
        }
        case 3:{

        }
    }
end

rule "Kitchen Lights Manual"
when
    Item gLightsKitchenDim changed
then
        if (gLightsKitchenDim !=0 || gLightsKitchenDim !=60 || gLightsKitchenDim != 100) {
            switch Lighting_Scenes.postUpdate(3){
                case 3:{
                }
            }
    }
end
        if (gLightsKitchenDim !=0 && gLightsKitchenDim !=60 && gLightsKitchenDim != 100) {
            Lighting_Scenes.postUpdate(3)}
rule "Kitchen Lights Manual"
when
    Item gLightsKitchenDim changed
then
    if (gLightsKitchenDim == NULL) return;
    val dimmerValue = gLightsKitchenDim.state as Number
    if (dimmerValue != 0 || dimmerValue !=60 || dimmerValue != 100) { 
        switch Lighting_Scenes.state {
            case 3: {
            }
        }
    }
end

Sorry, but

... dimmerValue != 0 || dimmerValue !=60 ...

does’t make sense.

This appears to always post the update. if I select OFF, LOW or HIGH, it always switches back over to MANUAL

rule "Kitchen Lights Manual"
when
    Item gLightsKitchenDim changed
then
    if (gLightsKitchenDim !=0 && gLightsKitchenDim !=60 && gLightsKitchenDim != 100) {
            Lighting_Scenes.postUpdate(3)}
end
1 Like

You have to use the state, not the Item itself for comparison. Rule should be:

rule "Kitchen Lights Manual"
when
    Item gLightsKitchenDim changed
then
    if (!gLightsKitchenDim.state instanceof Number)                // state is not of Type Number
        return;                                                    // stop rule
    val dimmerValue = gLightsKitchenDim.state as Number            // state is of Type Number, so store it as a value
    if (dimmerValue !=0 && dimmerValue !=60 && dimmerValue != 100) // check if not equal to any auto value
        Lighting_Scenes.postUpdate(3)                              // switch to state "Manual"
end

Thanks for that!

It appears to be working correctly now, however I needed to comment out the first if statement.

rule "Kitchen Lights Manual"
when
    Item gLightsKitchenDim changed
then
    //if (gLightsKitchenDim.state instanceof Number) return;
    val dimmerValue = gLightsKitchenDim.state as Number
    if (dimmerValue !=0 && dimmerValue !=60 && dimmerValue != 100)
        Lighting_Scenes.postUpdate(3)
end

If I change it to the below as previously suggested, it works.

if (gLightsKitchenDim.state == NULL) return;

Please consider the ! within the term, this is the boolean NOT.

if (!gLightsKitchenDim.state instanceof Number)

vs.

if (gLightsKitchenDim.state instanceof Number)

I did try that, but it gives me an error.

Rule ‘Kitchen Lights Manual’: Unknown variable or command ‘!’; line 37, column 9, length 24

In visual code is also says “! cannot be resolved.”

The ! Needs to operate on the result of the instanceof operator. Therefore you need to put that operation in parens.

if (!(gLightsKitchenDim.state instanceof Number))

Without the parents it is trying to perform a ! On gLightsKitchenDim.state which doesn’t work because that is an Object, not a boolean.

1 Like

Thanks mate!

I just checked on my local file and it appears to work :slight_smile:

When I get home I will test it out.

Thanks again!