HELP! - ESPHome - ESP32 & LM393 Optical Sensor Power Spikes LED Pulse Meter

Hi Guys,

I’m in need of a bit of help here. I have my Energy monitoring setup and working well using an ESP32 which is fed with the digital output from a LM393 optical sensor. The optical sensor is mounted in a 3D-printed housing which is affixed to the pulse LED on my Landis & Gyr electric meter.

This is my electric meter which is housed in a dark cupboard.

Power spikes are appearing randomly and at first hit 100,000W and one at 180,000W :astonished: These spikes are spurious as there is nothing in my home that can momentarily draw that sort of power.

The above is a screen grab of the spikes; the ones with the red circle are the very high occurrences as noted previously, the ones with the blue circle were produced after I modified my yaml code to add a sliding_moving_window_average filter to try and eliminate the spikes, or at least reduce them. The spikes have been reduced (filter doing its job!) but it is still throwing out the accuracy of my meter readings and I would like to resolve it.

This is my yaml code for the ESP32

  # Uptime sensor
  - platform: uptime
    name: Elec Meter Uptime

  # instantaneous usage in kW
  - platform: pulse_meter
    pin: GPIO27
    unit_of_measurement: 'kW'
    name: 'Electricity Usage Now in kW'
    id: elec_meter_now_kW
    state_class: measurement
    device_class: power
    accuracy_decimals: 4
    icon: mdi:flash-outline
    #internal_filter_mode: "PULSE"
    internal_filter: 40ms
    timeout: 4min
      - multiply: 0.06
      - sliding_window_moving_average:
          window_size: 5
          send_every: 5
# Total meter usage since reset - used for display of actual Meter reading
# and displays value when reset using a service call
      accuracy_decimals: 3
      unit_of_measurement: 'kWh'
      device_class: "energy"
      name: 'elec_meter_total_kWh'
        - multiply: 0.001  # (1/1000 pulses per kWh)  

  # Instantaneous usage in Watts
  - platform: template
    name: "Electricity Usage Now in Watts"
    unit_of_measurement: 'W'
    state_class: measurement
    device_class: power
    lambda: 'return (id(elec_meter_now_kW).state * 1000);' 
    update_interval: 5s

  # WiFi Signal sensor
  - platform: wifi_signal
    name: Elec Meter WiFi Signal
    update_interval: 60s
  # Switch to restart
  - platform: restart
    name: Electricity meter monitor restart
# Sync time
  - platform: sntp
    id: my_time

I have tried modifying the internal_filter time and have found if I go above 40ms the meter reading seems to read a bit low - 40ms is possibly just on the threshold of the LED pulse length. I have also tried the filter_mode to “PULSE”, but again, this seemed to give a lower reading than the default “EDGE” reading. (It is currently defaulted to pulse edge reading hence the #internal_filter_mode set as a comment).

I spent most of yesterday reading various forum posts from others who have had similar problems, however, the other solutions to this issue didn’t seem to match my setup (do bear in mind that I’m new to YAML and I’m still finding it very confusing indeed! :flushed:).

I don’t know if it is possible to remove the random spike and replace that value with an average from two or more previous readings?? I really haven’t a clue. Any advice on what to do, and how to do it would be greatly appreciated so I can move on to other aspects of my HA setup and put my energy sensor setup behind me.

Thanks in advance. :+1:t2:


Answering my own question here… but I seemed to have solved it.

I didn’t realize there were internal pull-up/pull-down resistors on the ESP32… so I guessed that the input from the sensor to the ESP32 needed a pull-up resistor adding on. Instead of soldering one on I thought I would try turning on the built-in one. I included this part of the code in the yaml file for elec_meter_monitor in the ESPHome section which will turn on the built-in pull-up resistor.

 - platform: pulse_meter
	number: GPIO27
	  input: true
	  pullup: true

It’s now been running for over 24 hours and not a spike to be seen!