ESPHome 2026.11.0b6
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
6ESPHOME_LOG_TAG(TAG, "duty_time_sensor");
7
8#ifdef USE_BINARY_SENSOR
9void DutyTimeSensor::set_sensor(binary_sensor::BinarySensor *const sensor) {
10 sensor->add_on_state_callback([this](bool state) { this->process_state_(state); });
11}
12#endif
13
14void DutyTimeSensor::start() {
15 if (!this->last_state_)
16 this->process_state_(true);
17}
18
19void DutyTimeSensor::stop() {
20 if (this->last_state_)
21 this->process_state_(false);
22}
23
24void DutyTimeSensor::update() {
25 if (this->last_state_)
26 this->process_state_(true);
27}
28
29void DutyTimeSensor::loop() {
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 PUBLISH_STATE(this->last_duty_time_sensor_, (now - this->edge_time_) * 1e-3f);
74 }
75
76 } else if (state) {
77 // rising edge
78 this->last_time_ = now - this->last_time_; // store time with ms correction
79 this->edge_time_ = now; // store turn-on start time
80 this->last_state_ = true;
81 }
82}
83
85 this->total_sec_ = sec;
86 this->publish_state(sec + ms * 1e-3f);
87
88 if (this->restore_)
89 this->pref_.save(&sec);
90}
91
92void DutyTimeSensor::dump_config() {
93 ESP_LOGCONFIG(TAG,
94 "Duty Time:\n"
95 " Update Interval: %" PRId32 "ms\n"
96 " Restore: %s",
97 this->get_update_interval(), ONOFF(this->restore_));
98 LOG_SENSOR(" ", "Duty Time Sensor:", this);
99 LOG_SENSOR(" ", "Last Duty Time Sensor:", this->last_duty_time_sensor_);
100}
101
102} // namespace esphome::duty_time_sensor
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
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 publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:141
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "duty_time_sensor")
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t