ESPHome 2026.1.4
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
6
7namespace esphome::sensor {
8
9class SensorStateTrigger : public Trigger<float> {
10 public:
11 explicit SensorStateTrigger(Sensor *parent) {
12 parent->add_on_state_callback([this](float value) { this->trigger(value); });
13 }
14};
15
16class SensorRawStateTrigger : public Trigger<float> {
17 public:
18 explicit SensorRawStateTrigger(Sensor *parent) {
19 parent->add_on_raw_state_callback([this](float value) { this->trigger(value); });
20 }
21};
22
23template<typename... Ts> class SensorPublishAction : public Action<Ts...> {
24 public:
25 SensorPublishAction(Sensor *sensor) : sensor_(sensor) {}
27
28 void play(const Ts &...x) override { this->sensor_->publish_state(this->state_.value(x...)); }
29
30 protected:
32};
33
34class ValueRangeTrigger : public Trigger<float>, public Component {
35 public:
36 explicit ValueRangeTrigger(Sensor *parent) : parent_(parent) {}
37
38 template<typename V> void set_min(V min) { this->min_ = min; }
39 template<typename V> void set_max(V max) { this->max_ = max; }
40
41 void setup() override {
43 bool initial_state;
44 if (this->rtc_.load(&initial_state)) {
45 this->previous_in_range_ = initial_state;
46 }
47
48 this->parent_->add_on_state_callback([this](float state) { this->on_state_(state); });
49 }
50 float get_setup_priority() const override { return setup_priority::HARDWARE; }
51
52 protected:
53 void on_state_(float state) {
54 if (std::isnan(state))
55 return;
56
57 float local_min = this->min_.value(state);
58 float local_max = this->max_.value(state);
59
60 bool in_range;
61 if (std::isnan(local_min) && std::isnan(local_max)) {
62 in_range = this->previous_in_range_;
63 } else if (std::isnan(local_min)) {
64 in_range = state <= local_max;
65 } else if (std::isnan(local_max)) {
66 in_range = state >= local_min;
67 } else {
68 in_range = local_min <= state && state <= local_max;
69 }
70
71 if (in_range != this->previous_in_range_ && in_range) {
72 this->trigger(state);
73 }
74
75 this->previous_in_range_ = in_range;
76 this->rtc_.save(&in_range);
77 }
78
81 bool previous_in_range_{false};
84};
85
86template<typename... Ts> class SensorInRangeCondition : public Condition<Ts...> {
87 public:
89
90 void set_min(float min) { this->min_ = min; }
91 void set_max(float max) { this->max_ = max; }
92 bool check(const Ts &...x) override {
93 const float state = this->parent_->state;
94 if (std::isnan(this->min_)) {
95 return state <= this->max_;
96 } else if (std::isnan(this->max_)) {
97 return state >= this->min_;
98 } else {
99 return this->min_ <= state && state <= this->max_;
100 }
101 }
102
103 protected:
105 float min_{NAN};
106 float max_{NAN};
107};
108
109} // namespace esphome::sensor
virtual void play(const Ts &...x)=0
Base class for all automation conditions.
Definition automation.h:217
bool save(const T *src)
Definition preferences.h:21
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
uint32_t get_preference_hash()
Get a unique hash for storing preferences/settings for this entity.
T value(X... x) const
Definition automation.h:148
void trigger(const Ts &...x)
Definition automation.h:238
Base-class for all sensors.
Definition sensor.h:42
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:76
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:89
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:116
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:90
bool check(const Ts &...x) override
Definition automation.h:92
TEMPLATABLE_VALUE(float, state) void play(const Ts &...x) override
Definition automation.h:26
float get_setup_priority() const override
Definition automation.h:50
TemplatableValue< float, float > max_
Definition automation.h:83
TemplatableValue< float, float > min_
Definition automation.h:82
bool state
Definition fan.h:0
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:80
ESPPreferences * global_preferences
uint16_t x
Definition tt21100.cpp:5