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

    
sensor:
  # 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
    filters:
      - 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
    total:
      accuracy_decimals: 3
      unit_of_measurement: 'kWh'
      device_class: "energy"
      name: 'elec_meter_total_kWh'
      filters:
        - 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:
  # Switch to restart
  - platform: restart
    name: Electricity meter monitor restart
    
# Sync time
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:

Mike.

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
    pin: 
	number: GPIO27
	mode: 
	  input: true
	  pullup: true

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

Cheers,

Mike.

Hi, I’m working on a project similar to this using the ESP32 and I want to ask regarding the optical sensor you used. Did you use the LM393 as a optical sensor because I believe they’re comparator right? Or can you walk me through your design project it would really help me @Muddy_Boots

Hi Adewale,

Yes, I did use the LM393. although I can’t remeber where I got it from. The small sensor I use has four pins +ve, GND, AO, and DO. I used the digital output (DO) from the sensor board into GPIO27 on the ESP32. The +ve and Gnd come from the 3.3V pin and the GND pin on the ESP32. A pal made a 3D printed housing for it so it fixes to my electric meter easily, Blu-Tack or other sticky poster putties would suffice.

I did the usual setup procedure for the ESP32 (I bought a ‘three-pack’ of WROOM DevKit C units
from Amazon) by plugging it into the PC USB socket and setting it up wirelessly via ESPHome in Home Assistant. Once the initial setup was done and the ESP32 device was accessible via Home Assistant I created a new device in ESPHome and named it electric_meter_monitor (or whatever you wish to call it)

This is the yaml that I added in order for the ESP32 to provide relevant information for Home Assistant

esphome:
  name: elec-meter-monitor
  platform: ESP32
  board: nodemcu-32s

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "4Z3zoJKjDPyix32yRGOATSvqdvtS2u1t0WxeOI0wT4s=" #this is the key you will need to add in Home Assistant when you setup the device.

ota:
  password: "******************************************"

wifi:
  ssid: your wifi ssd
  password: your wifi password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Elec-Meter-Monitor"
    password: "****************"

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

  # instantaneous usage in kW
  - platform: pulse_meter
    pin: 
      number: GPIO27
      mode:
        input: true
        pullup: true
    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
    filters:
      - 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
    total:
      accuracy_decimals: 3
      unit_of_measurement: 'kWh'
      device_class: "energy"
      name: 'elec_meter_total_kWh'
      filters:
        - 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:
  # Switch to restart
  - platform: restart
    name: Electricity meter monitor restart
    
# Sync time
time:
  - platform: sntp
    id: my_time

Once you have set up the code using ESPHome and sent it to the ESP32 device, it should start registering the pulsed LED flash from your electricity meter, an LED on the ESP32 board will also flash too at the same time. The device should show up as an entity in the ESPHome Integration with a number of entities listed too.

Once this is up and running you can add various ‘Meter’ Helpers to log the electricity consumption either Daily, Weekly, or Monthly and also add some code to the main configuration.yaml so you can convert the ‘Meter’ Helpers data into cost ‘sensors’ for use in a dashboard… this is my Electricity Dashboard (below)

I hope the above is useful, and clear. If you need further info, do feel free to ask :+1:t2:

Cheers,

Mike.

Hi,

This was super helpful. Can you tell me what optical sensor you used for this. @Muddy_Boots

Thanks,
Ayo

Hi Ayo,

The sensor I have was given to me by a neighbour who is also a HA tinkerer, and it is the type with a light dependent resistor on it. There are plenty of these shown on eBay or AliExpress… a generic picture of it show below…

If you click this eBay link it will take you to a seller offering the same type I am using.

I modified it slightly by removing the light-dependent resistor and placing it on the other side of the circuit board so I could make adjustments whilst the sensor was over the red LED on the electricity meter - you don’t have to do this, I just thought it would be easier when setting it up in the confined space of the meter cupboard. :roll_eyes:

I hope that helps,

Cheers,

Mike.