From 75372b04505c931e2081c6b03bcd25bad3237577 Mon Sep 17 00:00:00 2001 From: David00 Date: Sun, 2 Apr 2023 18:52:01 -0700 Subject: [PATCH] Fix crash when user has channel 1 disabled --- rpi_power_monitor/power_monitor.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rpi_power_monitor/power_monitor.py b/rpi_power_monitor/power_monitor.py index 1e9ffcb..dca855f 100644 --- a/rpi_power_monitor/power_monitor.py +++ b/rpi_power_monitor/power_monitor.py @@ -459,7 +459,11 @@ def calculate_power(self, samples, board_voltage): ac_voltage_ratio = (self.grid_voltage / self.ac_transformer_output_voltage) * 11 # Rough approximation voltage_scaling_factor = vref * ac_voltage_ratio * self.voltage_calibration - num_samples = len(v_samples_1) + # Get the number of samples by checking the length of one of the sample buffers. + for chan_num in range(1, 7): + if f'ct{chan_num}' in samples.keys(): + num_samples = len(samples[f'ct{chan_num}']) + break for i in range(0, num_samples): if ct1_samples: