Home Assistant Unofficial Reference 2024.12.1
homeassistant.components.statistics.sensor.StatisticsSensor Class Reference
Inheritance diagram for homeassistant.components.statistics.sensor.StatisticsSensor:
[legend]
Collaboration diagram for homeassistant.components.statistics.sensor.StatisticsSensor:
[legend]

Public Member Functions

None __init__ (self, HomeAssistant hass, str source_entity_id, str name, str|None unique_id, str state_characteristic, int|None samples_max_buffer_size, timedelta|None samples_max_age, bool samples_keep_last, int precision, int percentile)
 
None async_added_to_hass (self)
 
CALLBACK_TYPE async_start_preview (self, Callable[[str, Mapping[str, Any]], None] preview_callback)
 
None async_update (self)
 
- Public Member Functions inherited from homeassistant.components.sensor.SensorEntity
None add_to_platform_start (self, HomeAssistant hass, EntityPlatform platform, asyncio.Semaphore|None parallel_updates)
 
None async_internal_added_to_hass (self)
 
None async_registry_entry_updated (self)
 
dict[str, Any]|None capability_attributes (self)
 
SensorDeviceClass|None device_class (self)
 
er.EntityOptionsType|None get_initial_entity_options (self)
 
datetime|None last_reset (self)
 
str|None native_unit_of_measurement (self)
 
StateType|date|datetime|Decimal native_value (self)
 
list[str]|None options (self)
 
Any state (self)
 
dict[str, Any]|None state_attributes (self)
 
SensorStateClass|str|None state_class (self)
 
int|None suggested_display_precision (self)
 
str|None suggested_unit_of_measurement (self)
 
str|None unit_of_measurement (self)
 
- Public Member Functions inherited from homeassistant.helpers.entity.Entity
None __init_subclass__ (cls, **Any kwargs)
 
str __repr__ (self)
 
None add_to_platform_abort (self)
 
None add_to_platform_finish (self)
 
bool assumed_state (self)
 
None async_device_update (self, bool warning=True)
 
None async_internal_will_remove_from_hass (self)
 
None async_on_remove (self, CALLBACK_TYPE func)
 
None async_remove (self, *bool force_remove=False)
 
None async_removed_from_registry (self)
 
None async_schedule_update_ha_state (self, bool force_refresh=False)
 
None async_set_context (self, Context context)
 
None async_update_ha_state (self, bool force_refresh=False)
 
None async_will_remove_from_hass (self)
 
None async_write_ha_state (self)
 
str|None attribution (self)
 
bool available (self)
 
DeviceInfo|None device_info (self)
 
bool enabled (self)
 
EntityCategory|None entity_category (self)
 
str|None entity_picture (self)
 
bool entity_registry_enabled_default (self)
 
bool entity_registry_visible_default (self)
 
Mapping[str, Any]|None extra_state_attributes (self)
 
bool force_update (self)
 
HassJobType get_hassjob_type (self, str function_name)
 
bool has_entity_name (self)
 
str|None icon (self)
 
str|UndefinedType|None name (self)
 
None schedule_update_ha_state (self, bool force_refresh=False)
 
bool should_poll (self)
 
str|None suggested_object_id (self)
 
int|None supported_features (self)
 
str|None translation_key (self)
 
Mapping[str, strtranslation_placeholders (self)
 
str|None unique_id (self)
 
bool use_device_name (self)
 
- Public Member Functions inherited from homeassistant.helpers.entity.CachedProperties
None __init__ (cls, str name, tuple[type,...] bases, dict[Any, Any] namespace, **Any kwargs)
 
Any __new__ (mcs, str name, tuple[type,...] bases, dict[Any, Any] namespace, set[str]|None cached_properties=None, **Any kwargs)
 

Private Member Functions

None _add_state_to_queue (self, State new_state)
 
None _async_cancel_update_listener (self)
 
None _async_handle_new_state (self, State|None reported_state)
 
datetime|None _async_next_to_purge_timestamp (self)
 
None _async_purge_update_and_schedule (self)
 
None _async_scheduled_update (self, datetime now)
 
None _async_stats_sensor_startup (self)
 
None _async_stats_sensor_state_change_listener (self, Event[EventStateChangedData] event)
 
None _async_stats_sensor_state_report_listener (self, Event[EventStateReportedData] event)
 
SensorDeviceClass|None _calculate_device_class (self, State new_state, str|None unit)
 
None _calculate_state_attributes (self, State new_state)
 
SensorStateClass|None _calculate_state_class (self, State new_state)
 
str|None _calculate_unit_of_measurement (self, State new_state)
 
Callable[[], float|int|datetime|None_callable_characteristic_fn (self, str characteristic)
 
list[State_fetch_states_from_database (self)
 
None _initialize_from_database (self)
 
None _purge_old_states (self, timedelta max_age)
 
StateType _stat_average_linear (self)
 
StateType _stat_average_step (self)
 
StateType _stat_average_timeless (self)
 
StateType _stat_binary_average_step (self)
 
StateType _stat_binary_average_timeless (self)
 
StateType _stat_binary_count (self)
 
StateType _stat_binary_count_off (self)
 
StateType _stat_binary_count_on (self)
 
datetime|None _stat_binary_datetime_newest (self)
 
datetime|None _stat_binary_datetime_oldest (self)
 
StateType _stat_binary_mean (self)
 
StateType _stat_change (self)
 
StateType _stat_change_sample (self)
 
StateType _stat_change_second (self)
 
StateType _stat_count (self)
 
datetime|None _stat_datetime_newest (self)
 
datetime|None _stat_datetime_oldest (self)
 
datetime|None _stat_datetime_value_max (self)
 
datetime|None _stat_datetime_value_min (self)
 
StateType _stat_distance_95_percent_of_values (self)
 
StateType _stat_distance_99_percent_of_values (self)
 
StateType _stat_distance_absolute (self)
 
StateType _stat_mean (self)
 
StateType _stat_mean_circular (self)
 
StateType _stat_median (self)
 
StateType _stat_noisiness (self)
 
StateType _stat_percentile (self)
 
StateType _stat_standard_deviation (self)
 
StateType _stat_sum (self)
 
StateType _stat_sum_differences (self)
 
StateType _stat_sum_differences_nonnegative (self)
 
StateType _stat_total (self)
 
StateType _stat_value_max (self)
 
StateType _stat_value_min (self)
 
StateType _stat_variance (self)
 
None _update_extra_state_attributes (self)
 
None _update_value (self)
 

Private Attributes

 _attr_available
 
 _attr_device_class
 
 _attr_device_info
 
 _attr_extra_state_attributes
 
 _attr_native_unit_of_measurement
 
 _attr_native_value
 
 _attr_state_class
 
 _precision
 
 _preview_callback
 
 _state_characteristic
 
 _update_listener
 

Static Private Attributes

 _attr_icon = ICON
 
bool _attr_should_poll = False
 

Additional Inherited Members

- Public Attributes inherited from homeassistant.components.sensor.SensorEntity
 entity_category
 
 registry_entry
 
 unit_of_measurement
 
- Public Attributes inherited from homeassistant.helpers.entity.Entity
 device_entry
 
 entity_id
 
 hass
 
 parallel_updates
 
 platform
 
 registry_entry
 
- Static Public Attributes inherited from homeassistant.components.sensor.SensorEntity
 Decimal
 
 None
 
 UndefinedType
 
- Static Public Attributes inherited from homeassistant.helpers.entity.Entity
 bool
 
 EntityPlatform
 
 HomeAssistant
 
 None
 
 StateInfo
 
 StateType
 
 str
 

Detailed Description

Representation of a Statistics sensor.

Definition at line 327 of file sensor.py.

Constructor & Destructor Documentation

◆ __init__()

None homeassistant.components.statistics.sensor.StatisticsSensor.__init__ (   self,
HomeAssistant  hass,
str  source_entity_id,
str  name,
str | None  unique_id,
str  state_characteristic,
int | None  samples_max_buffer_size,
timedelta | None  samples_max_age,
bool  samples_keep_last,
int  precision,
int  percentile 
)
Initialize the Statistics sensor.

Definition at line 333 of file sensor.py.

Member Function Documentation

◆ _add_state_to_queue()

None homeassistant.components.statistics.sensor.StatisticsSensor._add_state_to_queue (   self,
State  new_state 
)
private
Add the state to the queue.

Definition at line 457 of file sensor.py.

◆ _async_cancel_update_listener()

None homeassistant.components.statistics.sensor.StatisticsSensor._async_cancel_update_listener (   self)
private
Cancel the scheduled update listener.

Definition at line 667 of file sensor.py.

◆ _async_handle_new_state()

None homeassistant.components.statistics.sensor.StatisticsSensor._async_handle_new_state (   self,
State | None  reported_state 
)
private
Handle the sensor state changes.

Definition at line 397 of file sensor.py.

◆ _async_next_to_purge_timestamp()

datetime | None homeassistant.components.statistics.sensor.StatisticsSensor._async_next_to_purge_timestamp (   self)
private
Find the timestamp when the next purge would occur.

Definition at line 623 of file sensor.py.

◆ _async_purge_update_and_schedule()

None homeassistant.components.statistics.sensor.StatisticsSensor._async_purge_update_and_schedule (   self)
private
Purge old states, update the sensor and schedule the next update.

Definition at line 647 of file sensor.py.

◆ _async_scheduled_update()

None homeassistant.components.statistics.sensor.StatisticsSensor._async_scheduled_update (   self,
datetime  now 
)
private
Timer callback for sensor update.

Definition at line 674 of file sensor.py.

◆ _async_stats_sensor_startup()

None homeassistant.components.statistics.sensor.StatisticsSensor._async_stats_sensor_startup (   self)
private
Add listener and get recorded state.

Historical data needs to be loaded from the database first before we
can start accepting new incoming changes.
This is needed to ensure that the buffer is properly sorted by time.

Definition at line 428 of file sensor.py.

◆ _async_stats_sensor_state_change_listener()

None homeassistant.components.statistics.sensor.StatisticsSensor._async_stats_sensor_state_change_listener (   self,
Event[EventStateChangedData event 
)
private

Definition at line 415 of file sensor.py.

◆ _async_stats_sensor_state_report_listener()

None homeassistant.components.statistics.sensor.StatisticsSensor._async_stats_sensor_state_report_listener (   self,
Event[EventStateReportedData event 
)
private

Definition at line 422 of file sensor.py.

◆ _calculate_device_class()

SensorDeviceClass | None homeassistant.components.statistics.sensor.StatisticsSensor._calculate_device_class (   self,
State  new_state,
str | None   unit 
)
private
Return the calculated device class.

The device class is calculated based on the state characteristics,
the source device class and the unit of measurement is
in the device class units list.

Definition at line 535 of file sensor.py.

◆ _calculate_state_attributes()

None homeassistant.components.statistics.sensor.StatisticsSensor._calculate_state_attributes (   self,
State  new_state 
)
private
Set the entity state attributes.

Definition at line 490 of file sensor.py.

◆ _calculate_state_class()

SensorStateClass | None homeassistant.components.statistics.sensor.StatisticsSensor._calculate_state_class (   self,
State  new_state 
)
private
Return the calculated state class.

Will be None if the characteristics is not numerical, otherwise
SensorStateClass.MEASUREMENT.

Definition at line 577 of file sensor.py.

◆ _calculate_unit_of_measurement()

str | None homeassistant.components.statistics.sensor.StatisticsSensor._calculate_unit_of_measurement (   self,
State  new_state 
)
private
Return the calculated unit of measurement.

The unit of measurement is that of the source sensor, adjusted based on the
state characteristics.

Definition at line 501 of file sensor.py.

◆ _callable_characteristic_fn()

Callable[[], float | int | datetime | None] homeassistant.components.statistics.sensor.StatisticsSensor._callable_characteristic_fn (   self,
str   characteristic 
)
private
Return the function callable of one characteristic function.

Definition at line 768 of file sensor.py.

◆ _fetch_states_from_database()

list[State] homeassistant.components.statistics.sensor.StatisticsSensor._fetch_states_from_database (   self)
private
Fetch the states from the database.

Definition at line 683 of file sensor.py.

◆ _initialize_from_database()

None homeassistant.components.statistics.sensor.StatisticsSensor._initialize_from_database (   self)
private
Initialize the list of states from the database.

The query will get the list of states in DESCENDING order so that we
can limit the result to self._sample_size. Afterwards reverse the
list so that we get it in the right order again.

If MaxAge is provided then query will restrict to entries younger then
current datetime - MaxAge.

Definition at line 708 of file sensor.py.

◆ _purge_old_states()

None homeassistant.components.statistics.sensor.StatisticsSensor._purge_old_states (   self,
timedelta  max_age 
)
private
Remove states which are older than a given age.

Definition at line 587 of file sensor.py.

◆ _stat_average_linear()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_average_linear (   self)
private

Definition at line 782 of file sensor.py.

◆ _stat_average_step()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_average_step (   self)
private

Definition at line 797 of file sensor.py.

◆ _stat_average_timeless()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_average_timeless (   self)
private

Definition at line 811 of file sensor.py.

◆ _stat_binary_average_step()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_binary_average_step (   self)
private

Definition at line 955 of file sensor.py.

◆ _stat_binary_average_timeless()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_binary_average_timeless (   self)
private

Definition at line 967 of file sensor.py.

◆ _stat_binary_count()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_binary_count (   self)
private

Definition at line 970 of file sensor.py.

◆ _stat_binary_count_off()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_binary_count_off (   self)
private

Definition at line 976 of file sensor.py.

◆ _stat_binary_count_on()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_binary_count_on (   self)
private

Definition at line 973 of file sensor.py.

◆ _stat_binary_datetime_newest()

datetime | None homeassistant.components.statistics.sensor.StatisticsSensor._stat_binary_datetime_newest (   self)
private

Definition at line 979 of file sensor.py.

◆ _stat_binary_datetime_oldest()

datetime | None homeassistant.components.statistics.sensor.StatisticsSensor._stat_binary_datetime_oldest (   self)
private

Definition at line 982 of file sensor.py.

◆ _stat_binary_mean()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_binary_mean (   self)
private

Definition at line 985 of file sensor.py.

◆ _stat_change()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_change (   self)
private

Definition at line 814 of file sensor.py.

◆ _stat_change_sample()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_change_sample (   self)
private

Definition at line 819 of file sensor.py.

◆ _stat_change_second()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_change_second (   self)
private

Definition at line 824 of file sensor.py.

◆ _stat_count()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_count (   self)
private

Definition at line 831 of file sensor.py.

◆ _stat_datetime_newest()

datetime | None homeassistant.components.statistics.sensor.StatisticsSensor._stat_datetime_newest (   self)
private

Definition at line 834 of file sensor.py.

◆ _stat_datetime_oldest()

datetime | None homeassistant.components.statistics.sensor.StatisticsSensor._stat_datetime_oldest (   self)
private

Definition at line 839 of file sensor.py.

◆ _stat_datetime_value_max()

datetime | None homeassistant.components.statistics.sensor.StatisticsSensor._stat_datetime_value_max (   self)
private

Definition at line 844 of file sensor.py.

◆ _stat_datetime_value_min()

datetime | None homeassistant.components.statistics.sensor.StatisticsSensor._stat_datetime_value_min (   self)
private

Definition at line 849 of file sensor.py.

◆ _stat_distance_95_percent_of_values()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_distance_95_percent_of_values (   self)
private

Definition at line 854 of file sensor.py.

◆ _stat_distance_99_percent_of_values()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_distance_99_percent_of_values (   self)
private

Definition at line 859 of file sensor.py.

◆ _stat_distance_absolute()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_distance_absolute (   self)
private

Definition at line 864 of file sensor.py.

◆ _stat_mean()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_mean (   self)
private

Definition at line 869 of file sensor.py.

◆ _stat_mean_circular()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_mean_circular (   self)
private

Definition at line 874 of file sensor.py.

◆ _stat_median()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_median (   self)
private

Definition at line 881 of file sensor.py.

◆ _stat_noisiness()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_noisiness (   self)
private

Definition at line 886 of file sensor.py.

◆ _stat_percentile()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_percentile (   self)
private

Definition at line 893 of file sensor.py.

◆ _stat_standard_deviation()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_standard_deviation (   self)
private

Definition at line 901 of file sensor.py.

◆ _stat_sum()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_sum (   self)
private

Definition at line 908 of file sensor.py.

◆ _stat_sum_differences()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_sum_differences (   self)
private

Definition at line 913 of file sensor.py.

◆ _stat_sum_differences_nonnegative()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_sum_differences_nonnegative (   self)
private

Definition at line 923 of file sensor.py.

◆ _stat_total()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_total (   self)
private

Definition at line 933 of file sensor.py.

◆ _stat_value_max()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_value_max (   self)
private

Definition at line 936 of file sensor.py.

◆ _stat_value_min()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_value_min (   self)
private

Definition at line 941 of file sensor.py.

◆ _stat_variance()

StateType homeassistant.components.statistics.sensor.StatisticsSensor._stat_variance (   self)
private

Definition at line 946 of file sensor.py.

◆ _update_extra_state_attributes()

None homeassistant.components.statistics.sensor.StatisticsSensor._update_extra_state_attributes (   self)
private
Calculate and update the various attributes.

Definition at line 734 of file sensor.py.

◆ _update_value()

None homeassistant.components.statistics.sensor.StatisticsSensor._update_value (   self)
private
Front to call the right statistical characteristics functions.

One of the _stat_*() functions is represented by self._state_characteristic_fn().

Definition at line 751 of file sensor.py.

◆ async_added_to_hass()

None homeassistant.components.statistics.sensor.StatisticsSensor.async_added_to_hass (   self)
Register callbacks.

Reimplemented from homeassistant.helpers.entity.Entity.

Definition at line 453 of file sensor.py.

◆ async_start_preview()

CALLBACK_TYPE homeassistant.components.statistics.sensor.StatisticsSensor.async_start_preview (   self,
Callable[[str, Mapping[str, Any]], None preview_callback 
)
Render a preview.

Definition at line 376 of file sensor.py.

◆ async_update()

None homeassistant.components.statistics.sensor.StatisticsSensor.async_update (   self)
Get the latest data and updates the states.

Definition at line 643 of file sensor.py.

Member Data Documentation

◆ _attr_available

homeassistant.components.statistics.sensor.StatisticsSensor._attr_available
private

Definition at line 387 of file sensor.py.

◆ _attr_device_class

homeassistant.components.statistics.sensor.StatisticsSensor._attr_device_class
private

Definition at line 496 of file sensor.py.

◆ _attr_device_info

homeassistant.components.statistics.sensor.StatisticsSensor._attr_device_info
private

Definition at line 350 of file sensor.py.

◆ _attr_extra_state_attributes

homeassistant.components.statistics.sensor.StatisticsSensor._attr_extra_state_attributes
private

Definition at line 367 of file sensor.py.

◆ _attr_icon

homeassistant.components.statistics.sensor.StatisticsSensor._attr_icon = ICON
staticprivate

Definition at line 331 of file sensor.py.

◆ _attr_native_unit_of_measurement

homeassistant.components.statistics.sensor.StatisticsSensor._attr_native_unit_of_measurement
private

Definition at line 493 of file sensor.py.

◆ _attr_native_value

homeassistant.components.statistics.sensor.StatisticsSensor._attr_native_value
private

Definition at line 766 of file sensor.py.

◆ _attr_should_poll

bool homeassistant.components.statistics.sensor.StatisticsSensor._attr_should_poll = False
staticprivate

Definition at line 330 of file sensor.py.

◆ _attr_state_class

homeassistant.components.statistics.sensor.StatisticsSensor._attr_state_class
private

Definition at line 499 of file sensor.py.

◆ _precision

homeassistant.components.statistics.sensor.StatisticsSensor._precision
private

Definition at line 764 of file sensor.py.

◆ _preview_callback

homeassistant.components.statistics.sensor.StatisticsSensor._preview_callback
private

Definition at line 392 of file sensor.py.

◆ _state_characteristic

homeassistant.components.statistics.sensor.StatisticsSensor._state_characteristic
private

Definition at line 526 of file sensor.py.

◆ _update_listener

homeassistant.components.statistics.sensor.StatisticsSensor._update_listener
private

Definition at line 662 of file sensor.py.


The documentation for this class was generated from the following file: