I’m virtualizing my lights, as has been done before (Make any light controllable by HSBType to simplify automation), but I’ve encountered strange behavior in the processing of rules that I’d like help explaining!
My end goal is to modify incoming light command messages; I am attempting to write my code in a generic manner so that it can identify the type of light as well as the type of command, and then perform math/mapping to a new command (such that all lights could be treated as color, dimmer, or switch, and have reasonable behavior on all hardware endpoints even if they do not support that type of command - eventually extending this beyond lights). An intermediate goal is to learn how to write rules (I am inexperienced with OH2 & Java, but I’m not new to programming).
My immediate goal, however, is to understand why a code fragment (two failing versions are included below) would be failing; there is a way for me to write it (shown below) that functions and meets my requirements, but of course I am immediately suspicious of this behavior / my code.
For the same set of inputs (constant realItem, constant receivedCommand), this will work (“nested IF case”), correctly extracting the Brightness value from HSB:
if ( realItem.getType() == "Dimmer" ) {
if ( receivedCommand instanceof HSBType ) {
realCommand = receivedCommand.getBrightness().toString()
}
}
This will not work (“boolean logic case”):
if ( ( realItem.getType() == "Dimmer" ) && ( receivedCommand instanceof HSBType ) ) {
realCommand = receivedCommand.getBrightness().toString()
}
12:07:19.668 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘virtual light’: ‘getBrightness’ is not a member of ‘org.eclipse.smarthome.core.types.Command’; line 42, column 31, length 31
Now it starts to feel really weird (“getType only case”) (remember, the receivedCommand is still HSBType; this fragment is just for testing):
if ( realItem.getType() == "Dimmer" ) {
realCommand = receivedCommand.getBrightness().toString()
}
12:03:31.658 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘virtual light’: ‘getBrightness’ is not a member of ‘org.eclipse.smarthome.core.types.Command’; line 42, column 31, length 31
I’ve noted that if I explicitly cast the command to HSBType, it seems to work in all cases. Should this be necessary? Is this “more correct”? (It’s just super weird because the {} should not execute unless the command is ALREADY HSBType.)
realCommand = (receivedCommand as HSBType).brightness.toString()
I’m sure I’m probably doing something horrible, something wrong, and probably also something horribly wrong. What is it? (;