ESPHome 2025.9.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"
8
9#include <vector>
10#include <memory>
11
12namespace esphome {
13namespace sensor {
14
15#define LOG_SENSOR(prefix, type, obj) \
16 if ((obj) != nullptr) { \
17 ESP_LOGCONFIG(TAG, \
18 "%s%s '%s'\n" \
19 "%s State Class: '%s'\n" \
20 "%s Unit of Measurement: '%s'\n" \
21 "%s Accuracy Decimals: %d", \
22 prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str(), prefix, \
23 state_class_to_string((obj)->get_state_class()).c_str(), prefix, \
24 (obj)->get_unit_of_measurement().c_str(), prefix, (obj)->get_accuracy_decimals()); \
25 if (!(obj)->get_device_class().empty()) { \
26 ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
27 } \
28 if (!(obj)->get_icon().empty()) { \
29 ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
30 } \
31 if ((obj)->get_force_update()) { \
32 ESP_LOGV(TAG, "%s Force Update: YES", prefix); \
33 } \
34 }
35
36#define SUB_SENSOR(name) \
37 protected: \
38 sensor::Sensor *name##_sensor_{nullptr}; \
39\
40 public: \
41 void set_##name##_sensor(sensor::Sensor *sensor) { this->name##_sensor_ = sensor; }
42
52
53std::string state_class_to_string(StateClass state_class);
54
60 public:
61 explicit Sensor();
62
64 int8_t get_accuracy_decimals();
66 void set_accuracy_decimals(int8_t accuracy_decimals);
67
71 void set_state_class(StateClass state_class);
72
82 void set_force_update(bool force_update) { sensor_flags_.force_update = force_update; }
83
85 void add_filter(Filter *filter);
86
97 void add_filters(const std::vector<Filter *> &filters);
98
100 void set_filters(const std::vector<Filter *> &filters);
101
103 void clear_filters();
104
106 float get_state() const;
108 float get_raw_state() const;
109
117 void publish_state(float state);
118
119 // ========== INTERNAL METHODS ==========
120 // (In most use cases you won't need these)
122 void add_on_state_callback(std::function<void(float)> &&callback);
124 void add_on_raw_state_callback(std::function<void(float)> &&callback);
125
133 float state;
134
140
142
143 protected:
144 std::unique_ptr<CallbackManager<void(float)>> raw_callback_;
146
148
149 // Group small members together to avoid padding
152
153 // Bit-packed flags for sensor-specific settings
154 struct SensorFlags {
157 uint8_t force_update : 1;
158 uint8_t reserved : 5; // Reserved for future use
160};
161
162} // namespace sensor
163} // namespace esphome
Apply a filter to sensor values such as moving average.
Definition filter.h:20
Base-class for all sensors.
Definition sensor.h:59
void set_state_class(StateClass state_class)
Manually set the state class.
Definition sensor.cpp:35
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition sensor.cpp:68
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
void internal_send_state_to_frontend(float state)
Definition sensor.cpp:100
float get_raw_state() const
Getter-syntax for .raw_state.
Definition sensor.cpp:98
void set_force_update(bool force_update)
Set force update mode.
Definition sensor.h:82
CallbackManager< void(float)> callback_
Storage for filtered state callbacks.
Definition sensor.h:145
void add_filters(const std::vector< Filter * > &filters)
Add a list of vectors to the back of the filter chain.
Definition sensor.cpp:82
float get_state() const
Getter-syntax for .state.
Definition sensor.cpp:97
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals.
Definition sensor.cpp:30
void set_filters(const std::vector< Filter * > &filters)
Clear the filters and replace them by filters.
Definition sensor.cpp:87
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:60
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:133
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition sensor.cpp:39
Filter * filter_list_
Store all active filters.
Definition sensor.h:147
float raw_state
This member variable stores the current raw state of the sensor, without any filters applied.
Definition sensor.h:139
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition sensor.cpp:25
void clear_filters()
Clear the entire filter chain.
Definition sensor.cpp:91
int8_t accuracy_decimals_
Accuracy in decimals (-1 = not set)
Definition sensor.h:150
StateClass state_class_
State class (STATE_CLASS_NONE = not set)
Definition sensor.h:151
bool get_force_update() const
Get whether force update mode is enabled.
Definition sensor.h:80
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:144
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:61
std::string state_class_to_string(StateClass state_class)
Definition sensor.cpp:9
StateClass
Sensor state classes.
Definition sensor.h:46
@ STATE_CLASS_TOTAL
Definition sensor.h:50
@ STATE_CLASS_TOTAL_INCREASING
Definition sensor.h:49
@ STATE_CLASS_MEASUREMENT
Definition sensor.h:48
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7