ESPHome  2024.11.3
Public Member Functions | Data Fields | Protected Attributes
esphome::sensor::Sensor Class Reference

Base-class for all sensors. More...

#include <sensor.h>

Inheritance diagram for esphome::sensor::Sensor:
esphome::EntityBase esphome::EntityBase_DeviceClass esphome::EntityBase_UnitOfMeasurement esphome::a01nyub::A01nyubComponent esphome::a02yyuw::A02yyuwComponent esphome::absolute_humidity::AbsoluteHumidityComponent esphome::adc128s102::ADC128S102Sensor esphome::adc::ADCSensor esphome::ade7953_base::ADE7953 esphome::ads1115::ADS1115Sensor esphome::ads1118::ADS1118Sensor esphome::apds9306::APDS9306 esphome::as5600::AS5600Sensor esphome::bh1750::BH1750Sensor esphome::binary_sensor_map::BinarySensorMap esphome::ble_client::BLEClientRSSISensor esphome::ble_client::BLESensor esphome::ble_rssi::BLERSSISensor esphome::cd74hc4067::CD74HC4067Sensor esphome::combination::CombinationComponent esphome::copy::CopySensor esphome::ct_clamp::CTClampSensor esphome::dallas_temp::DallasTemperatureSensor esphome::demo::DemoSensor esphome::duty_cycle::DutyCycleSensor esphome::duty_time_sensor::DutyTimeSensor esphome::ens160_base::ENS160Component esphome::esp32_hall::ESP32HallSensor esphome::ezo::EZOSensor esphome::fs3000::FS3000Component esphome::gp2y1010au0f::GP2Y1010AU0FSensor esphome::homeassistant::HomeassistantSensor esphome::hrxl_maxsonar_wr::HrxlMaxsonarWrComponent esphome::hx711::HX711Sensor esphome::integration::IntegrationSensor esphome::internal_temperature::InternalTemperatureSensor esphome::jsn_sr04t::Jsnsr04tComponent esphome::ld2420::LD2420Sensor esphome::m5stack_8angle::M5Stack8AngleKnobSensor esphome::max31855::MAX31855Sensor esphome::max31856::MAX31856Sensor esphome::max31865::MAX31865Sensor esphome::max44009::MAX44009Sensor esphome::max6675::MAX6675Sensor esphome::mcp3008::MCP3008Sensor esphome::mcp3204::MCP3204Sensor esphome::mcp9808::MCP9808Sensor esphome::micronova::MicroNovaSensor esphome::modbus_controller::ModbusSensor esphome::mqtt_subscribe::MQTTSubscribeSensor esphome::nau7802::NAU7802Sensor esphome::nextion::NextionSensor esphome::ntc::NTC esphome::pid::PIDClimateSensor esphome::pulse_counter::PulseCounterSensor esphome::pulse_meter::PulseMeterSensor esphome::pulse_width::PulseWidthSensor esphome::resistance::ResistanceSensor esphome::rotary_encoder::RotaryEncoderSensor esphome::sdp3x::SDP3XComponent esphome::sen0321_sensor::Sen0321Sensor esphome::sen21231_sensor::Sen21231Sensor esphome::sgp4x::SGP4xComponent esphome::sml::SmlSensor esphome::sts3x::STS3XComponent esphome::sun::SunSensor esphome::tc74::TC74Component esphome::tee501::TEE501Component esphome::teleinfo::TeleInfoSensor esphome::template_::TemplateSensor esphome::tmp102::TMP102Component esphome::tmp1075::TMP1075Sensor esphome::tmp117::TMP117Component esphome::tof10120::TOF10120Sensor esphome::total_daily_energy::TotalDailyEnergy esphome::tsl2561::TSL2561Sensor esphome::tuya::TuyaSensor esphome::ultrasonic::UltrasonicSensorComponent esphome::uponor_smatrix::UponorSmatrixSensor esphome::uptime::UptimeSecondsSensor esphome::uptime::UptimeTimestampSensor esphome::vbus::VBusCustomSubSensor esphome::veml3235::VEML3235Sensor esphome::vl53l0x::VL53L0XSensor esphome::wifi_signal::WiFiSignalSensor esphome::xgzp68xx::XGZP68XXComponent esphome::zio_ultrasonic::ZioUltrasonicComponent

Public Member Functions

 Sensor ()
 
int8_t get_accuracy_decimals ()
 Get the accuracy in decimals, using the manual override if set. More...
 
void set_accuracy_decimals (int8_t accuracy_decimals)
 Manually set the accuracy in decimals. More...
 
StateClass get_state_class ()
 Get the state class, using the manual override if set. More...
 
void set_state_class (StateClass state_class)
 Manually set the state class. More...
 
bool get_force_update () const
 Get whether force update mode is enabled. More...
 
void set_force_update (bool force_update)
 Set force update mode. More...
 
void add_filter (Filter *filter)
 Add a filter to the filter chain. Will be appended to the back. More...
 
void add_filters (const std::vector< Filter *> &filters)
 Add a list of vectors to the back of the filter chain. More...
 
void set_filters (const std::vector< Filter *> &filters)
 Clear the filters and replace them by filters. More...
 
void clear_filters ()
 Clear the entire filter chain. More...
 
float get_state () const
 Getter-syntax for .state. More...
 
float get_raw_state () const
 Getter-syntax for .raw_state. More...
 
void publish_state (float state)
 Publish a new state to the front-end. More...
 
void add_on_state_callback (std::function< void(float)> &&callback)
 Add a callback that will be called every time a filtered value arrives. More...
 
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. More...
 
bool has_state () const
 Return whether this sensor has gotten a full state (that passed through all filters) yet. More...
 
virtual std::string unique_id ()
 Override this method to set the unique ID of this sensor. More...
 
void internal_send_state_to_frontend (float state)
 
- Public Member Functions inherited from esphome::EntityBase
const StringRefget_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)
 
std::string get_icon () const
 
void set_icon (const char *icon)
 
- Public Member Functions inherited from esphome::EntityBase_DeviceClass
std::string get_device_class ()
 Get the device class, using the manual override if set. More...
 
void set_device_class (const char *device_class)
 Manually set the device class. More...
 
- Public Member Functions inherited from esphome::EntityBase_UnitOfMeasurement
std::string get_unit_of_measurement ()
 Get the unit of measurement, using the manual override if set. More...
 
void set_unit_of_measurement (const char *unit_of_measurement)
 Manually set the unit of measurement. More...
 

Data Fields

float state
 This member variable stores the last state that has passed through all filters. More...
 
float raw_state
 This member variable stores the current raw state of the sensor, without any filters applied. More...
 

Protected Attributes

CallbackManager< void(float)> raw_callback_
 Storage for raw state callbacks. More...
 
CallbackManager< void(float)> callback_
 Storage for filtered state callbacks. More...
 
Filterfilter_list_ {nullptr}
 Store all active filters. More...
 
optional< int8_t > accuracy_decimals_
 Accuracy in decimals override. More...
 
optional< StateClassstate_class_ {STATE_CLASS_NONE}
 State class override. More...
 
bool force_update_ {false}
 Force update mode. More...
 
bool has_state_ {false}
 
- 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_
 
bool has_own_name_ {false}
 
bool internal_ {false}
 
bool disabled_by_default_ {false}
 
EntityCategory entity_category_ {ENTITY_CATEGORY_NONE}
 
- Protected Attributes inherited from esphome::EntityBase_DeviceClass
const char * device_class_ {nullptr}
 Device class override. More...
 
- Protected Attributes inherited from esphome::EntityBase_UnitOfMeasurement
const char * unit_of_measurement_ {nullptr}
 Unit of measurement override. More...
 

Additional Inherited Members

- Protected Member Functions inherited from esphome::EntityBase
virtual uint32_t hash_base ()
 The hash_base() function has been deprecated. More...
 
void calc_object_id_ ()
 

Detailed Description

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.

Definition at line 57 of file sensor.h.

Constructor & Destructor Documentation

◆ Sensor()

esphome::sensor::Sensor::Sensor ( )
explicit

Definition at line 23 of file sensor.cpp.

Member Function Documentation

◆ add_filter()

void esphome::sensor::Sensor::add_filter ( Filter filter)

Add a filter to the filter chain. Will be appended to the back.

Definition at line 57 of file sensor.cpp.

◆ add_filters()

void esphome::sensor::Sensor::add_filters ( const std::vector< 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 71 of file sensor.cpp.

◆ add_on_raw_state_callback()

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 53 of file sensor.cpp.

◆ add_on_state_callback()

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 52 of file sensor.cpp.

◆ clear_filters()

void esphome::sensor::Sensor::clear_filters ( )

Clear the entire filter chain.

Definition at line 80 of file sensor.cpp.

◆ get_accuracy_decimals()

int8_t esphome::sensor::Sensor::get_accuracy_decimals ( )

Get the accuracy in decimals, using the manual override if set.

Definition at line 25 of file sensor.cpp.

◆ get_force_update()

bool esphome::sensor::Sensor::get_force_update ( ) const
inline

Get whether force update mode is enabled.

If the sensor is in force_update mode, the frontend is required to save all state changes to the database when they are published, even if the state is the same as before.

Definition at line 78 of file sensor.h.

◆ get_raw_state()

float esphome::sensor::Sensor::get_raw_state ( ) const

Getter-syntax for .raw_state.

Definition at line 87 of file sensor.cpp.

◆ get_state()

float esphome::sensor::Sensor::get_state ( ) const

Getter-syntax for .state.

Definition at line 86 of file sensor.cpp.

◆ get_state_class()

StateClass esphome::sensor::Sensor::get_state_class ( )

Get the state class, using the manual override if set.

Definition at line 33 of file sensor.cpp.

◆ has_state()

bool esphome::sensor::Sensor::has_state ( ) const

Return whether this sensor has gotten a full state (that passed through all filters) yet.

Definition at line 97 of file sensor.cpp.

◆ internal_send_state_to_frontend()

void esphome::sensor::Sensor::internal_send_state_to_frontend ( float  state)

Definition at line 90 of file sensor.cpp.

◆ publish_state()

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.

Parameters
stateThe state as a floating point number.

Definition at line 39 of file sensor.cpp.

◆ set_accuracy_decimals()

void esphome::sensor::Sensor::set_accuracy_decimals ( int8_t  accuracy_decimals)

Manually set the accuracy in decimals.

Definition at line 30 of file sensor.cpp.

◆ set_filters()

void esphome::sensor::Sensor::set_filters ( const std::vector< Filter *> &  filters)

Clear the filters and replace them by filters.

Definition at line 76 of file sensor.cpp.

◆ set_force_update()

void esphome::sensor::Sensor::set_force_update ( bool  force_update)
inline

Set force update mode.

Definition at line 80 of file sensor.h.

◆ set_state_class()

void esphome::sensor::Sensor::set_state_class ( StateClass  state_class)

Manually set the state class.

Definition at line 32 of file sensor.cpp.

◆ unique_id()

std::string esphome::sensor::Sensor::unique_id ( )
virtual

Override this method to set the unique ID of this sensor.

Reimplemented in esphome::adc::ADCSensor, esphome::esp32_hall::ESP32HallSensor, esphome::uptime::UptimeSecondsSensor, and esphome::wifi_signal::WiFiSignalSensor.

Definition at line 88 of file sensor.cpp.

Field Documentation

◆ accuracy_decimals_

optional<int8_t> esphome::sensor::Sensor::accuracy_decimals_
protected

Accuracy in decimals override.

Definition at line 156 of file sensor.h.

◆ callback_

CallbackManager<void(float)> esphome::sensor::Sensor::callback_
protected

Storage for filtered state callbacks.

Definition at line 152 of file sensor.h.

◆ filter_list_

Filter* esphome::sensor::Sensor::filter_list_ {nullptr}
protected

Store all active filters.

Definition at line 154 of file sensor.h.

◆ force_update_

bool esphome::sensor::Sensor::force_update_ {false}
protected

Force update mode.

Definition at line 158 of file sensor.h.

◆ has_state_

bool esphome::sensor::Sensor::has_state_ {false}
protected

Definition at line 159 of file sensor.h.

◆ raw_callback_

CallbackManager<void(float)> esphome::sensor::Sensor::raw_callback_
protected

Storage for raw state callbacks.

Definition at line 151 of file sensor.h.

◆ raw_state

float esphome::sensor::Sensor::raw_state

This member variable stores the current raw state of the sensor, without any filters applied.

Unlike .state,this will be updated immediately when publish_state is called.

Definition at line 137 of file sensor.h.

◆ state

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.

Definition at line 131 of file sensor.h.

◆ state_class_

optional<StateClass> esphome::sensor::Sensor::state_class_ {STATE_CLASS_NONE}
protected

State class override.

Definition at line 157 of file sensor.h.


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