ESPHome 2026.2.0
Loading...
Searching...
No Matches
sensor.h
Go to the documentation of this file.
1#pragma once
2
6#include "esphome/core/log.h"
8
9#include <initializer_list>
10#include <memory>
11
12namespace esphome::sensor {
13
14void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *obj);
15
16#define LOG_SENSOR(prefix, type, obj) log_sensor(TAG, prefix, LOG_STR_LITERAL(type), obj)
17
18#define SUB_SENSOR(name) \
19 protected: \
20 sensor::Sensor *name##_sensor_{nullptr}; \
21\
22 public: \
23 void set_##name##_sensor(sensor::Sensor *sensor) { this->name##_sensor_ = sensor; }
24
35constexpr uint8_t STATE_CLASS_LAST = static_cast<uint8_t>(STATE_CLASS_MEASUREMENT_ANGLE);
36
37const LogString *state_class_to_string(StateClass state_class);
38
44 public:
45 explicit Sensor();
46
48 int8_t get_accuracy_decimals();
50 void set_accuracy_decimals(int8_t accuracy_decimals);
51
55 void set_state_class(StateClass state_class);
56
66 void set_force_update(bool force_update) { sensor_flags_.force_update = force_update; }
67
69 void add_filter(Filter *filter);
70
81 void add_filters(std::initializer_list<Filter *> filters);
82
84 void set_filters(std::initializer_list<Filter *> filters);
85
87 void clear_filters();
88
90 float get_state() const;
92 float get_raw_state() const;
93
101 void publish_state(float state);
102
103 // ========== INTERNAL METHODS ==========
104 // (In most use cases you won't need these)
106 void add_on_state_callback(std::function<void(float)> &&callback);
108 void add_on_raw_state_callback(std::function<void(float)> &&callback);
109
117 float state;
118
124
126
127 protected:
130
132
133 // Group small members together to avoid padding
136
137 // Bit-packed flags for sensor-specific settings
138 struct SensorFlags {
141 uint8_t force_update : 1;
142 uint8_t reserved : 5; // Reserved for future use
144};
145
146} // namespace esphome::sensor
Apply a filter to sensor values such as moving average.
Definition filter.h:19
Base-class for all sensors.
Definition sensor.h:43
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:83
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:129
void internal_send_state_to_frontend(float state)
Definition sensor.cpp:115
float get_raw_state() const
Getter-syntax for .raw_state.
Definition sensor.cpp:113
void set_force_update(bool force_update)
Set force update mode.
Definition sensor.h:66
float get_state() const
Getter-syntax for .state.
Definition sensor.cpp:112
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals.
Definition sensor.cpp:50
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:78
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:117
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition sensor.cpp:59
Filter * filter_list_
Store all active filters.
Definition sensor.h:131
float raw_state
This member variable stores the current raw state of the sensor, without any filters applied.
Definition sensor.h:123
LazyCallbackManager< void(float)> raw_callback_
Storage for raw state callbacks.
Definition sensor.h:128
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:106
int8_t accuracy_decimals_
Accuracy in decimals (-1 = not set)
Definition sensor.h:134
StateClass state_class_
State class (STATE_CLASS_NONE = not set)
Definition sensor.h:135
bool get_force_update() const
Get whether force update mode is enabled.
Definition sensor.h:64
struct esphome::sensor::Sensor::SensorFlags sensor_flags_
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:79
void set_filters(std::initializer_list< Filter * > filters)
Clear the filters and replace them by filters.
Definition sensor.cpp:102
void add_filters(std::initializer_list< Filter * > filters)
Add a list of vectors to the back of the filter chain.
Definition sensor.cpp:97
uint16_t type
constexpr uint8_t STATE_CLASS_LAST
Definition sensor.h:35
StateClass
Sensor state classes.
Definition sensor.h:28
@ STATE_CLASS_TOTAL
Definition sensor.h:32
@ STATE_CLASS_TOTAL_INCREASING
Definition sensor.h:31
@ STATE_CLASS_MEASUREMENT_ANGLE
Definition sensor.h:33
@ STATE_CLASS_MEASUREMENT
Definition sensor.h:30
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