ESPHome 2025.5.0
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
6
7namespace esphome {
8namespace sensor {
9
10class SensorStateTrigger : public Trigger<float> {
11 public:
12 explicit SensorStateTrigger(Sensor *parent) {
13 parent->add_on_state_callback([this](float value) { this->trigger(value); });
14 }
15};
16
17class SensorRawStateTrigger : public Trigger<float> {
18 public:
19 explicit SensorRawStateTrigger(Sensor *parent) {
20 parent->add_on_raw_state_callback([this](float value) { this->trigger(value); });
21 }
22};
23
24template<typename... Ts> class SensorPublishAction : public Action<Ts...> {
25 public:
26 SensorPublishAction(Sensor *sensor) : sensor_(sensor) {}
28
29 void play(Ts... x) override { this->sensor_->publish_state(this->state_.value(x...)); }
30
31 protected:
33};
34
35class ValueRangeTrigger : public Trigger<float>, public Component {
36 public:
37 explicit ValueRangeTrigger(Sensor *parent) : parent_(parent) {}
38
39 template<typename V> void set_min(V min) { this->min_ = min; }
40 template<typename V> void set_max(V max) { this->max_ = max; }
41
42 void setup() override {
44 bool initial_state;
45 if (this->rtc_.load(&initial_state)) {
46 this->previous_in_range_ = initial_state;
47 }
48
49 this->parent_->add_on_state_callback([this](float state) { this->on_state_(state); });
50 }
51 float get_setup_priority() const override { return setup_priority::HARDWARE; }
52
53 protected:
54 void on_state_(float state) {
55 if (std::isnan(state))
56 return;
57
58 float local_min = this->min_.value(state);
59 float local_max = this->max_.value(state);
60
61 bool in_range;
62 if (std::isnan(local_min) && std::isnan(local_max)) {
63 in_range = this->previous_in_range_;
64 } else if (std::isnan(local_min)) {
65 in_range = state <= local_max;
66 } else if (std::isnan(local_max)) {
67 in_range = state >= local_min;
68 } else {
69 in_range = local_min <= state && state <= local_max;
70 }
71
72 if (in_range != this->previous_in_range_ && in_range) {
73 this->trigger(state);
74 }
75
76 this->previous_in_range_ = in_range;
77 this->rtc_.save(&in_range);
78 }
79
82 bool previous_in_range_{false};
85};
86
87template<typename... Ts> class SensorInRangeCondition : public Condition<Ts...> {
88 public:
90
91 void set_min(float min) { this->min_ = min; }
92 void set_max(float max) { this->max_ = max; }
93 bool check(Ts... x) override {
94 const float state = this->parent_->state;
95 if (std::isnan(this->min_)) {
96 return state <= this->max_;
97 } else if (std::isnan(this->max_)) {
98 return state >= this->min_;
99 } else {
100 return this->min_ <= state && state <= this->max_;
101 }
102 }
103
104 protected:
106 float min_{NAN};
107 float max_{NAN};
108};
109
110} // namespace sensor
111} // namespace esphome
virtual void play(Ts... x)=0
Base class for all automation conditions.
Definition automation.h:75
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_object_id_hash()
Base-class for all sensors.
Definition sensor.h:57
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
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:52
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:131
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:53
TEMPLATABLE_VALUE(float, state) void play(Ts... x) override
Definition automation.h:27
float get_setup_priority() const override
Definition automation.h:51
TemplatableValue< float, float > max_
Definition automation.h:84
TemplatableValue< float, float > min_
Definition automation.h:83
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:18
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
ESPPreferences * global_preferences
uint16_t x
Definition tt21100.cpp:5