[SOLVED] Error in VSC when creating Timer with variables

System is OH2.4 release.
I want to create a function where the lights are indicating air quality by switching color from original color (e.g. white) to a color from green to red (depending on air quality), showing this for 5 seconds and then coming back to original colors (e.g. white).

Approach is as following:

// store original vaule
var String Wo_Lowboard_co_hsb = Wo_Lowboard_co.state.toString

// calculate and set new value
var String HSBString2 =  [DO SOME CALCULATION]
sendCommand(Wo_Lowboard_co, HSBString2)

// wait 5 seconds and set back to original value
createTimer(now.plusSeconds(5)) [|
  sendCommand(Wo_Lowboard_co, Wo_Lowboard_co_hsb)
]

Problem is, that I get the following error in Visual Studio Code (VSC):

Cannot refer to the non-final variable Wo_Lowboard_co_hsb inside a lambda expression

Function itself works. Original color is been set after 5 seconds.
Any ideas of how to get rid of this error message in VSC?

1 Like

Just found the soluition myself:
Declaring variables “global” and not inside the rule solves the problem.

You could have also changes the var to val inside your Rule and it would have fixed it as well.

@rlkoshak, thanks. That is even better …