ESPHome 2025.10.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
15void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *obj);
16
17#define LOG_SENSOR(prefix, type, obj) log_sensor(TAG, prefix, LOG_STR_LITERAL(type), obj)
18
19#define SUB_SENSOR(name) \
20 protected: \
21 sensor::Sensor *name##_sensor_{nullptr}; \
22\
23 public: \
24 void set_##name##_sensor(sensor::Sensor *sensor) { this->name##_sensor_ = sensor; }
25
35
36const LogString *state_class_to_string(StateClass state_class);
37
43 public:
44 explicit Sensor();
45
47 int8_t get_accuracy_decimals();
49 void set_accuracy_decimals(int8_t accuracy_decimals);
50
54 void set_state_class(StateClass state_class);
55
65 void set_force_update(bool force_update) { sensor_flags_.force_update = force_update; }
66
68 void add_filter(Filter *filter);
69
80 void add_filters(const std::vector<Filter *> &filters);
81
83 void set_filters(const std::vector<Filter *> &filters);
84
86 void clear_filters();
87
89 float get_state() const;
91 float get_raw_state() const;
92
100 void publish_state(float state);
101
102 // ========== INTERNAL METHODS ==========
103 // (In most use cases you won't need these)
105 void add_on_state_callback(std::function<void(float)> &&callback);
107 void add_on_raw_state_callback(std::function<void(float)> &&callback);
108
116 float state;
117
123
125
126 protected:
127 std::unique_ptr<CallbackManager<void(float)>> raw_callback_;
129
131
132 // Group small members together to avoid padding
135
136 // Bit-packed flags for sensor-specific settings
137 struct SensorFlags {
140 uint8_t force_update : 1;
141 uint8_t reserved : 5; // Reserved for future use
143};
144
145} // namespace sensor
146} // namespace esphome
Apply a filter to sensor values such as moving average.
Definition filter.h:20
Base-class for all sensors.
Definition sensor.h:42
void set_state_class(StateClass state_class)
Manually set the state class.
Definition sensor.cpp:63
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition sensor.cpp:96
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:73
void internal_send_state_to_frontend(float state)
Definition sensor.cpp:128
float get_raw_state() const
Getter-syntax for .raw_state.
Definition sensor.cpp:126
void set_force_update(bool force_update)
Set force update mode.
Definition sensor.h:65
CallbackManager< void(float)> callback_
Storage for filtered state callbacks.
Definition sensor.h:128
void add_filters(const std::vector< Filter * > &filters)
Add a list of vectors to the back of the filter chain.
Definition sensor.cpp:110
float get_state() const
Getter-syntax for .state.
Definition sensor.cpp:125
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals.
Definition sensor.cpp:58
void set_filters(const std::vector< Filter * > &filters)
Clear the filters and replace them by filters.
Definition sensor.cpp:115
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:88
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:116
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition sensor.cpp:67
Filter * filter_list_
Store all active filters.
Definition sensor.h:130
float raw_state
This member variable stores the current raw state of the sensor, without any filters applied.
Definition sensor.h:122
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition sensor.cpp:53
void clear_filters()
Clear the entire filter chain.
Definition sensor.cpp:119
int8_t accuracy_decimals_
Accuracy in decimals (-1 = not set)
Definition sensor.h:133
StateClass state_class_
State class (STATE_CLASS_NONE = not set)
Definition sensor.h:134
bool get_force_update() const
Get whether force update mode is enabled.
Definition sensor.h:63
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:127
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:89
uint16_t type
StateClass
Sensor state classes.
Definition sensor.h:29
@ STATE_CLASS_TOTAL
Definition sensor.h:33
@ STATE_CLASS_TOTAL_INCREASING
Definition sensor.h:32
@ STATE_CLASS_MEASUREMENT
Definition sensor.h:31
void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *obj)
Definition sensor.cpp:10
const LogString * state_class_to_string(StateClass state_class)
Definition sensor.cpp:37
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7