Rule to adjust TV backlight based on running application

Hi all,

Relatively new to OpenHab - I’ve got the basics working, but have now started exploring some more complex rules. I’m trying to have a TV backlight (via hue binding) adjust it’s color based on the application running on the TV (detected via the LG WebOS binding).

So far I have this as the basic code:

rule "test"
when
    Item Livingroom_TV_Power changed to ON or
    Item Livingroom_TV_Application received update
then
    var StringType application = new StringType(Livingroom_TV_Application.string)
    logInfo("test.rules","TV application identified as:" +application)
    var DecimalType hue = new DecimalType(0)
    var PercentType sat = new PercentType(0)
    var PercentType bright = new PercentType(0)
    var PercentType temp = new PercentType(0)
    switch (application) {
        case com.webos.app.livetv:  
            hue = DecimalType(0)
            sat = PercentType(0)
            bright = PercentType(75)
            temp = PercentType(100);
            break;
        case bbc:
            hue = DecimalType(300)
            sat = PercentType(100)
            bright = PercentType(75)
            temp = PercentType(0);
            break;
        case lovefilm:
            hue = DecimalType(240)
            sat = PercentType(100)
            bright = PercentType(75)
            temp = PercentType(0);
            break;
        case netflix:
            hue = DecimalType(0)
            sat = PercentType(100)
            bright = PercentType(75)
            temp = PercentType(0);
            break;
        case com.webos.app.hdmi2:   
            hue = DecimalType(120)
            sat = PercentType(100)
            bright = PercentType(75)
            temp = PercentType(0);
            break;
        default:
            hue = DecimalType(0)
            sat = PercentType(0)
            bright = PercentType(75)
            temp = PercentType(100)
            logWarn("test.rules","TV application not corrected, setting to default values")
            break;
    }
    var HSBType light = new HSBType(hue,sat,bright)
    sendCommand(Livingroom_TVBacklight_Temperature, temp)
    sendCommand(Livingroom_TVBacklight_Color, light)
end

But I’m getting multiple errors in my log file, and the rule simply isn’t even being loaded (which makes debugging it challenging)

[WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'test.rules' has errors, therefore ignoring it: [15,17]: no viable alternative at input '='
[15,19]: no viable alternative at input 'PercentType'
[15,30]: no viable alternative at input '('
[15,31]: extraneous input '0' expecting ')'
[16,20]: no viable alternative at input '='
[16,22]: no viable alternative at input 'PercentType'
[16,33]: no viable alternative at input '('
[16,34]: extraneous input '75' expecting ')'
[17,18]: no viable alternative at input '='
[17,20]: no viable alternative at input 'PercentType'
[17,31]: no viable alternative at input '('
[17,32]: extraneous input '100' expecting ')'
[17,36]: mismatched input ';' expecting '=>'
[18,18]: no viable alternative at input ';'
[19,9]: mismatched input 'case' expecting 'end'

It seems my syntax has gone badly wrong somehow, but I’m genuinely struggling to work out quite why it’s so unhappy?

Anyone with more java skills able to help point out my errors?

Thanks!

OK, few mistakes here:
Have a look at: http://www.eclipse.org/xtend/documentation/203_xtend_expressions.html#switch-expression for the use on the switch expression in XTend
You are making things too complicated with Types, use String and Number, that will do.
Your application variable is a String so your case needs to have ""

rule "test"
when
    Item Livingroom_TV_Power changed to ON or
    Item Livingroom_TV_Application received update
then
    var String application = new StringType(Livingroom_TV_Application.state.toString)
    logInfo("test.rules","TV application identified as:" +application)
    var Number hue = 0
    var Number sat = 0
    var Number bright = 0
    var Number temp = 0
    switch (application) {
        case "com.webos.app.livetv": {
            hue = 0
            sat = 0
            bright = 75
            temp = 100
        }
        case "bbc": {
            hue = 300
            sat = 100
            bright = 75
            temp = 0
        }
        case "lovefilm": {
            hue = 240
            sat = 100
            bright = 75
            temp = 0
        }
        case "netflix": {
            hue = 0
            sat = 100
            bright = 75
            temp = 0
        }
        case "com.webos.app.hdmi2": {
            hue = 120
            sat = 100
            bright = 75
            temp = 0
        }
        default: {
            hue = 0
            sat = 0
            bright = 75
            temp = 100
            logWarn("test.rules","TV application not corrected, setting to default values")
        }
    }
    var HSBType light = new HSBType(hue,sat,bright)
    sendCommand(Livingroom_TVBacklight_Temperature, temp)
    sendCommand(Livingroom_TVBacklight_Color, light)
end

I’d only add the nit pick that the Item method should be used.

    Livingroom_TVBacklight_Temperature.sendCommand(temp)
    Livingroom_TVBacklight_Color.sendCommand(light)

Yep, missed that… :wink: