ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
6
7namespace esphome::sensor {
8
9class ValueRangeTrigger final : public Trigger<float>, public Component {
10 public:
11 explicit ValueRangeTrigger(Sensor *parent) : parent_(parent) {}
12
13 template<typename V> void set_min(V min) { this->min_ = min; }
14 template<typename V> void set_max(V max) { this->max_ = max; }
15
16 void setup() override {
17 this->rtc_ = this->parent_->make_entity_preference<bool>();
18 bool initial_state;
19 if (this->rtc_.load(&initial_state)) {
20 this->previous_in_range_ = initial_state;
21 }
22
23 this->parent_->add_on_state_callback([this](float state) { this->on_state_(state); });
24 }
25 float get_setup_priority() const override { return setup_priority::HARDWARE; }
26
27 protected:
28 void on_state_(float state) {
29 if (std::isnan(state))
30 return;
31
32 float local_min = this->min_.value(state);
33 float local_max = this->max_.value(state);
34
35 bool in_range;
36 if (std::isnan(local_min) && std::isnan(local_max)) {
37 in_range = this->previous_in_range_;
38 } else if (std::isnan(local_min)) {
39 in_range = state <= local_max;
40 } else if (std::isnan(local_max)) {
41 in_range = state >= local_min;
42 } else {
43 in_range = local_min <= state && state <= local_max;
44 }
45
46 if (in_range != this->previous_in_range_ && in_range) {
47 this->trigger(state);
48 }
49
50 this->previous_in_range_ = in_range;
51 this->rtc_.save(&in_range);
52 }
53
56 bool previous_in_range_{false};
57 TemplatableFn<float, float> min_{[](float) -> float { return NAN; }};
58 TemplatableFn<float, float> max_{[](float) -> float { return NAN; }};
59};
60
61template<typename... Ts> class SensorInRangeCondition final : public Condition<Ts...> {
62 public:
64
65 void set_min(float min) { this->min_ = min; }
66 void set_max(float max) { this->max_ = max; }
67 bool check(const Ts &...x) override {
68 const float state = this->parent_->state;
69 if (std::isnan(this->min_)) {
70 return state <= this->max_;
71 } else if (std::isnan(this->max_)) {
72 return state >= this->min_;
73 } else {
74 return this->min_ <= state && state <= this->max_;
75 }
76 }
77
78 protected:
80 float min_{NAN};
81 float max_{NAN};
82};
83
84} // namespace esphome::sensor
Base class for all automation conditions.
Definition automation.h:533
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
Function-pointer-only templatable storage (4 bytes on 32-bit).
Definition automation.h:20
T value(X... x) const
Definition automation.h:68
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:556
Base-class for all sensors.
Definition sensor.h:47
void add_on_state_callback(F &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.h:121
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:140
bool check(const Ts &...x) override
Definition automation.h:67
float get_setup_priority() const override
Definition automation.h:25
TemplatableFn< float, float > max_
Definition automation.h:58
TemplatableFn< float, float > min_
Definition automation.h:57
bool state
Definition fan.h:2
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
uint16_t x
Definition tt21100.cpp:5