Class: OpenHAB::Core::Items::NumberItem

Inherits:
GenericItem show all
Includes:
NumericItem
Defined in:
lib/openhab/core/items/number_item.rb

Overview

A NumberItem has a decimal value and is usually used for all kinds of sensors, like temperature, brightness, wind, etc. It can also be used as a counter or as any other thing that can be expressed as a number.

Non-dimensioned numbers will have a state of DecimalType, while dimensioned numbers will have a state of QuantityType. Be sure to read the documentation for those two classes for how to work with the different states of a NumberItem.

Examples:

Number Items can be selected in an enumerable with grep.

# Get all NumberItems
items.grep(NumberItem)
     .each { |number| logger.info("#{item.name} is a Number Item") }

Constant Summary

Constants included from Semantics

Semantics::Equipment, Semantics::Location, Semantics::Point, Semantics::Property, Semantics::Tag

Constants included from Persistence

Persistence::HistoricState

Instance Attribute Summary collapse

Attributes inherited from GenericItem

#category, #formatted_state, #label, #name, #raw_state, #tags

Attributes included from Semantics

#equipment, #equipment_type, #location, #location_type, #point_type, #property_type, #semantic_type

Attributes included from Item

#accepted_command_types, #accepted_data_types, #all_groups, #channel, #channel_uid, #channel_uids, #channels, #groups, #links, #metadata, #name, #provider, #thing, #things

Instance Method Summary collapse

Methods inherited from GenericItem

#command, #modify, #null?, #refresh, #state?, #time_series=, #undef?, #update

Methods included from Semantics

add, #equipment?, #location?, lookup, #point?, #points, remove, #semantic?, tags

Methods included from Item

#call_item?, #color_item?, #contact_item?, #date_time_item?, #dimmer_item?, #group_item?, #image_item?, #inspect, #link, #location_item?, #member_of?, #number_item?, #player_item?, #rollershutter_item?, #string_item?, #switch_item?, #tagged?, #to_s, #unlink

Methods included from DSL::Items::TimedCommand

#command

Methods included from Persistence

#all_states_between, #all_states_since, #all_states_until, #average_between, #average_since, #average_until, #changed_between?, #changed_since?, #changed_until?, #count_between, #count_since, #count_state_changes_between, #count_state_changes_since, #count_state_changes_until, #count_until, #delta_between, #delta_since, #delta_until, #deviation_between, #deviation_since, #deviation_until, #evolution_rate, #evolution_rate_between, #evolution_rate_since, #evolution_rate_until, #historic_state, #last_change, #last_update, #maximum_between, #maximum_since, #maximum_until, #median_between, #median_since, #median_until, #minimum_between, #minimum_since, #minimum_until, #next_change, #next_state, #next_update, #persist, #persisted_state, #previous_state, #remove_all_states_between, #remove_all_states_since, #remove_all_states_until, #sum_between, #sum_since, #sum_until, #updated_between?, #updated_since?, #updated_until?, #variance_between, #variance_since, #variance_until

Methods included from DSL::Items::Ensure::Ensurable

#ensure

Instance Attribute Details

#dimensionClass? (readonly)

Returns The dimension of the number item.

Returns:

  • (Class, nil)

    The dimension of the number item.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/openhab/core/items/number_item.rb', line 34

class NumberItem < GenericItem
  include NumericItem

  # raw numbers translate directly to {DecimalType}, not a string
  # @!visibility private
  def format_type(command)
    if command.is_a?(Numeric)
      if unit && (target_unit = DSL.unit(unit.dimension) || unit)
        return Types::QuantityType.new(command, target_unit)
      end

      return Types::DecimalType.new(command)
    end

    super
  end

  # @!visibility private
  def config_eql?(other)
    super && dimension == other.dimension
  end

  # @!attribute [r] range
  # Returns the range of values allowed for this item, as defined by its
  # state description.
  #
  # If this item has a {#unit}, it will be applied to the result, returning
  # a range of {QuantityType} instead of BigDecimal.
  # @return [Range, nil]
  # @note State descriptions can be provided by bindings, defined in
  #   metadata, or theoretically come from other sources.
  def range
    return unless (sd = state_description)

    # check if we have a unit, even if the item's metadata doesn't declare
    # it properly
    unit = self.unit || ((s = state) && s.is_a?(QuantityType) && s.unit)
    min = sd.minimum&.to_d
    max = sd.maximum&.to_d
    return nil unless min || max

    min |= unit if min && unit
    max |= unit if max && unit
    min..max
  end

  protected

  # Adds the unit dimension
  def type_details
    ":#{dimension}" if dimension
  end
end

#rangeRange? (readonly)

Note:

State descriptions can be provided by bindings, defined in metadata, or theoretically come from other sources.

Returns the range of values allowed for this item, as defined by its state description.

If this item has a #unit, it will be applied to the result, returning a range of QuantityType instead of BigDecimal.

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/openhab/core/items/number_item.rb', line 65

def range
  return unless (sd = state_description)

  # check if we have a unit, even if the item's metadata doesn't declare
  # it properly
  unit = self.unit || ((s = state) && s.is_a?(QuantityType) && s.unit)
  min = sd.minimum&.to_d
  max = sd.maximum&.to_d
  return nil unless min || max

  min |= unit if min && unit
  max |= unit if max && unit
  min..max
end

#stateDecimalType, ... (readonly)

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/openhab/core/items/number_item.rb', line 34

class NumberItem < GenericItem
  include NumericItem

  # raw numbers translate directly to {DecimalType}, not a string
  # @!visibility private
  def format_type(command)
    if command.is_a?(Numeric)
      if unit && (target_unit = DSL.unit(unit.dimension) || unit)
        return Types::QuantityType.new(command, target_unit)
      end

      return Types::DecimalType.new(command)
    end

    super
  end

  # @!visibility private
  def config_eql?(other)
    super && dimension == other.dimension
  end

  # @!attribute [r] range
  # Returns the range of values allowed for this item, as defined by its
  # state description.
  #
  # If this item has a {#unit}, it will be applied to the result, returning
  # a range of {QuantityType} instead of BigDecimal.
  # @return [Range, nil]
  # @note State descriptions can be provided by bindings, defined in
  #   metadata, or theoretically come from other sources.
  def range
    return unless (sd = state_description)

    # check if we have a unit, even if the item's metadata doesn't declare
    # it properly
    unit = self.unit || ((s = state) && s.is_a?(QuantityType) && s.unit)
    min = sd.minimum&.to_d
    max = sd.maximum&.to_d
    return nil unless min || max

    min |= unit if min && unit
    max |= unit if max && unit
    min..max
  end

  protected

  # Adds the unit dimension
  def type_details
    ":#{dimension}" if dimension
  end
end

#unitjavax.measure.Unit? (readonly)

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/openhab/core/items/number_item.rb', line 34

class NumberItem < GenericItem
  include NumericItem

  # raw numbers translate directly to {DecimalType}, not a string
  # @!visibility private
  def format_type(command)
    if command.is_a?(Numeric)
      if unit && (target_unit = DSL.unit(unit.dimension) || unit)
        return Types::QuantityType.new(command, target_unit)
      end

      return Types::DecimalType.new(command)
    end

    super
  end

  # @!visibility private
  def config_eql?(other)
    super && dimension == other.dimension
  end

  # @!attribute [r] range
  # Returns the range of values allowed for this item, as defined by its
  # state description.
  #
  # If this item has a {#unit}, it will be applied to the result, returning
  # a range of {QuantityType} instead of BigDecimal.
  # @return [Range, nil]
  # @note State descriptions can be provided by bindings, defined in
  #   metadata, or theoretically come from other sources.
  def range
    return unless (sd = state_description)

    # check if we have a unit, even if the item's metadata doesn't declare
    # it properly
    unit = self.unit || ((s = state) && s.is_a?(QuantityType) && s.unit)
    min = sd.minimum&.to_d
    max = sd.maximum&.to_d
    return nil unless min || max

    min |= unit if min && unit
    max |= unit if max && unit
    min..max
  end

  protected

  # Adds the unit dimension
  def type_details
    ":#{dimension}" if dimension
  end
end

Instance Method Details

#type_detailsObject (protected)

Adds the unit dimension



83
84
85
# File 'lib/openhab/core/items/number_item.rb', line 83

def type_details
  ":#{dimension}" if dimension
end