ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
sensor.h
Go to the documentation of this file.
1#pragma once
2
6#include "esphome/core/log.h"
7#ifdef USE_SENSOR_FILTER
9#endif
10
11#include <initializer_list>
12#include <memory>
13
14namespace esphome::sensor {
15
16class Sensor;
17
18void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *obj);
19
20#define LOG_SENSOR(prefix, type, obj) log_sensor(TAG, prefix, LOG_STR_LITERAL(type), obj)
21
22#define SUB_SENSOR(name) \
23 protected: \
24 sensor::Sensor *name##_sensor_{nullptr}; \
25\
26 public: \
27 void set_##name##_sensor(sensor::Sensor *sensor) { this->name##_sensor_ = sensor; }
28
39constexpr uint8_t STATE_CLASS_LAST = static_cast<uint8_t>(STATE_CLASS_MEASUREMENT_ANGLE);
40
41const LogString *state_class_to_string(StateClass state_class);
42
47class Sensor : public EntityBase {
48 public:
49 explicit Sensor();
50
52 int8_t get_accuracy_decimals();
54 void set_accuracy_decimals(int8_t accuracy_decimals);
57
61 void set_state_class(StateClass state_class);
62
72 void set_force_update(bool force_update) { sensor_flags_.force_update = force_update; }
73
74#ifdef USE_SENSOR_FILTER
76 void add_filter(Filter *filter);
77
88 void add_filters(std::initializer_list<Filter *> filters);
89
91 void set_filters(std::initializer_list<Filter *> filters);
92
94 void clear_filters();
95#endif
96
98 float get_state() const { return this->state; }
100 float get_raw_state() const {
101#pragma GCC diagnostic push
102#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
103 return this->raw_state;
104#pragma GCC diagnostic pop
105 }
106
114 void publish_state(float state);
115
116 // ========== INTERNAL METHODS ==========
117 // (In most use cases you won't need these)
119 template<typename F> void add_on_state_callback(F &&callback) { this->callback_.add(std::forward<F>(callback)); }
123 template<typename F> void add_on_raw_state_callback(F &&callback) {
124#ifdef USE_SENSOR_FILTER
125 this->raw_callback_.add(std::forward<F>(callback));
126#else
127 this->callback_.add(std::forward<F>(callback));
128#endif
129 }
130
138 float state;
139
140#pragma GCC diagnostic push
141#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
143 ESPDEPRECATED("Use get_raw_state() instead of .raw_state. Will be removed in 2026.10.0", "2026.4.0")
144 float raw_state;
145#pragma GCC diagnostic pop
146
148
149 protected:
150#ifdef USE_SENSOR_FILTER
152#endif
154
155#ifdef USE_SENSOR_FILTER
157#endif
158
159 // Group small members together to avoid padding
162
163 // Bit-packed flags for sensor-specific settings
164 struct SensorFlags {
167 uint8_t force_update : 1;
168 uint8_t reserved : 5; // Reserved for future use
170};
171
172} // namespace esphome::sensor
Apply a filter to sensor values such as moving average.
Definition filter.h:22
Base-class for all sensors.
Definition sensor.h:47
ESPDEPRECATED("Use get_raw_state() instead of .raw_state. Will be removed in 2026.10.0", "2026.4.0") float raw_state
void set_state_class(StateClass state_class)
Manually set the state class.
Definition sensor.cpp:58
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition sensor.cpp:91
void add_on_state_callback(F &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.h:119
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
LazyCallbackManager< void(float)> callback_
Storage for filtered state callbacks.
Definition sensor.h:153
void internal_send_state_to_frontend(float state)
Definition sensor.cpp:122
void add_on_raw_state_callback(F &&callback)
Add a callback that will be called every time the sensor sends a raw value.
Definition sensor.h:123
float get_raw_state() const
Getter-syntax for .raw_state.
Definition sensor.h:100
void set_force_update(bool force_update)
Set force update mode.
Definition sensor.h:72
float get_state() const
Getter-syntax for .state.
Definition sensor.h:98
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals.
Definition sensor.cpp:53
bool has_accuracy_decimals() const
Check if the accuracy in decimals has been manually set.
Definition sensor.h:56
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:138
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition sensor.cpp:62
Filter * filter_list_
Store all active filters.
Definition sensor.h:156
LazyCallbackManager< void(float)> raw_callback_
Storage for raw state callbacks.
Definition sensor.h:151
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition sensor.cpp:48
void clear_filters()
Clear the entire filter chain.
Definition sensor.cpp:114
int8_t accuracy_decimals_
Accuracy in decimals (-1 = not set)
Definition sensor.h:160
StateClass state_class_
State class (STATE_CLASS_NONE = not set)
Definition sensor.h:161
bool get_force_update() const
Get whether force update mode is enabled.
Definition sensor.h:70
struct esphome::sensor::Sensor::SensorFlags sensor_flags_
void set_filters(std::initializer_list< Filter * > filters)
Clear the filters and replace them by filters.
Definition sensor.cpp:110
void add_filters(std::initializer_list< Filter * > filters)
Add a list of vectors to the back of the filter chain.
Definition sensor.cpp:105
uint16_t type
constexpr uint8_t STATE_CLASS_LAST
Definition sensor.h:39
StateClass
Sensor state classes.
Definition sensor.h:32
@ STATE_CLASS_TOTAL
Definition sensor.h:36
@ STATE_CLASS_TOTAL_INCREASING
Definition sensor.h:35
@ STATE_CLASS_MEASUREMENT_ANGLE
Definition sensor.h:37
@ STATE_CLASS_MEASUREMENT
Definition sensor.h:34
void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *obj)
Definition sensor.cpp:12
const LogString * state_class_to_string(StateClass state_class)
Definition sensor.cpp:38
const char * tag
Definition log.h:74