ESPHome  2024.9.2
automation.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace sensor {
9 
10 class 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 
17 class 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 
24 template<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 
35 class 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 {
43  this->rtc_ = global_preferences->make_preference<bool>(this->parent_->get_object_id_hash());
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 
87 template<typename... Ts> class SensorInRangeCondition : public Condition<Ts...> {
88  public:
89  SensorInRangeCondition(Sensor *parent) : parent_(parent) {}
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
TEMPLATABLE_VALUE(float, state) void play(Ts... x) override
Definition: automation.h:27
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
uint16_t x
Definition: tt21100.cpp:17
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
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
float get_setup_priority() const override
Definition: automation.h:51
Base class for all automation conditions.
Definition: automation.h:74
ESPPreferences * global_preferences
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base-class for all sensors.
Definition: sensor.h:57
esphome::sensor::Sensor * sensor
Definition: statsd.h:37
bool state
Definition: fan.h:34