Skip to content

Commit

Permalink
Add document about getting thing status in scripts and rule.
Browse files Browse the repository at this point in the history
The change of the code is at eclipse-archived/smarthome#3001
After this commit, the end user can find the documentation about how to react
to thing status change in the rule.

Bug: openhab#517
Signed-off-by: Maoliang Huang <hellomao@outlook.com>

Incorporate more pull request feedback.
  • Loading branch information
kceiw committed Sep 26, 2017
1 parent 829c238 commit 1ef45d5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
23 changes: 23 additions & 0 deletions addons/actions.md
Expand Up @@ -95,6 +95,29 @@ The Timer object supports the following methods:
* `hasTerminated`: returns true if the code has run and completed
* `reschedule(AbstractInstant instant)`: reschedules the timer to execute at the new time. If the Timer has terminated this method does nothing.

### Thing Status Action

`getThingStatusInfo(String thingUID)`: Gets status information of the given thing identified by `thingUID`.

The result is of type `ThingStatusInfo`.
It contains [Thing Status]({{base}}/concepts/things.html), [Status Details]({{base}}/concepts/things.html) and [Status Description]({{base}}/concepts/things.html).
Refer to [Thing Status API]({{base}}/concepts/things.html) for how to get those information.
If you just want to know the status, you can use `thingStatusInfo.getStatus().toString()` and the result will be one of the values in [Thing Status]({{base}}/concepts/things.html).

> If the thing is removed or it's not added yet, it'll return null.
For example:

```java
var thingStatusInfo = getThingStatusInfo("zwave:device:c5155aa4:node2")

if ((thingStatusInfo != null) && (thingStatusInfo.getStatus().toString() == "ONLINE")) {
logInfo("ThingStatus", "The thing is online.")
} else {
logError("ThingStatus", "The thing is offline or doesn't exist.")
}
```

## Installable Actions

<table id="actions-select" class="striped">
Expand Down
23 changes: 23 additions & 0 deletions configuration/rules-dsl.md
Expand Up @@ -113,6 +113,7 @@ There are different categories of rule triggers:
- **Item**(-Event)-based triggers: They react on events on the openHAB event bus, i.e. commands and status updates for items
- **Time**-based triggers: They react at special times, e.g. at midnight, every hour, etc.
- **System**-based triggers: They react on certain system statuses.
- **Thing**-based triggers: They react on thing status, i.e. change from ONLINE to OFFLINE.

Here are the details for each category:

Expand Down Expand Up @@ -178,6 +179,28 @@ then
end
```

### Thing Triggers

You can listen for status updates or status changes (an update might leave the status unchanged).
You can decide whether you want to catch only a specific status or any.
Here is the syntax for all these cases (parts in square brackets are optional):

```java
Thing <thingUID> received update [<status>]
Thing <thingUID> changed [from <status>] [to <status>]
```

The status used in the trigger and the script is a string (no quotes).
You can find all the possible values for status from [Thing Status]({{base}}/concepts/things.html).
And refer to [Thing Status Action]({{base}}/addons/actions.html) to find how to get thing status in the script.

The `thingUID` is the identifier assigned to the Thing, manually in your configuration or automatically during auto discovery.
You can find it from PaperUI or from Karaf remote console.
For example, one z-wave device can be "zwave:device:c5155aa4:node14".

> Note: You need to use quotes around `thingUID` if it contains special characters such as ':'.
> The parser will not recognize the whole text correctly without quotes.
## Scripts

The expression language used within scripts is the same that is used in the Xtend language - see the [documentation of expressions](http://www.eclipse.org/xtend/documentation/203_xtend_expressions.html) on the Xtend homepage.
Expand Down

0 comments on commit 1ef45d5

Please sign in to comment.