Ct_clamp sensor very low sample rate - bug?

Hi all,

I’ve just configured a CT clamp sensor, and confirmed it’s working just fine with some Arduino sketch code, but in ESPHome it’s behaving strangly.

The ct_clamp sensor should be sampling the adc several hundred or thousand times a second, the default sample period is 200ms and most people’s experience is that this gives about 500 samples, which is plenty to calculate the RMS value of the 50hz AC sinewave.

However in my setup, on a Wemos ESP32 the ct_clamp is only taking a single sample… claiming it has a sample rate of 5 samples per second. This will never work for sampling a 50hz signal!

I can’t work out what is going wrong, is there a setting somewhere for the global ADC sample rate?

Config:

sensor:
  - platform: adc
    pin: GPIO34
    id: adc0
  - platform: adc
    pin: GPIO35
    id: adc1
  - platform: adc
    pin: GPIO36
    id: adc2
  - platform: adc
    pin: GPIO39
    id: adc3
  - platform: ct_clamp
    sensor: adc0
    name: "Total Energy Usage"
    update_interval: 10s
  - platform: ct_clamp
    sensor: adc1
    name: "House Energy Usage"
    update_interval: 10s
  - platform: ct_clamp
    sensor: adc2
    name: "Garage Energy Usage"
    update_interval: 10s
  - platform: ct_clamp
    sensor: adc3
    name: "EV Charger Energy Usage"
    update_interval: 10s

Output:

[08:34:28][D][ct_clamp:041]: 'EV Charger Energy Usage' - Raw AC Value: 0.000A after 1 different samples (5 SPS)
[08:34:28][D][sensor:094]: 'EV Charger Energy Usage': Sending state 0.00000 A with 2 decimals of accuracy
[08:34:28][D][ct_clamp:041]: 'House Energy Usage' - Raw AC Value: 0.000A after 1 different samples (5 SPS)
[08:34:28][D][sensor:094]: 'House Energy Usage': Sending state 0.00000 A with 2 decimals of accuracy
[08:34:28][D][ct_clamp:041]: 'Total Energy Usage' - Raw AC Value: 0.000A after 1 different samples (5 SPS)
[08:34:28][D][sensor:094]: 'Total Energy Usage': Sending state 0.00000 A with 2 decimals of accuracy
[08:34:31][D][ct_clamp:041]: 'Garage Energy Usage' - Raw AC Value: 0.000A after 1 different samples (5 SPS)
[08:34:31][D][sensor:094]: 'Garage Energy Usage': Sending state 0.00000 A with 2 decimals of accuracy
[08:34:36][D][sensor:094]: 'adc2': Sending state 1.03300 V with 2 decimals of accuracy
[08:34:38][D][ct_clamp:041]: 'EV Charger Energy Usage' - Raw AC Value: 0.000A after 1 different samples (5 SPS)
[08:34:38][D][sensor:094]: 'EV Charger Energy Usage': Sending state 0.00000 A with 2 decimals of accuracy
[08:34:38][D][ct_clamp:041]: 'House Energy Usage' - Raw AC Value: 0.000A after 1 different samples (5 SPS)
[08:34:38][D][sensor:094]: 'House Energy Usage': Sending state 0.00000 A with 2 decimals of accuracy
[08:34:38][D][ct_clamp:041]: 'Total Energy Usage' - Raw AC Value: 0.000A after 1 different samples (5 SPS)
[08:34:38][D][sensor:094]: 'Total Energy Usage': Sending state 0.00000 A with 2 decimals of accuracy

The ct_clamp sensor is working as designed. The ct_clamp sensor throws away any samples that are the same as the previous sample. When there is no current flow through the device under test, all samples are the same and so the only sample taken is the first, and since ct_clamp samples for 200ms by default that works out to a 5 SPS rate. The SPS should increase when you measure actual current. I faced a similar issue and had to read the code to figure this out, it is not documented.

1 Like

:slight_smile: Boy that would be great if it actually worked like that …

changing the attenuation affects this

I would love to see 2 samples when off and then good values when it under load.

What I ponder is I have a 20amp clamp with a 1.2 amp load on my workbench. Is that not enough load percentage wize?

sensor:
  - platform: adc
    pin: GPIO3 #32
    name: ADC Channel 1
    attenuation: 12DB #0db
    accuracy_decimals: 1 #5
    id: adc_channel_1

I get random numbers with zero load.