ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
sensor_with_dedup.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <cmath>
7
8namespace esphome::sensor {
9
11#define SUB_SENSOR_WITH_DEDUP(name, dedup_type) \
12 protected: \
13 sensor::SensorWithDedup<dedup_type> name##_sensor_{}; \
14\
15 public: \
16 void set_##name##_sensor(sensor::Sensor *sensor) { this->name##_sensor_.set_sensor(sensor); }
17
18#define LOG_SENSOR_WITH_DEDUP(prefix, type, s) LOG_SENSOR(prefix, type, (s).get_sensor())
19
22template<typename T> class SensorWithDedup {
23 public:
24 void set_sensor(Sensor *sens) {
25 this->sens_ = sens;
26 this->dedup_ = {};
27 }
28
30 if (this->sens_ != nullptr && this->dedup_.next(state)) {
31 this->sens_->publish_state(static_cast<float>(state));
32 }
33 }
34
36 if (this->sens_ != nullptr && this->dedup_.next_unknown()) {
37 this->sens_->publish_state(NAN);
38 }
39 }
40
41 bool has_sensor() const { return this->sens_ != nullptr; }
42 Sensor *get_sensor() const { return this->sens_; }
43
44 protected:
45 Sensor *sens_{nullptr};
47};
48
49} // namespace esphome::sensor
Helper class to deduplicate items in a series of values.
Definition helpers.h:2045
Base-class for all sensors.
Definition sensor.h:47
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
A sensor pointer that may be null plus the last value it published, stored inline.
bool state
Definition fan.h:2