ESPHome 2025.6.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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)->unique_id().empty()) { \
32 ESP_LOGV(TAG, "%s Unique ID: '%s'", prefix, (obj)->unique_id().c_str()); \
33 } \
34 if ((obj)->get_force_update()) { \
35 ESP_LOGV(TAG, "%s Force Update: YES", prefix); \
36 } \
37 }
38
39#define SUB_SENSOR(name) \
40 protected: \
41 sensor::Sensor *name##_sensor_{nullptr}; \
42\
43 public: \
44 void set_##name##_sensor(sensor::Sensor *sensor) { this->name##_sensor_ = sensor; }
45
55
56std::string state_class_to_string(StateClass state_class);
57
63 public:
64 explicit Sensor();
65
67 int8_t get_accuracy_decimals();
69 void set_accuracy_decimals(int8_t accuracy_decimals);
70
74 void set_state_class(StateClass state_class);
75
83 bool get_force_update() const { return force_update_; }
85 void set_force_update(bool force_update) { force_update_ = force_update; }
86
88 void add_filter(Filter *filter);
89
100 void add_filters(const std::vector<Filter *> &filters);
101
103 void set_filters(const std::vector<Filter *> &filters);
104
106 void clear_filters();
107
109 float get_state() const;
111 float get_raw_state() const;
112
120 void publish_state(float state);
121
122 // ========== INTERNAL METHODS ==========
123 // (In most use cases you won't need these)
125 void add_on_state_callback(std::function<void(float)> &&callback);
127 void add_on_raw_state_callback(std::function<void(float)> &&callback);
128
136 float state;
137
143
148 virtual std::string unique_id();
149
151
152 protected:
153 std::unique_ptr<CallbackManager<void(float)>> raw_callback_;
155
157
160 bool force_update_{false};
161};
162
163} // namespace sensor
164} // namespace esphome
Apply a filter to sensor values such as moving average.
Definition filter.h:20
Base-class for all sensors.
Definition sensor.h:62
void set_state_class(StateClass state_class)
Manually set the state class.
Definition sensor.cpp:32
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition sensor.cpp:62
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
void internal_send_state_to_frontend(float state)
Definition sensor.cpp:95
optional< int8_t > accuracy_decimals_
Accuracy in decimals override.
Definition sensor.h:158
float get_raw_state() const
Getter-syntax for .raw_state.
Definition sensor.cpp:92
void set_force_update(bool force_update)
Set force update mode.
Definition sensor.h:85
CallbackManager< void(float)> callback_
Storage for filtered state callbacks.
Definition sensor.h:154
void add_filters(const std::vector< Filter * > &filters)
Add a list of vectors to the back of the filter chain.
Definition sensor.cpp:76
float get_state() const
Getter-syntax for .state.
Definition sensor.cpp:91
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals.
Definition sensor.cpp:30
bool force_update_
Force update mode.
Definition sensor.h:160
void set_filters(const std::vector< Filter * > &filters)
Clear the filters and replace them by filters.
Definition sensor.cpp:81
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:54
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:136
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition sensor.cpp:33
Filter * filter_list_
Store all active filters.
Definition sensor.h:156
float raw_state
This member variable stores the current raw state of the sensor, without any filters applied.
Definition sensor.h:142
optional< StateClass > state_class_
State class override.
Definition sensor.h:159
virtual std::string unique_id()
Override this method to set the unique ID of this sensor.
Definition sensor.cpp:93
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:85
bool get_force_update() const
Get whether force update mode is enabled.
Definition sensor.h:83
std::unique_ptr< CallbackManager< void(float)> > raw_callback_
Storage for raw state callbacks (lazy allocated).
Definition sensor.h:153
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:55
std::string state_class_to_string(StateClass state_class)
Definition sensor.cpp:9
StateClass
Sensor state classes.
Definition sensor.h:49
@ STATE_CLASS_TOTAL
Definition sensor.h:53
@ STATE_CLASS_TOTAL_INCREASING
Definition sensor.h:52
@ STATE_CLASS_MEASUREMENT
Definition sensor.h:51
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7