Today my question is: How to validate an item in a rule and if the item or its state is not valid set it to a default value? I searched the forum, but did not find a solution. As far as i understand the lifcycle of an item is as follows:
1.) The item is not existing, so i cloud check if it ist null
2.) If the item in not equal null it is existant and the state is NULL
3.) If there is something wrong the bindig cloud set the state of the item to UNDEF
4.) In any other case the state of an item reflects the state like ON or a number or something else
Based on this a general rule to validate could be like this:
val myItemDefaultValue = 12
var Number myItemValue
if (myItem === null) {
Thread::sleep(200) // Wait 200ms for the system
}
if ((myItem.state == NULL) || (myItem.state == UNDEF)) {
myItem.sendCommand(myItemDefaultValue)
} else {
myItemValue = myItem.state as DecimalType
}
At the end we have a valid value to work within a rule. Maybe there is another way to do this. Maybe a general lamdba.
any suggestions?