Hi,
I run OpenHAB 2.1, and I can now see the status of Things in rules (e.g. see http://docs.openhab.org/addons/actions.html#thing-status-action).
I am now trying to link Items to the status of the Thing they come from, so as I can tell in my rules if an Item is online or offline based on whether the underlying Thing is online or offline.
This is because in my rules I want to randomly pick one Item in a group, with the constraint that this Item (i.e. its underlying Thing) must be online.
I found a slightly convoluted way to achieve this, but I am hoping there is a neater way to do this.
Is there an API call to get the Thing an Item is linked to, and/or the list of Items linked to an Item?
Thanks,
Thib.
ps: my current way to do this (note I am by no mean a Xtend expert, so good-practices and improvement suggestions welcome!)
First I add a tag with the name of the thing the item is linked to:
Switch sLight "My light" <light> (gBackLights) ["thing=zwave:device:controller:node5"] {channel="zwave:device:controller:node5:switch_binary"}
This tag is not ideal as it is redundant information (we could tell the Thing from the channel), and a typo is all too easy (e.g one might type ānode6ā instead of ānode5ā and get unexpected results).
Then I created a function to check if the item is online:
// isOnline.apply(item): returns a boolean indicating if the item (and underlying thing) is online
// Note: this function assumes that a tag "thing=your:thing:string:name" is set on the item, e.g.:
// Switch sLight "my light" <light> (gLights) ["thing=zwave:device:controller:node5"] {channel="zwave:device:controller:node5:switch_binary"}
// Note that we return true if we can't find that tag.
val Functions.Function1<GenericItem,Boolean> isOnline = [
GenericItem item |
logDebug("isOnline", "Starting on item=" + item.name)
for (String tag: item.tags) {
if (tag.startsWith("thing=")) {
var thingName = tag.split("=").get(1)
// Now check the status of that Thing
var thingStatusInfo = getThingStatusInfo(thingName)
if ((thingStatusInfo != null) && (thingStatusInfo.getStatus().toString() != "ONLINE")) {
logDebug("isOnline", "item '{}' is *NOT* online.", item.name)
return false
}
}
}
// Return true if we didn't find that channel
return true
]
and finally an example rule using this function:
rule "test item and thing status"
when
Item sLight changed
then
gBackLights.members.forEach[item|
if (isOnline.apply(item as GenericItem)) {
logInfo("test item status", "item {} is online", item.name)
}
else {
logInfo("test item status", "item {} is offline", item.name)
}
]
end