ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
duty_time_sensor.cpp
Go to the documentation of this file.
1#include "duty_time_sensor.h"
2#include "esphome/core/hal.h"
3
5
6static const char *const TAG = "duty_time_sensor";
7
8#ifdef USE_BINARY_SENSOR
10 sensor->add_on_state_callback([this](bool state) { this->process_state_(state); });
11}
12#endif
13
15 if (!this->last_state_)
16 this->process_state_(true);
17}
18
20 if (this->last_state_)
21 this->process_state_(false);
22}
23
25 if (this->last_state_)
26 this->process_state_(true);
27}
28
30 if (this->func_ == nullptr)
31 return;
32
33 const bool state = this->func_();
34
35 if (state != this->last_state_)
36 this->process_state_(state);
37}
38
40 uint32_t seconds = 0;
41
42 if (this->restore_) {
44 this->pref_.load(&seconds);
45 }
46
47 this->set_value_(seconds);
48}
49
51 this->last_time_ = 0;
52 if (this->last_state_)
53 this->last_time_ = millis(); // last time with 0 ms correction
54 this->publish_and_save_(sec, 0);
55}
56
58 const uint32_t now = millis();
59
60 if (this->last_state_) {
61 // update or falling edge
62 const uint32_t tm = now - this->last_time_;
63 const uint32_t ms = tm % 1000;
64
65 this->publish_and_save_(this->total_sec_ + tm / 1000, ms);
66 this->last_time_ = now - ms; // store time with ms correction
67
68 if (!state) {
69 // falling edge
70 this->last_time_ = ms; // temporary store ms correction only
71 this->last_state_ = false;
72
73 if (this->last_duty_time_sensor_ != nullptr) {
74 const uint32_t turn_on_ms = now - this->edge_time_;
75 this->last_duty_time_sensor_->publish_state(turn_on_ms * 1e-3f);
76 }
77 }
78
79 } else if (state) {
80 // rising edge
81 this->last_time_ = now - this->last_time_; // store time with ms correction
82 this->edge_time_ = now; // store turn-on start time
83 this->last_state_ = true;
84 }
85}
86
88 this->total_sec_ = sec;
89 this->publish_state(sec + ms * 1e-3f);
90
91 if (this->restore_)
92 this->pref_.save(&sec);
93}
94
96 ESP_LOGCONFIG(TAG,
97 "Duty Time:\n"
98 " Update Interval: %" PRId32 "ms\n"
99 " Restore: %s",
100 this->get_update_interval(), ONOFF(this->restore_));
101 LOG_SENSOR(" ", "Duty Time Sensor:", this);
102 LOG_SENSOR(" ", "Last Duty Time Sensor:", this->last_duty_time_sensor_);
103}
104
105} // namespace esphome::duty_time_sensor
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
void add_on_state_callback(F &&callback)
Base class for all binary_sensor-type classes.
void publish_and_save_(uint32_t sec, uint32_t ms)
void set_sensor(binary_sensor::BinarySensor *sensor)
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:138
bool state
Definition fan.h:2
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t