Hi everyone,
I’m writing a script to DIM my Ikea lights on a long press (Velbus) input - but while it looks to be working something strange is happening…
What I wanted is - if the brightness is below 30%, the long press must increase brightness to max 100. If the brightness is above 30% the function must decrease brightness.
What happens now with the below code is that indeed the light goes from 100 to 30% brightness, but then increases again to 40, then back to 30 back to 40 etc… and I dont understand why it would not stay within it’s own loop / if statement…
ideas?
var ButtonLong=false
var ButtonDown=false
rule "Test short press"
when
Channel 'velbus:vmbgpod-2:7f964e441a:02:input#CH1' triggered PRESSED
then
ButtonLong=false
var IKEA_state = IKEA_Color.state()
logInfo("IKEA","IKEA detected on with luminosity " + IKEA_state.toString)
end
rule "Test long press"
when
Channel 'velbus:vmbgpod-2:7f964e441a:02:input#CH1' triggered LONG_PRESSED
then
logInfo("TEST", "Test fired! LONG_PRESSED")
ButtonLong=true
ButtonDown=true
if (IKEA_Color.state.toString != "0,0,0") { //validating that the light is actually on
var HSBType hsbValue = IKEA_Color.state as HSBType
logInfo("IKEA", "IKEA_Color=" + hsbValue)
var DecimalType hue = new DecimalType(hsbValue.hue.intValue % 360 + 10) // 0-360; 0=red, 120=green, 240=blue, 360=red(again)
var PercentType sat = new PercentType(hsbValue.saturation.intValue) // 0-100
var PercentType bright = new PercentType(hsbValue.brightness.intValue) // 0-100
logInfo("IKEA", "IKEA_Color_HUE=" + hue)
logInfo("IKEA", "IKEA_Color_SATURATION=" + sat)
logInfo("IKEA", "IKEA_Color_BRIGHNTESS=" + bright)
if (bright <30) { //if the brightness is below 30 - we need to dim upwards
logInfo("IKEA", "IKEA_Color_BRIGHNTESS DIM UP")
while(bright < 100 && ButtonDown == true) {
bright = bright + 10 // doing 10% on 1000 sleep in a loop till 100% or button down is false - eg released)
logInfo("IKEA", "IKEA_Color_BRIGHNTESS=" + bright)
var newStatus = hue+","+sat+","+bright
IKEA_Color.sendCommand(newStatus)
Thread::sleep(1000)
}
}
if (bright >30 ) { //if brightness is above 30% we need to dim down by 10% steps
logInfo("IKEA", "IKEA_Color_BRIGHNTESS DIM DOWN")
while(bright < 100 && bright > 10 && ButtonDown == true) { //just keeping a minimum of 10% - while button is still pressed downward
bright = bright - 10
logInfo("IKEA", "IKEA_Color_BRIGHNTESS=" + bright)
var newStatus = hue+","+sat+","+bright
IKEA_Color.sendCommand(newStatus)
Thread::sleep(1000) // one minute
logInfo("IKEA", "IKEA_Color_BRIGHNTESS DIM DOWN NEXT")
}
}
}
end
rule "Test release"
when
Channel 'velbus:vmbgpod-2:7f964e441a:02:input#CH1' triggered RELEASED
then
ButtonDown = false
if (ButtonLong==false){
logInfo("IKEA", "Test fired! PRESSED")
if (IKEA_Color.state.toString == "0,0,0") {
IKEA_Color.sendCommand("ON")
}
else{
IKEA_Color.sendCommand("OFF")
}
}
ButtonLong=false
end
and the log:
21:45:23.906 [INFO ] [org.openhab.core.model.script.TEST ] - Test fired! LONG_PRESSED
21:45:23.917 [INFO ] [org.openhab.core.model.script.IKEA ] - IKEA_Color=124,69,59
21:45:23.928 [INFO ] [org.openhab.core.model.script.IKEA ] - IKEA_Color_HUE=134
21:45:23.935 [INFO ] [org.openhab.core.model.script.IKEA ] - IKEA_Color_SATURATION=69
21:45:23.942 [INFO ] [org.openhab.core.model.script.IKEA ] - IKEA_Color_BRIGHNTESS=59
21:45:23.951 [INFO ] [org.openhab.core.model.script.IKEA ] - IKEA_Color_BRIGHNTESS DIM DOWN
21:45:23.962 [INFO ] [org.openhab.core.model.script.IKEA ] - IKEA_Color_BRIGHNTESS=49
21:45:23.972 [INFO ] [openhab.event.ItemCommandEvent ] - Item 'IKEA_Color' received command 134,69,49
21:45:23.980 [INFO ] [openhab.event.ItemStatePredictedEvent] - Item 'IKEA_Color' predicted to become 134,69,49
21:45:23.989 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'IKEA_Color' changed from 124,69,59 to 134,69,49
21:45:24.027 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'IKEA_Color' changed from 134,69,49 to 133,69,59
21:45:24.630 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'IKEA_Color' changed from 133,69,59 to 133,69,49
21:45:24.973 [INFO ] [org.openhab.core.model.script.IKEA ] - IKEA_Color_BRIGHNTESS DIM DOWN NEXT
21:45:24.985 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID 'myrules-2' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.lib.NumberExtensions.operator_greaterThan(org.openhab.core.types.Type,java.lang.Number) on instance: null in myrules
21:45:27.838 [INFO ] [openhab.event.ChannelTriggeredEvent ] - velbus:vmbgpod-2:7f964e441a:02:input#CH1 triggered RELEASED