ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
automation.cpp
Go to the documentation of this file.
1#include "automation.h"
2#include "esphome/core/log.h"
3
4namespace esphome::number {
5
6static const char *const TAG = "number.automation";
7
8union convert {
9 float from;
10 uint32_t to;
11};
12
14 float local_min = this->min_.value(0.0);
15 float local_max = this->max_.value(0.0);
16 convert hash = {.from = (local_max - local_min)};
17 this->rtc_ = this->parent_->make_entity_preference<bool>(hash.to);
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}
26
28 if (std::isnan(state))
29 return;
30
31 float local_min = this->min_.value(state);
32 float local_max = this->max_.value(state);
33
34 bool in_range;
35 if (std::isnan(local_min) && std::isnan(local_max)) {
36 in_range = this->previous_in_range_;
37 } else if (std::isnan(local_min)) {
38 in_range = state <= local_max;
39 } else if (std::isnan(local_max)) {
40 in_range = state >= local_min;
41 } else {
42 in_range = local_min <= state && state <= local_max;
43 }
44
45 if (in_range != this->previous_in_range_ && in_range) {
46 this->trigger(state);
47 }
48
49 this->previous_in_range_ = in_range;
50 this->rtc_.save(&in_range);
51}
52
53} // namespace esphome::number
bool save(const T *src)
Definition preferences.h:21
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
T value(X... x) const
Definition automation.h:160
void trigger(const Ts &...x)
Definition automation.h:325
void add_on_state_callback(std::function< void(float)> &&callback)
Definition number.cpp:32
TemplatableValue< float, float > min_
Definition automation.h:66
float get_setup_priority() const override
TemplatableValue< float, float > max_
Definition automation.h:67
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:29