Skip to main content Link Search Menu Expand Document (external link)

Persistence

Persistence functions can be accessed through the item object. The following methods related to persistence are available:

MethodParametersReturn ValueExample
persistserviceNilItem1.persist
last_updateserviceZonedDateTimeItem1.last_update
previous_stateskip_equal: (default: false), serviceitem stateItem1.previous_state Item1.previous_state(skip_equal: true)
average_betweenbegin, end, serviceDecimalType or QuantityTypeItem1.average_between(2.hours, 1.hours, :influxdb)
average_sincetimestamp, serviceDecimalType or QuantityTypeItem1.average_since(1.hours, :influxdb)
changed_between?begin, end, serviceboolean 
changed_since?timestamp, serviceboolean 
delta_betweenbegin, end, serviceDecimalType or QuantityType 
delta_sincetimestamp, serviceDecimalType or QuantityType 
deviation_betweenbegin, end, serviceDecimalType or QuantityType 
deviation_sincetimestamp, serviceDecimalType or QuantityType 
evolution_ratetimestamp, serviceDecimalType 
historic_statetimestamp, serviceHistoricState 
maximum_betweenbegin, end, serviceHistoricState 
maximum_sincetimestamp, serviceHistoricState 
minimum_betweenbegin, end, serviceHistoricState 
minimum_sincetimestamp, serviceHistoricState 
sum_betweenbegin, end, serviceDecimalType or QuantityType 
sum_sincetimestamp, serviceDecimalType or QuantityType 
updated_between?begin, end, serviceboolean 
updated_since?timestamp, serviceboolean 
variance_betweenbegin, end, serviceDecimalType or QuantityType 
variance_sincetimestamp, serviceDecimalType or QuantityType 
  • The timestamp, begin and end parameters accept a java ZonedDateTime, a Ruby Time, or a Duration object that specifies how far back in time.
  • The service optional parameter accepts the name of the persistence service to use, as a String or Symbol. When not specified, the system’s default persistence service will be used.
  • Dimensioned NumberItems will return a QuantityType object
  • HistoricState the item state with timestamp attribute from OpenHAB’s HistoricItem. It contains the following properties:
    • timestamp - a ZonedDateTime object indicating the timestamp of the persisted data
    • state - the state of the item persisted at the timestamp above. This will be a QuantityType for dimensioned NumberItem. It is not necessary to access the state property, as the HistoricState itself will return the state. See the example below.
  • The *_between persistence methods are available since OpenHAB 3.3M7

Examples:

Given the following items are configured to persist on every change:

Number        UV_Index
Number:Power  Power_Usage "Power Usage [%.2f W]"
# UV_Index average will return a DecimalType
logger.info("UV_Index Average: #{UV_Index.average_since(12.hours)}") 
# Power_Usage has a Unit of Measurement, so 
# Power_Usage.average_since will return a QuantityType with the same unit
logger.info("Power_Usage Average: #{Power_Usage.average_since(12.hours)}") 

Comparison using Quantity

# Because Power_Usage has a unit, the return value 
# from average_since is a Quantity which can be
# compared against a string with quantity
if Power_Usage.average_since(15.minutes) > '5 kW'
  logger.info("The power usage exceeded its 15 min average)
end

HistoricState

max = Power_Usage.maximum_since(24.hours)
logger.info("Max power usage: #{max}, at: #{max.timestamp})

Persistence Block

A persistence block can group multiple persistence operations together under a single service. For example, instead of:

Item1.persist(:influxdb)
Item1.changed_since(1.hour, :influxdb)
Item1.average_since(12.hours, :influxdb)

Using a persistence block:

persistence(:influxdb) do
  Item1.persist
  Item1.changed_since(1.hour)
  Item1.average_since(12.hours)
end