Hi,
I have an ESP 32 and 3 CT clamps for my 3 phases at home (Europe). Following CT Clamp Current Sensor — ESPHome, this is my config:
sensor:
- platform: ct_clamp
sensor: adc_sensor_i1
name: "I_ph1"
id: I_ph1
update_interval: 1s
sample_duration: 1s
filters:
- calibrate_linear:
# Measured value of 0 maps to 0A
- 0 -> 0
# 1 V --> 30 Amp from CT sensor
- 1.0 -> 30.0
- platform: ct_clamp
sensor: adc_sensor_i2
name: "I_ph2"
id: I_ph2
update_interval: 1s
sample_duration: 1s
filters:
- calibrate_linear:
# Measured value of 0 maps to 0A
- 0 -> 0
# 1 V --> 30 Amp from CT sensor
- 1.0 -> 30.0
# - sliding_window_moving_average:
# window_size: 10 # 200ms * 10 -> 2s
# send_every: 10
- platform: ct_clamp
sensor: adc_sensor_i3
name: "I_ph3"
id: I_ph3
update_interval: 1s
sample_duration: 1s
filters:
- calibrate_linear:
# Measured value of 0 maps to 0A
- 0 -> 0
# 1 V --> 30 Amp from CT sensor
- 1.0 -> 30.0
- platform: adc
pin: GPIO32
id: adc_sensor_i1
# raw: true
attenuation: auto
# update_interval: 1s
- platform: adc
pin: GPIO33
id: adc_sensor_i2
# raw: true
attenuation: auto
# update_interval: 1s
- platform: adc
pin: GPIO34
id: adc_sensor_i3
# raw: true
attenuation: auto
# update_interval: 1s
I’m having some trouble understanding sample_duration
, and in particular, knowing if it can be used for continuously measuring the current and building an average, then reporting that average every 1s or so.
What I would like to achieve is to measure the current as often as possible, create an average, and report that average to home assistant. How can I achieve this? I tried with sliding_window_moving_average
(see code commented), but not sure if this is the way to do it.
I think you’re on the right track. sample_duration
should be set to lower value; it defaults to 200ms, and it looks like a reasonable value. This parameter controls how often the CT clamp will be polled for readings.
update_interval
, on the other hand, controls how often ESPHome’s sensor produces a value. This setting should be higher than the sample duration. I’d make it at least twice as long.
Then you can put filters o top which will average values produced by ESPHome’s sensor. Sliding window moving average is good for this purpose, but you have to be mindful of the window side parameter. Keep in mind that you’re averaging values that appear every update_interval
.
Thanks @lukasz-tuz ,
a few follow up questions.
- If I set up the window_size parameter to let’s say 10, and the update_interval to 1s, will the device report values every 10 secs? or will it still report every 1 sec, but having the average of the last 10 sec?
- if I set the sample_duration parameter to 200ms and the update_interval to 1s, it means that out of the 1000ms in every second we are reporting, only the last 200 ms are being resampled. All that happens on the first 800ms won’t be included in the final value. If I want to avoid it, does it make sense to have sample_duration and update_interval exactly the same value?
- without adding an extra average function, can the update_interval and sample_duration can be used as an average? what I want is to report every 2 seconds a value that is the average of “as many samples as possible within these 2 secs”
thanks
If I set up the window_size parameter to let’s say 10, and the update_interval to 1s, will the device report values every 10 secs?
No. window_size
determines how many readings to average over, update_interval
- how often the averaged value will be published. So with window_size
set to 10 and update_interval
to 1s, you’ll get an average of 10 previous samples published every second.
if I set the sample_duration parameter to 200ms and the update_interval to 1s, it means that out of the 1000ms in every second we are reporting, only the last 200 ms are being resampled.
Not exactly. Sensor’s code calculates RMS value over the update_interval
. Inside the 1 second update interval window you’ll get five samples (every 200ms). The five collected samples will be used to calculate RMS value, and RMS value is published by the sensor.
without adding an extra average function, can the update_interval and sample_duration can be used as an average? what I want is to report every 2 seconds a value that is the average of “as many samples as possible within these 2 secs”
Yep; just leave the sample_duration
at the default value, set update_interval
to 2 seconds, and every two seconds you’ll get a RMS value of sampled readings.
1 Like