ESPHome 2026.11.0b6
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 <cmath>
12#include <initializer_list>
13#include <memory>
14
15namespace esphome::sensor {
16
17class Sensor;
18
19void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *obj);
20
21#define LOG_SENSOR(prefix, type, obj) log_sensor(TAG, prefix, LOG_STR_LITERAL(type), obj)
22
23#define SUB_SENSOR(name) \
24 protected: \
25 sensor::Sensor *name##_sensor_{nullptr}; \
26\
27 public: \
28 void set_##name##_sensor(sensor::Sensor *sensor) { this->name##_sensor_ = sensor; }
29
40constexpr uint8_t STATE_CLASS_LAST = static_cast<uint8_t>(STATE_CLASS_MEASUREMENT_ANGLE);
41
42const LogString *state_class_to_string(StateClass state_class);
43
48class Sensor : public EntityBase {
49 public:
50 explicit Sensor();
51
53 int8_t get_accuracy_decimals();
55 void set_accuracy_decimals(int8_t accuracy_decimals);
58
62 void set_state_class(StateClass state_class);
63
73 void set_force_update(bool force_update) { sensor_flags_.force_update = force_update; }
74
75#ifdef USE_SENSOR_FILTER
77 void add_filter(Filter *filter);
78
89 void add_filters(std::initializer_list<Filter *> filters);
90
92 void set_filters(std::initializer_list<Filter *> filters);
93
95 void clear_filters();
96#endif
97
99 float get_state() const { return this->state; }
101 float get_raw_state() const {
102#ifdef USE_SENSOR_FILTER
103 return this->raw_state_;
104#else
105 return this->state; // No filters compiled in, raw == filtered
106#endif
107 }
108
117 void publish_state(float state);
118
119 // ========== INTERNAL METHODS ==========
120 // (In most use cases you won't need these)
122 template<typename F> void add_on_state_callback(F &&callback) { this->callback_.add(std::forward<F>(callback)); }
126 template<typename F> void add_on_raw_state_callback(F &&callback) {
127#ifdef USE_SENSOR_FILTER
128 this->raw_callback_.add(std::forward<F>(callback));
129#else
130 this->callback_.add(std::forward<F>(callback));
131#endif
132 }
133
141 float state;
142
144
145 protected:
146#ifdef USE_SENSOR_FILTER
147 float raw_state_{NAN};
149#endif
151
152#ifdef USE_SENSOR_FILTER
154#endif
155
156 // Group small members together to avoid padding
159
160 // Bit-packed flags for sensor-specific settings
161 struct SensorFlags {
164 uint8_t force_update : 1;
165 uint8_t reserved : 5; // Reserved for future use
167};
168
175inline float source_value(const Sensor *sensor, float min, float max) {
176 if (sensor == nullptr)
177 return NAN;
178 const float value = sensor->state;
179 // isnan first: callers test the result with isnan, and the compiler can reuse this check
180 if (std::isnan(value) || value < min || value > max)
181 return NAN;
182 return value;
183}
184
185} // 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:48
void set_state_class(StateClass state_class)
Manually set the state class.
Definition sensor.cpp:55
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition sensor.cpp:85
void add_on_state_callback(F &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.h:122
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
LazyCallbackManager< void(float)> callback_
Storage for filtered state callbacks.
Definition sensor.h:150
void internal_send_state_to_frontend(float state)
Definition sensor.cpp:116
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:126
float get_raw_state() const
Get the last state received by publish_state(), before any filters were applied.
Definition sensor.h:101
void set_force_update(bool force_update)
Set force update mode.
Definition sensor.h:73
float get_state() const
Getter-syntax for .state.
Definition sensor.h:99
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals.
Definition sensor.cpp:50
bool has_accuracy_decimals() const
Check if the accuracy in decimals has been manually set.
Definition sensor.h:57
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:141
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition sensor.cpp:59
float raw_state_
The last state passed to publish_state(), before filters.
Definition sensor.h:147
Filter * filter_list_
Store all active filters.
Definition sensor.h:153
LazyCallbackManager< void(float)> raw_callback_
Storage for raw state callbacks.
Definition sensor.h:148
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition sensor.cpp:45
void clear_filters()
Clear the entire filter chain.
Definition sensor.cpp:108
int8_t accuracy_decimals_
Accuracy in decimals (-1 = not set)
Definition sensor.h:157
StateClass state_class_
State class (STATE_CLASS_NONE = not set)
Definition sensor.h:158
bool get_force_update() const
Get whether force update mode is enabled.
Definition sensor.h:71
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:104
void add_filters(std::initializer_list< Filter * > filters)
Add a list of vectors to the back of the filter chain.
Definition sensor.cpp:99
uint8_t type
constexpr uint8_t STATE_CLASS_LAST
Definition sensor.h:40
StateClass
Sensor state classes.
Definition sensor.h:33
@ STATE_CLASS_TOTAL
Definition sensor.h:37
@ STATE_CLASS_TOTAL_INCREASING
Definition sensor.h:36
@ STATE_CLASS_MEASUREMENT_ANGLE
Definition sensor.h:38
@ STATE_CLASS_MEASUREMENT
Definition sensor.h:35
float source_value(const Sensor *sensor, float min, float max)
Read another sensor as an input, such as the temperature a gas sensor compensates for.
Definition sensor.h:175
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
uint32_t value
Definition hal.cpp:41