It’s not reliable. You need to use the Item name. The problem is the Item Object changes from one run to the next causing targetItem to become stale and stop working.
You would use sendCommand(targetItem, cmd)
.
HSBType is not supported syntax in Rules DSL. I’m surprised it didn’t throw an error on that.
You’ve not specified the types of the stuff you’ve put into the ArrayList so when you call get
you end up with an Object and Object is not supported by sendCommand. You either need to tell it the type:
var List<HSBType> colors = newArrayList(uvIndex10Color, uvIndex6Color)
or cast the Object returned by get.
colors.get(1) as HSBType
Design Pattern: How to Structure a Rule. You would still use the switch but move the actual call to the Item to the end of the Rule so there is only one line that actually does stuff. Once you do that, the need for your targetLight variable from 1 probably goes away too since the light Item itself will only be referenced in the one place.
Personally, I prefer to use a Map for something like this. Key the map on the index value and then you don’t even need the switch statement at all.
import java.util.Map
val Map<Number, HSBType> colors = newHasMap( "1" -> HSBType::fromRGB(0, 128, 0),
"2" -> HSBType::fromRGB(0, 255, 0),
...
rule "uvIndex"
when
Item switchNanoWeather changed to ON
then
var currentUvIndex = (UVIndex_Current_UVIndex.state as Number + 0.5) as Number
LightPanel0901_Color.sendCommand(colors.get(currentUvIndex)
end