Hello guys.
When is the command “instanceof” needed to be used?
If I wanna access an item status it should be item.state as Decimaltype?
The instanceof
operator will tell you if an object is of a certain type. There are many uses for it, but here’s a random example… you have a rule that has multiple triggers that are mixed motion sensors and contact sensors, and you’d like to know if the triggeringItem is a SwitchItem or ContactItem…
if (triggeringItem instanceof ContactItem) {
// do contact stuff
}
else if (triggeringItem instanceof SwitchItem) {
// do switch stuff
}
… similarly…
if (triggeringItem.state instanceof OpenCloseType) {
// do contact stuff
}
else if (triggeringItem.state instanceof OnOffType) {
// do switch stuff
}
That wouldn’t be sensible if was, say, a String or Switch Item type,
If ii’s a Number type Item, it’s usually simplest to use the Item.state as Number for further processing.