|
ESPHome 2025.12.0-dev
|
Base-class for all sensors. More...
#include <sensor.h>
Data Structures | |
| struct | SensorFlags |
Public Member Functions | |
| Sensor () | |
| int8_t | get_accuracy_decimals () |
| Get the accuracy in decimals, using the manual override if set. | |
| void | set_accuracy_decimals (int8_t accuracy_decimals) |
| Manually set the accuracy in decimals. | |
| StateClass | get_state_class () |
| Get the state class, using the manual override if set. | |
| void | set_state_class (StateClass state_class) |
| Manually set the state class. | |
| bool | get_force_update () const |
| Get whether force update mode is enabled. | |
| void | set_force_update (bool force_update) |
| Set force update mode. | |
| void | add_filter (Filter *filter) |
| Add a filter to the filter chain. Will be appended to the back. | |
| void | add_filters (std::initializer_list< Filter * > filters) |
| Add a list of vectors to the back of the filter chain. | |
| void | set_filters (std::initializer_list< Filter * > filters) |
| Clear the filters and replace them by filters. | |
| void | clear_filters () |
| Clear the entire filter chain. | |
| float | get_state () const |
| Getter-syntax for .state. | |
| float | get_raw_state () const |
| Getter-syntax for .raw_state. | |
| void | publish_state (float state) |
| Publish a new state to the front-end. | |
| void | add_on_state_callback (std::function< void(float)> &&callback) |
| Add a callback that will be called every time a filtered value arrives. | |
| void | add_on_raw_state_callback (std::function< void(float)> &&callback) |
| Add a callback that will be called every time the sensor sends a raw value. | |
| void | internal_send_state_to_frontend (float state) |
Public Member Functions inherited from esphome::EntityBase | |
| const StringRef & | get_name () const |
| void | set_name (const char *name) |
| bool | has_own_name () const |
| std::string | get_object_id () const |
| void | set_object_id (const char *object_id) |
| uint32_t | get_object_id_hash () |
| bool | is_internal () const |
| void | set_internal (bool internal) |
| bool | is_disabled_by_default () const |
| void | set_disabled_by_default (bool disabled_by_default) |
| EntityCategory | get_entity_category () const |
| void | set_entity_category (EntityCategory entity_category) |
| ESPDEPRECATED("Use get_icon_ref() instead for better performance (avoids string copy). Will be removed in ESPHome 2026.5.0", "2025.11.0") std void | set_icon (const char *icon) |
| StringRef | get_icon_ref () const |
| uint32_t | get_device_id () const |
| void | set_device (Device *device) |
| bool | has_state () const |
| void | set_has_state (bool state) |
| uint32_t | get_preference_hash () |
| Get a unique hash for storing preferences/settings for this entity. | |
Public Member Functions inherited from esphome::EntityBase_DeviceClass | |
| ESPDEPRECATED("Use get_device_class_ref() instead for better performance (avoids string copy). Will be removed in " "ESPHome 2026.5.0", "2025.11.0") std void | set_device_class (const char *device_class) |
| Get the device class, using the manual override if set. | |
| StringRef | get_device_class_ref () const |
| Get the device class as StringRef. | |
Public Member Functions inherited from esphome::EntityBase_UnitOfMeasurement | |
| ESPDEPRECATED("Use get_unit_of_measurement_ref() instead for better performance (avoids string copy). Will be " "removed in ESPHome 2026.5.0", "2025.11.0") std void | set_unit_of_measurement (const char *unit_of_measurement) |
| Get the unit of measurement, using the manual override if set. | |
| StringRef | get_unit_of_measurement_ref () const |
| Get the unit of measurement as StringRef. | |
Data Fields | |
| float | state |
| This member variable stores the last state that has passed through all filters. | |
| float | raw_state |
| This member variable stores the current raw state of the sensor, without any filters applied. | |
Protected Attributes | |
| std::unique_ptr< CallbackManager< void(float)> > | raw_callback_ |
| Storage for raw state callbacks (lazy allocated). | |
| CallbackManager< void(float)> | callback_ |
| Storage for filtered state callbacks. | |
| Filter * | filter_list_ {nullptr} |
| Store all active filters. | |
| int8_t | accuracy_decimals_ {-1} |
| Accuracy in decimals (-1 = not set) | |
| StateClass | state_class_ {STATE_CLASS_NONE} |
| State class (STATE_CLASS_NONE = not set) | |
| struct esphome::sensor::Sensor::SensorFlags | sensor_flags_ |
Protected Attributes inherited from esphome::EntityBase | |
| StringRef | name_ |
| const char * | object_id_c_str_ {nullptr} |
| const char * | icon_c_str_ {nullptr} |
| uint32_t | object_id_hash_ {} |
| Device * | device_ {} |
| struct esphome::EntityBase::EntityFlags | flags_ |
Protected Attributes inherited from esphome::EntityBase_DeviceClass | |
| const char * | device_class_ {nullptr} |
| Device class override. | |
Protected Attributes inherited from esphome::EntityBase_UnitOfMeasurement | |
| const char * | unit_of_measurement_ {nullptr} |
| Unit of measurement override. | |
Additional Inherited Members | |
Protected Member Functions inherited from esphome::EntityBase | |
| StringRef | get_object_id_ref_for_api_ () const |
| void | calc_object_id_ () |
| bool | is_object_id_dynamic_ () const |
| Check if the object_id is dynamic (changes with MAC suffix) | |
Base-class for all sensors.
A sensor has unit of measurement and can use publish_state to send out a new value with the specified accuracy.
|
explicit |
Definition at line 53 of file sensor.cpp.
| void esphome::sensor::Sensor::add_filter | ( | Filter * | filter | ) |
Add a filter to the filter chain. Will be appended to the back.
Definition at line 98 of file sensor.cpp.
| void esphome::sensor::Sensor::add_filters | ( | std::initializer_list< Filter * > | filters | ) |
Add a list of vectors to the back of the filter chain.
This may look like:
sensor->add_filters({ LambdaFilter([&](float value) -> optional<float> { return 42/value; }), OffsetFilter(1), SlidingWindowMovingAverageFilter(15, 15), // average over last 15 values });
Definition at line 112 of file sensor.cpp.
| void esphome::sensor::Sensor::add_on_raw_state_callback | ( | std::function< void(float)> && | callback | ) |
Add a callback that will be called every time the sensor sends a raw value.
Definition at line 91 of file sensor.cpp.
| void esphome::sensor::Sensor::add_on_state_callback | ( | std::function< void(float)> && | callback | ) |
Add a callback that will be called every time a filtered value arrives.
Definition at line 90 of file sensor.cpp.
| void esphome::sensor::Sensor::clear_filters | ( | ) |
Clear the entire filter chain.
Definition at line 121 of file sensor.cpp.
| int8_t esphome::sensor::Sensor::get_accuracy_decimals | ( | ) |
Get the accuracy in decimals, using the manual override if set.
Definition at line 55 of file sensor.cpp.
|
inline |
| float esphome::sensor::Sensor::get_raw_state | ( | ) | const |
Getter-syntax for .raw_state.
Definition at line 128 of file sensor.cpp.
| float esphome::sensor::Sensor::get_state | ( | ) | const |
Getter-syntax for .state.
Definition at line 127 of file sensor.cpp.
| StateClass esphome::sensor::Sensor::get_state_class | ( | ) |
Get the state class, using the manual override if set.
Definition at line 69 of file sensor.cpp.
| void esphome::sensor::Sensor::internal_send_state_to_frontend | ( | float | state | ) |
Definition at line 130 of file sensor.cpp.
| void esphome::sensor::Sensor::publish_state | ( | float | state | ) |
Publish a new state to the front-end.
First, the new state will be assigned to the raw_value. Then it's passed through all filters until it finally lands in the .value member variable and a callback is issued.
| state | The state as a floating point number. |
Definition at line 75 of file sensor.cpp.
| void esphome::sensor::Sensor::set_accuracy_decimals | ( | int8_t | accuracy_decimals | ) |
Manually set the accuracy in decimals.
Definition at line 60 of file sensor.cpp.
| void esphome::sensor::Sensor::set_filters | ( | std::initializer_list< Filter * > | filters | ) |
Clear the filters and replace them by filters.
Definition at line 117 of file sensor.cpp.
|
inline |
| void esphome::sensor::Sensor::set_state_class | ( | StateClass | state_class | ) |
Manually set the state class.
Definition at line 65 of file sensor.cpp.
|
protected |
|
protected |
|
protected |
|
protected |
| float esphome::sensor::Sensor::raw_state |
|
protected |
| float esphome::sensor::Sensor::state |
This member variable stores the last state that has passed through all filters.
On startup, when no state is available yet, this is NAN (not-a-number) and the validity can be checked using has_state().
This is exposed through a member variable for ease of use in esphome lambdas.
|
protected |