Slider not working with 1.8.3

Hi, I was trying to create a single Slider to control multiple Hue bulbs, but it was not working properly. I built a minimal example to try to isolate the problem, but it also persists, so I wonder whether this is a bug or some mistake I’m doing:

// demo.sitemap
sitemap demo label="Demo"
{
    Frame {
        Slider item=vdimmer
        Switch item=vswitch
    }
}

// demo.items
Dimmer vdimmer "Virtual Dimmer"
Switch vswitch "Virtual Switch"

// demo.rules
import org.openhab.core.library.types.*

rule "V Dim"
when
    Item vdimmer command received or
    Item vdimmer changed
then
    logInfo("virtual", "dimmer")
end

rule "V Switch"
when
    Item vswitch command received or
    Item vswitch changed
then
    logInfo("virtual", "switch")
end

When I tail the log I can see the output from the “V Switch” rule, but when I click the Increase/Decrease buttons on the Slider nothing happens. I am using the Classic UI on openHAB 1.8.3, the only add-on installed is org.openhab.binding.hue-1.9.0-SNAPSHOT (as the hue binding that comes with 1.8.3 does not work properly).

Can someone help me understand whether I’m doing something wrong here? Thanks

The trigger for received commands is

Item vdimmer received command

and because you don’t have real hardware yet, there is nothing to change the value of vdimmer, so this rule is never triggered.
For changing the value of vdimmer, you will have to add a rule like that:

rule "V Dim"
when
    Item vdimmer received command 
then
    if(!(vdimmer instanceOf decimalType))  //to initialize vdimmer if not initialized yet
        vdimmer.postUpdate(0)    //this will prevent an error
    if (receivedCommand==INCREASE)
        vdimmer.postUpdate((vdimmer.state as DecimalType + 5)
    else if (receivedCommand==DECREASE)
        vdimmer.postUpdate((vdimmer.state as DecimalType - 5)
    logInfo("V Dim", "vdimmer state = {}",vdimmer.state.toString)
end

Thanks for pointing out the issue with the trigger for received commands, that was a mistake when I created that minimal example.

I tried tests with real hardware at home and I still couldn’t see it working; eventually I found what my problem was: at least with the Classic UI on Firefox, if you simply click the Decrease/Increase buttons (as unfortunately it’s not displayed as a real slider), no commands are triggered. You need to keep the button pressed, after 200ms it starts sending commands which you can intercept with a rule.

My biggest surprise, however, was to see that both switches and dimmers work fine to control the lights, out-of-the-box, without any rule:

Group:Switch:OR(ON, OFF) slQC "On / Off Control" <hue> (gQC)
Group:Dimmer:AVG dlQC "Brightness Control [%s%%]" <hue> (gQC)

Dimmer lQC1 "Bulb 1" <hue> (slQC, dlQC) {hue="1"}
Dimmer lQC2 "Bulb 2" <hue> (slQC, dlQC) {hue="2"}

What I am still struggling with is to make the percentage in the brightness control to appear as 39% instead of 0.39000000%. Hopefully I’ll get this working with a rule similar to the one you suggested above.

No, the rules are only necessary if you don’t have hardware which supports INCREASE/DECREASE commands.

To display percentage Values, please use

Dimmer lQC1 "Bulb 1 [%.0f%%]" <hue> (slQC, dlQC) {hue="1"}

as the value is a number, not a string. (Same for Group Item)

In fact, short press to the up/down buttons in Classic UI should send a “100” respectively “0” to set the item to min/max brightness. To support switching, you could set the Item in the sitemap as follows:

Slider item= lQC1 switchSupport sendFrequency=400 

This will result in a widget which will send commands ON/OFF when pressed short, and (every 400msec) repeated INCREASE/DECREASE commands when pressed long.

Thank you for the tips regarding switchSupport and the right format for percentage in labels!

I now get the idea of the rule to replace the behavior one would get with real hardware (that’s interesting for testing purposes), but I cannot make that guard condition at the beginning to work that way:

if(!(dlQC.state instanceOf DecimalType))
// Results in: The name '! <XMemberFeatureCallImplCustom>' cannot be resolved to an item or type.

if(dlQC.state instanceOf DecimalType)
// Results in: The name 'DecimalType' cannot be resolved to an item or type.

This dynamic type system of Xtend is too confusing for me. I managed to implement something that works properly and also behaves OK with the switch with the following rule instead:

rule "Fake Dim dlQC"
when
    Item dlQC received command 
then
    var Number cs = if (dlQC.state instanceof DecimalType) dlQC.state as DecimalType else 0

    switch receivedCommand {
        case INCREASE :
            dlQC.postUpdate(if (cs > 95) 100 else cs + 5)
        case DECREASE :
            dlQC.postUpdate(if (cs < 5) 0 else cs - 5)
        case OFF :
            dlQC.postUpdate(0)
        case ON :
            dlQC.postUpdate(100)
    }
end

The only inconvenient is that, when I switch OFF and ON, I think it gets the value 1 (which should mean 100%) but it actually treats it as 1%, so INCREASE sets the dimmer to 6%, then 11% and so on.

My first thoughts were missing imports:

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import org.openhab.core.types.CommandcamelCase

though I don’t know, if they are really necessary for this rule. :wink:

Second thought… I hate camelCase… :wink:

if(!(dlQC.state instanceof DecimalType))

should be correct, ! is the boolean NOT, the expression in parentheses is negated, so “if not instance of decimal type”, but of course you got the point, as your rule proofs. :slight_smile: