Skip to content

Commit

Permalink
[items] ItemHistory: Adjust to breaking changes & Add new methods for…
Browse files Browse the repository at this point in the history
… future states (#331)

Refs openhab/openhab-core#3736.

- Rename `Item::history` to `Item::persistence`.
- Rename `HistoricItem` to `PersistedItem` and introduce `PersistedState` for state as string, Quantity & number.
- Make variance, deviation, average, sum & delta return the state as `PersistedState` instead of number.
- Add `**Until` methods for all existing queries.
- Add `nextState` and `nextUpdate` methods.
- Rename `historicState` method to `persistedState`.
- Remove `latestState` method.
- Remove deprecated `evolutionRate` method.
- Add `removeAllStatesSince`, `removeAllStatesUntil` & `removeAllStatesBetween` methods.

---------

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
  • Loading branch information
florian-h05 committed May 5, 2024
1 parent 9b53ed4 commit cd3fc4e
Show file tree
Hide file tree
Showing 11 changed files with 1,302 additions and 876 deletions.
83 changes: 52 additions & 31 deletions README.md
Expand Up @@ -396,7 +396,7 @@ Calling `getItem(...)` or `...` returns an `Item` object with the following prop

- Item : `object`
- .rawItem ⇒ `HostItem`
- .history[`ItemHistory`](#itemhistory)
- .persistence[`ItemPersistence`](#itempersistence)
- .semantics ⇒ [`ItemSemantics`](https://openhab.github.io/openhab-js/items.ItemSemantics.html)
- .type ⇒ `string`
- .name ⇒ `string`
Expand Down Expand Up @@ -496,63 +496,84 @@ items.replaceItem({

See [openhab-js : ItemConfig](https://openhab.github.io/openhab-js/global.html#ItemConfig) for full API documentation.

#### `ItemHistory`
#### `ItemPersistence`

Calling `Item.history` returns an `ItemHistory` object with the following functions:
Calling `Item.history` returns an `ItemPersistence` object with the following functions:

- ItemHistory :`object`
- .averageBetween(begin, end, serviceId) ⇒ `number | null`
- .averageSince(timestamp, serviceId) ⇒ `number | null`
- .changedBetween(begin, end, serviceId) ⇒ `boolean`
- ItemPersistence :`object`
- .averageSince(timestamp, serviceId) ⇒ `PersistedState | null`
- .averageUntil(timestamp, serviceId) ⇒ `PersistedState | null`
- .averageBetween(begin, end, serviceId) ⇒ `PersistedState | null`
- .changedSince(timestamp, serviceId) ⇒ `boolean`
- .deltaBetween(begin, end, serviceId) ⇒ `number | null`
- .deltaSince(timestamp, serviceId) ⇒ `number | null`
- .deviationBetween(begin, end, serviceId) ⇒ `number | null`
- .deviationSince(timestamp, serviceId) ⇒ `number | null`
- .evolutionRateBetween(begin, end, serviceId) ⇒ `number | null`
- .changedUntil(timestamp, serviceId) ⇒ `boolean`
- .changedBetween(begin, end, serviceId) ⇒ `boolean`
- .countSince(timestamp, serviceId) ⇒ `number`
- .countUntil(timestamp, serviceId) ⇒ `number`
- .countBetween(begin, end, serviceId) ⇒ `number`
- .countStateChangesSince(timestamp, serviceId) ⇒ `number`
- .countStateChangesUntil(timestamp, serviceId) ⇒ `number`
- .countStateChangesBetween(begin, end, serviceId) ⇒ `number`
- .deltaSince(timestamp, serviceId) ⇒ `PersistedState | null`
- .deltaUntil(timestamp, serviceId) ⇒ `PersistedState | null`
- .deltaBetween(begin, end, serviceId) ⇒ `PersistedState | null`
- .deviationSince(timestamp, serviceId) ⇒ `PersistedState | null`
- .deviationUntil(timestamp, serviceId) ⇒ `PersistedState | null`
- .deviationBetween(begin, end, serviceId) ⇒ `PersistedState | null`
- .evolutionRateSince(timestamp, serviceId) ⇒ `number | null`
- .getAllStatesBetween(begin, end, serviceId) ⇒ `Array[HistoricItem]`
- .getAllStatesSince(timestamp, serviceId) ⇒ `Array[HistoricItem]`
- .historicState(timestamp, serviceId) ⇒ `HistoricItem | null`
- .evolutionRateUntil(timestamp, serviceId) ⇒ `number | null`
- .evolutionRateBetween(begin, end, serviceId) ⇒ `number | null`
- .getAllStatesSince(timestamp, serviceId) ⇒ `Array[PersistedItem]`
- .getAllStatesUntil(timestamp, serviceId) ⇒ `Array[PersistedItem]`
- .getAllStatesBetween(begin, end, serviceId) ⇒ `Array[PersistedItem]`
- .lastUpdate(serviceId) ⇒ `ZonedDateTime | null`
- .latestState(serviceId) ⇒ `string | null`
- .maximumBetween(begin, end, serviceId) ⇒ `HistoricItem | null`
- .maximumSince(timestamp,serviceId) ⇒ `HistoricItem | null`
- .minimumSince(begin, end, serviceId) ⇒ `HistoricItem | null`
- .minimumSince(timestamp, serviceId) ⇒ `HistoricItem | null`
- .nextUpdate(serviceId) ⇒ `ZonedDateTime | null`
- .maximumSince(timestamp,serviceId) ⇒ `PersistedItem | null`
- .maximumUntil(timestamp,serviceId) ⇒ `PersistedItem | null`
- .maximumBetween(begin, end, serviceId) ⇒ `PersistedItem | null`
- .minimumSince(timestamp, serviceId) ⇒ `PersistedItem | null`
- .minimumUntil(timestamp, serviceId) ⇒ `PersistedItem | null`
- .minimumBetween(begin, end, serviceId) ⇒ `PersistedItem | null`
- .persist(serviceId): Tells the persistence service to store the current Item state, which is then done asynchronously.
**Warning:** This has the side effect, that if the Item state changes shortly after `.persist` has been called, the new Item state will be persisted. See [JSDoc](https://openhab.github.io/openhab-js/items.ItemHistory.html#persist) for a possible work-around.
- .previousState(skipEqual, serviceId) ⇒ `HistoricItem | null`
- .sumBetween(begin, end, serviceId) ⇒ `number | null`
- .sumSince(timestamp, serviceId) ⇒ `number | null`
- .updatedBetween(begin, end, serviceId) ⇒ `boolean`
- .persistedState(timestamp, serviceId) ⇒ `PersistedItem | null`
- .previousState(skipEqual, serviceId) ⇒ `PersistedItem | null`
- .nextState(skipEqual, serviceId) ⇒ `PersistedItem | null`
- .sumSince(timestamp, serviceId) ⇒ `PersistedState | null`
- .sumUntil(timestamp, serviceId) ⇒ `PersistedState | null`
- .sumBetween(begin, end, serviceId) ⇒ `PersistedState | null`
- .updatedSince(timestamp, serviceId) ⇒ `boolean`
- .varianceBetween(begin, end, serviceId) ⇒ `number | null`
- .varianceSince(timestamp, serviceId) ⇒ `number | null`
- .updatedUntil(timestamp, serviceId) ⇒ `boolean`
- .updatedBetween(begin, end, serviceId) ⇒ `boolean`
- .varianceSince(timestamp, serviceId) ⇒ `PersistedState | null`
- .varianceUntil(timestamp, serviceId) ⇒ `PersistedState | null`
- .varianceBetween(begin, end, serviceId) ⇒ `PersistedState | null`

Note: `serviceId` is optional, if omitted, the default persistence service will be used.

```javascript
var yesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000));
var item = items.KitchenDimmer;
console.log('KitchenDimmer averageSince', item.history.averageSince(yesterday));
console.log('KitchenDimmer averageSince', item.persistence.averageSince(yesterday));
```

The `HistoricItem` object contains the following properties, representing Item state and the respective timestamp:
The `PersistedState` object contains the following properties, representing Item state:

- `state`: State as string
- `numericState`: State as number, if state can be represented as number, or `null` if that's not the case
- `quantityState`: Item state as [`Quantity`](#quantity) or `null` if state is not Quantity-compatible
- `rawState`: State as Java `State` object

The `PersistedItem` object extends `PersistedState` with the following properties, representing Item state and the respective timestamp:

- `timestamp`: Timestamp as [`time.ZonedDateTime`](#time)

```javascript
var midnight = time.toZDT('00:00');
var historic = items.KitchenDimmer.history.maximumSince(midnight);
var historic = items.KitchenDimmer.persistence.maximumSince(midnight);
console.log('KitchenDimmer maximum was ', historic.state, ' at ', historic.timestamp);
```

See [openhab-js : ItemHistory](https://openhab.github.io/openhab-js/items.ItemHistory.html) for full API documentation.
See [openhab-js : ItemPersistence](https://openhab.github.io/openhab-js/items.ItemHistory.html) for full API documentation.

### Things

Expand Down Expand Up @@ -822,7 +843,7 @@ Examples:
var now = time.ZonedDateTime.now();
var yesterday = time.ZonedDateTime.now().minusHours(24);
var item = items.Kitchen;
console.log("averageSince", item.history.averageSince(yesterday));
console.log("averageSince", item.persistence.averageSince(yesterday));
```
```javascript
Expand Down

0 comments on commit cd3fc4e

Please sign in to comment.