ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
filter.cpp
Go to the documentation of this file.
2#ifdef USE_BINARY_SENSOR_FILTER
3
4#include "filter.h"
5
6#include "binary_sensor.h"
8
10
11ESPHOME_LOG_TAG(TAG, "sensor.filter");
12
14 if (this->next_ == nullptr) {
15 this->parent_->send_state_internal(value);
16 } else {
17 this->next_->input(value);
18 }
19}
20
22 this->dedup_.next_unknown();
23 App.scheduler.cancel_timeout(this);
24}
25
26void Filter::input(bool value) {
27 if (!this->dedup_.next(value))
28 return;
29 auto b = this->new_value(value);
30 if (b.has_value()) {
31 this->output(*b);
32 }
33}
34
36 App.scheduler.set_timeout(this, this->timeout_delay_.value(), [this]() { this->parent_->invalidate_state(); });
37 // Every input re-arms the timeout, including repeats, so this filter must not de-dup
38 this->output(value);
39}
40
42 if (value) {
43 App.scheduler.set_timeout(this, this->on_delay_.value(), [this]() { this->output(true); });
44 } else {
45 App.scheduler.set_timeout(this, this->off_delay_.value(), [this]() { this->output(false); });
46 }
47 return {};
48}
49
50optional<bool> DelayedOnFilter::new_value(bool value) {
51 if (value) {
52 App.scheduler.set_timeout(this, this->delay_.value(), [this]() { this->output(true); });
53 return {};
54 } else {
55 App.scheduler.cancel_timeout(this);
56 return false;
57 }
58}
59
60optional<bool> DelayedOffFilter::new_value(bool value) {
61 if (!value) {
62 App.scheduler.set_timeout(this, this->delay_.value(), [this]() { this->output(false); });
63 return {};
64 } else {
65 App.scheduler.cancel_timeout(this);
66 return true;
67 }
68}
69
70optional<bool> InvertFilter::new_value(bool value) { return !value; }
71
72// AutorepeatFilterBase
73// Two independent timers per instance, keyed off two stable addresses inside
74// the filter: `this` for the timing-step timer, `&active_timing_` for the
75// on/off timer. Both are unique per instance and don't collide with anything
76// else, so the self-keyed scheduler API is sufficient.
78 if (value) {
79 if (this->active_timing_ != 0)
80 return {};
81 this->next_timing_();
82 return true;
83 } else {
84 App.scheduler.cancel_timeout(this);
85 App.scheduler.cancel_timeout(&this->active_timing_);
86 this->active_timing_ = 0;
87 return false;
88 }
89}
90
92 this->dedup_.next_unknown();
93 // The release cancels both timers; Filter::reset() would leave the on/off timer running
95}
96
99 App.scheduler.set_timeout(this, this->timings_[this->active_timing_].delay, [this]() { this->next_timing_(); });
100 }
101 if (this->active_timing_ <= this->timings_count_) {
102 this->active_timing_++;
103 }
104 if (this->active_timing_ == 2)
105 this->next_value_(false);
106}
107
109 const AutorepeatFilterTiming &timing = this->timings_[this->active_timing_ - 2];
110 this->output(val);
111 // A callback of that output may have stopped the filter
112 if (this->active_timing_ < 2)
113 return;
114 App.scheduler.set_timeout(&this->active_timing_, val ? timing.time_on : timing.time_off,
115 [this, val]() { this->next_value_(!val); });
116}
117
118LambdaFilter::LambdaFilter(std::function<optional<bool>(bool)> f) : f_(std::move(f)) {}
119
120optional<bool> LambdaFilter::new_value(bool value) { return this->f_(value); }
121
124 this->steady_ = true;
125}
126
127optional<bool> SettleFilter::new_value(bool value) {
128 if (!this->steady_) {
129 App.scheduler.set_timeout(this, this->delay_.value(), [this, value]() {
130 this->steady_ = true;
131 this->output(value);
132 });
133 return {};
134 } else {
135 this->steady_ = false;
136 App.scheduler.set_timeout(this, this->delay_.value(), [this]() { this->steady_ = true; });
137 return value;
138 }
139}
140
141} // namespace esphome::binary_sensor
142
143#endif // USE_BINARY_SENSOR_FILTER
bool next(T value)
Feeds the next item in the series to the deduplicator and returns false if this is a duplicate.
Definition helpers.h:2135
bool next_unknown()
Returns true if the deduplicator's value was previously known.
Definition helpers.h:2145
T value(X... x) const
Definition automation.h:69
optional< bool > new_value(bool value) override
Definition filter.cpp:77
const AutorepeatFilterTiming * timings_
Definition filter.h:110
void send_state_internal(bool new_state)
optional< bool > new_value(bool value) override
Definition filter.cpp:60
TemplatableFn< uint32_t > delay_
Definition filter.h:80
optional< bool > new_value(bool value) override
Definition filter.cpp:50
TemplatableFn< uint32_t > delay_
Definition filter.h:67
TemplatableFn< uint32_t > on_delay_
Definition filter.h:53
TemplatableFn< uint32_t > off_delay_
Definition filter.h:54
optional< bool > new_value(bool value) override
Definition filter.cpp:41
virtual void input(bool value)
Definition filter.cpp:26
virtual optional< bool > new_value(bool value)=0
Deduplicator< bool > dedup_
Definition filter.h:32
void output(bool value)
Definition filter.cpp:13
virtual void reset()
Forget the last value and drop any pending scheduled output; called when the sensor's state is invali...
Definition filter.cpp:21
optional< bool > new_value(bool value) override
Definition filter.cpp:70
LambdaFilter(std::function< optional< bool >(bool)> f)
Definition filter.cpp:118
std::function< optional< bool >(bool)> f_
Definition filter.h:136
optional< bool > new_value(bool value) override
Definition filter.cpp:120
TemplatableFn< uint32_t > delay_
Definition filter.h:166
optional< bool > new_value(bool value) override
Definition filter.cpp:127
TemplatableFn< uint32_t > timeout_delay_
Definition filter.h:42
void input(bool value) override
Definition filter.cpp:35
mopeka_std_values val[3]
ESPHOME_LOG_TAG(TAG, "binary_sensor.automation")
uint32_t value
Definition hal.cpp:41
void HOT delay(uint32_t ms)
Definition hal.cpp:85
Application App
Global storage of Application pointer - only one Application can exist.
STL namespace.