ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
ct_clamp_sensor.cpp
Go to the documentation of this file.
1#include "ct_clamp_sensor.h"
2
3#include "esphome/core/log.h"
4#include <cinttypes>
5#include <cmath>
6
8
9static const char *const TAG = "ct_clamp";
10
12 LOG_SENSOR("", "CT Clamp Sensor", this);
13 ESP_LOGCONFIG(TAG, " Sample Duration: %.2fs", this->sample_duration_ / 1e3f);
14 LOG_UPDATE_INTERVAL(this);
15}
16
18 // Update only starts the sampling phase, in loop() the actual sampling is happening.
19
20 // Request a high loop() execution interval during sampling phase.
21 this->high_freq_.start();
22
23 // Set timeout for ending sampling phase
24 this->set_timeout("read", this->sample_duration_, [this]() {
25 this->is_sampling_ = false;
26 this->high_freq_.stop();
27
28 if (this->num_samples_ == 0) {
29 // Shouldn't happen, but let's not crash if it does.
30 this->publish_state(NAN);
31 return;
32 }
33
34 const float rms_ac_dc_squared = this->sample_squared_sum_ / this->num_samples_;
35 const float rms_dc = this->sample_sum_ / this->num_samples_;
36 const float rms_ac_squared = rms_ac_dc_squared - rms_dc * rms_dc;
37 float rms_ac = 0;
38 if (rms_ac_squared > 0)
39 rms_ac = std::sqrt(rms_ac_squared);
40 ESP_LOGD(TAG, "'%s' - Raw AC Value: %.3fA after %" PRIu32 " different samples (%" PRIu32 " SPS)",
41 this->name_.c_str(), rms_ac, this->num_samples_, 1000 * this->num_samples_ / this->sample_duration_);
42 this->publish_state(rms_ac);
43 });
44
45 // Set sampling values
46 this->last_value_ = 0.0;
47 this->num_samples_ = 0;
48 this->sample_sum_ = 0.0f;
49 this->sample_squared_sum_ = 0.0f;
50 this->is_sampling_ = true;
51}
52
54 if (!this->is_sampling_)
55 return;
56
57 // Perform a single sample
58 float value = this->source_->sample();
59 if (std::isnan(value))
60 return;
61
62 // Assuming a sine wave, avoid requesting values faster than the ADC can provide them
63 if (this->last_value_ == value)
64 return;
65 this->last_value_ = value;
66
67 this->num_samples_++;
68 this->sample_sum_ += value;
69 this->sample_squared_sum_ += value * value;
70}
71
72} // namespace esphome::ct_clamp
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:510
void stop()
Stop running the loop continuously.
Definition helpers.cpp:736
void start()
Start running the loop continuously.
Definition helpers.cpp:730
constexpr const char * c_str() const
Definition string_ref.h:73
voltage_sampler::VoltageSampler * source_
The sampling source to read values from.
float last_value_
The DC offset of the circuit.
uint32_t sample_duration_
Duration in ms of the sampling phase.
HighFrequencyLoopRequester high_freq_
High Frequency loop() requester used during sampling phase.
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
virtual float sample()=0
Get a voltage reading, in V.