ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
sensor.cpp
Go to the documentation of this file.
1#include "sensor.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace sensor {
6
7static const char *const TAG = "sensor";
8
9// Function implementation of LOG_SENSOR macro to reduce code size
10void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *obj) {
11 if (obj == nullptr) {
12 return;
13 }
14
15 ESP_LOGCONFIG(tag,
16 "%s%s '%s'\n"
17 "%s State Class: '%s'\n"
18 "%s Unit of Measurement: '%s'\n"
19 "%s Accuracy Decimals: %d",
20 prefix, type, obj->get_name().c_str(), prefix,
21 LOG_STR_ARG(state_class_to_string(obj->get_state_class())), prefix,
23
24 if (!obj->get_device_class_ref().empty()) {
25 ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->get_device_class_ref().c_str());
26 }
27
28 if (!obj->get_icon_ref().empty()) {
29 ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
30 }
31
32 if (obj->get_force_update()) {
33 ESP_LOGV(tag, "%s Force Update: YES", prefix);
34 }
35}
36
37const LogString *state_class_to_string(StateClass state_class) {
38 switch (state_class) {
40 return LOG_STR("measurement");
42 return LOG_STR("total_increasing");
44 return LOG_STR("total");
46 default:
47 return LOG_STR("");
48 }
49}
50
51Sensor::Sensor() : state(NAN), raw_state(NAN) {}
52
55 return this->accuracy_decimals_;
56 return 0;
57}
58void Sensor::set_accuracy_decimals(int8_t accuracy_decimals) {
59 this->accuracy_decimals_ = accuracy_decimals;
61}
62
64 this->state_class_ = state_class;
66}
72
74 this->raw_state = state;
75 if (this->raw_callback_) {
76 this->raw_callback_->call(state);
77 }
78
79 ESP_LOGV(TAG, "'%s': Received new state %f", this->name_.c_str(), state);
80
81 if (this->filter_list_ == nullptr) {
83 } else {
84 this->filter_list_->input(state);
85 }
86}
87
88void Sensor::add_on_state_callback(std::function<void(float)> &&callback) { this->callback_.add(std::move(callback)); }
89void Sensor::add_on_raw_state_callback(std::function<void(float)> &&callback) {
90 if (!this->raw_callback_) {
91 this->raw_callback_ = make_unique<CallbackManager<void(float)>>();
92 }
93 this->raw_callback_->add(std::move(callback));
94}
95
97 // inefficient, but only happens once on every sensor setup and nobody's going to have massive amounts of
98 // filters
99 ESP_LOGVV(TAG, "Sensor(%p)::add_filter(%p)", this, filter);
100 if (this->filter_list_ == nullptr) {
101 this->filter_list_ = filter;
102 } else {
103 Filter *last_filter = this->filter_list_;
104 while (last_filter->next_ != nullptr)
105 last_filter = last_filter->next_;
106 last_filter->initialize(this, filter);
107 }
108 filter->initialize(this, nullptr);
109}
110void Sensor::add_filters(const std::vector<Filter *> &filters) {
111 for (Filter *filter : filters) {
112 this->add_filter(filter);
113 }
114}
115void Sensor::set_filters(const std::vector<Filter *> &filters) {
116 this->clear_filters();
117 this->add_filters(filters);
118}
120 if (this->filter_list_ != nullptr) {
121 ESP_LOGVV(TAG, "Sensor(%p)::clear_filters()", this);
122 }
123 this->filter_list_ = nullptr;
124}
125float Sensor::get_state() const { return this->state; }
126float Sensor::get_raw_state() const { return this->raw_state; }
127
129 this->set_has_state(true);
130 this->state = state;
131 ESP_LOGD(TAG, "'%s': Sending state %.5f %s with %d decimals of accuracy", this->get_name().c_str(), state,
132 this->get_unit_of_measurement_ref().c_str(), this->get_accuracy_decimals());
133 this->callback_.call(state);
134}
135
136} // namespace sensor
137} // namespace esphome
StringRef get_device_class_ref() const
Get the device class as StringRef.
StringRef get_unit_of_measurement_ref() const
Get the unit of measurement as StringRef.
const StringRef & get_name() const
StringRef get_icon_ref() const
Definition entity_base.h:62
void set_has_state(bool state)
Definition entity_base.h:86
constexpr const char * c_str() const
Definition string_ref.h:69
constexpr bool empty() const
Definition string_ref.h:71
Apply a filter to sensor values such as moving average.
Definition filter.h:20
virtual void initialize(Sensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
Definition filter.cpp:29
void input(float value)
Definition filter.cpp:14
Base-class for all sensors.
Definition sensor.h:42
void set_state_class(StateClass state_class)
Manually set the state class.
Definition sensor.cpp:63
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition sensor.cpp:96
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:73
void internal_send_state_to_frontend(float state)
Definition sensor.cpp:128
float get_raw_state() const
Getter-syntax for .raw_state.
Definition sensor.cpp:126
CallbackManager< void(float)> callback_
Storage for filtered state callbacks.
Definition sensor.h:128
void add_filters(const std::vector< Filter * > &filters)
Add a list of vectors to the back of the filter chain.
Definition sensor.cpp:110
float get_state() const
Getter-syntax for .state.
Definition sensor.cpp:125
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals.
Definition sensor.cpp:58
void set_filters(const std::vector< Filter * > &filters)
Clear the filters and replace them by filters.
Definition sensor.cpp:115
void add_on_state_callback(std::function< void(float)> &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.cpp:88
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:116
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition sensor.cpp:67
Filter * filter_list_
Store all active filters.
Definition sensor.h:130
float raw_state
This member variable stores the current raw state of the sensor, without any filters applied.
Definition sensor.h:122
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition sensor.cpp:53
void clear_filters()
Clear the entire filter chain.
Definition sensor.cpp:119
int8_t accuracy_decimals_
Accuracy in decimals (-1 = not set)
Definition sensor.h:133
StateClass state_class_
State class (STATE_CLASS_NONE = not set)
Definition sensor.h:134
bool get_force_update() const
Get whether force update mode is enabled.
Definition sensor.h:63
struct esphome::sensor::Sensor::SensorFlags sensor_flags_
std::unique_ptr< CallbackManager< void(float)> > raw_callback_
Storage for raw state callbacks (lazy allocated).
Definition sensor.h:127
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.
Definition sensor.cpp:89
uint16_t type
bool state
Definition fan.h:0
StateClass
Sensor state classes.
Definition sensor.h:29
@ STATE_CLASS_TOTAL
Definition sensor.h:33
@ STATE_CLASS_TOTAL_INCREASING
Definition sensor.h:32
@ STATE_CLASS_MEASUREMENT
Definition sensor.h:31
void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *obj)
Definition sensor.cpp:10
const LogString * state_class_to_string(StateClass state_class)
Definition sensor.cpp:37
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7