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.

1 Like

Hi
I was inspired by the direction you have taken to install my own monitoring unit.
I have also used your updated code.
However I have some questions.

I have not used any metrics from the sensor in HA yet until I understand what I am getting.
I can connect and obtain the logs which a section is listed below.

[14:38:52][C][mdns:116]:   Hostname: merc-power-meter
[14:38:52][C][ota:096]: Over-The-Air Updates:
[14:38:52][C][ota:097]:   Address: 192.168.190.102:3232
[14:38:52][C][ota:100]:   Using Password.
[14:38:52][C][ota:103]:   OTA version: 2.
[14:38:52][C][api:139]: API Server:
[14:38:52][C][api:140]:   Address: 192.168.190.102:6053
[14:38:52][C][api:142]:   Using noise encryption: YES
[14:38:52][C][wifi_signal.sensor:009]: WiFi Signal 'Elec Meter WiFi Signal'
[14:38:52][C][wifi_signal.sensor:009]:   Device Class: 'signal_strength'
[14:38:52][C][wifi_signal.sensor:009]:   State Class: 'measurement'
[14:38:52][C][wifi_signal.sensor:009]:   Unit of Measurement: 'dBm'
[14:38:52][C][wifi_signal.sensor:009]:   Accuracy Decimals: 0
[14:38:52][D][sensor:094]: 'elec_meter_total_kWh': Sending state 0.33900 kWh with 3 decimals of accuracy
[14:38:52][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.75323 kW with 4 decimals of accuracy
[14:38:56][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 753.23260 W with 1 decimals of accuracy
[14:38:57][D][sensor:094]: 'elec_meter_total_kWh': Sending state 0.34000 kWh with 3 decimals of accuracy
[14:38:57][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.74778 kW with 4 decimals of accuracy
[14:39:01][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 747.77954 W with 1 decimals of accuracy
[14:39:02][D][sensor:094]: 'elec_meter_total_kWh': Sending state 0.34100 kWh with 3 decimals of accuracy
[14:39:02][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.75231 kW with 4 decimals of accuracy
[14:39:06][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 752.30707 W with 1 decimals of accuracy
[14:39:07][D][sensor:094]: 'elec_meter_total_kWh': Sending state 0.34200 kWh with 3 decimals of accuracy
[14:39:07][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.75798 kW with 4 decimals of accuracy
[14:39:11][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 757.98383 W with 1 decimals of accuracy
[14:39:12][D][sensor:094]: 'elec_meter_total_kWh': Sending state 0.34300 kWh with 3 decimals of accuracy
[14:39:12][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.75411 kW with 4 decimals of accuracy
[14:39:16][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 754.11115 W with 1 decimals of accuracy
[14:39:16][D][sensor:094]: 'elec_meter_total_kWh': Sending state 0.34400 kWh with 3 decimals of accuracy
[14:39:16][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.75456 kW with 4 decimals of accuracy
[14:39:16][D][sensor:094]: 'Elec Meter WiFi Signal': Sending state -37.00000 dBm with 0 decimals of accuracy
[14:39:21][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 754.55688 W with 1 decimals of accuracy
[14:39:21][D][sensor:094]: 'elec_meter_total_kWh': Sending state 0.34500 kWh with 3 decimals of accuracy

Is this what you would expect as a output?
I did edit accuracy down to 3 to understand what impact it has.
I am using the LM393 with Digital out and it is working perfect with the LED pust from the meter.
I can see the watts increasing but it appears to increment only 1 per 5 seconds
This could change as energy demand increases.
Are you also waare of a way that I could edit the total value either in the card or better in HA to match the meter reading?

Thanks

Second picture.

1 Like

Hi Tony,

I’ve now been running this elec meter sensor for over a year, and it seems pretty reliable. However, in saying that, it does suffer from some problems:-

  1. Due to its position, it does sometimes drop out from the WiFi network, and wirelessly updating the ESP32 can be a challenge. The dropout also affects the overall total elec consumption figure, which is supposed to mirror the actual elec’ meter readout.
  2. I have noticed a discrepancy between the true meter total and the ESP32 / Home Assistant total over the course of each month, so have modified my code a bit, by adding a multiplier figure to the meter reading in the hope I can gain a bit more accuracy, or at least minimise the discrepancy. I just logged the actual meter reading against the HA Total on a weekly basis over a couple of months and averaged out the discrepancies between the two readings to provide an ‘adjustment figure’. It does help, up to a point. The meter installed by the electric company in my home has a 5% variation in accuracy (from what I have read on other forums about it) so this will not be helping much with regard to total accuracy. Dealing with elec companies here is a bloody nightmare, so I couldn’t give a rats arse about any minor inaccuracy on my installed meter. I’d just rather live with it. I have to post my meter reading online anyway as it a dumb meter, so the Home Assistant monitor is just used as an approximate indicator of what is being consumed.

This is a copy of my logs as of 10 minutes ago…

[10:44:02][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02966 number with 5 decimals of accuracy
[10:44:07][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.28638 kWh with 5 decimals of accuracy
[10:44:07][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.39996 kW with 4 decimals of accuracy
[10:44:09][D][sensor:094]: 'Elec Meter Uptime': Sending state 517293.62500 s with 0 decimals of accuracy
[10:44:11][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 399.96329 W with 1 decimals of accuracy
[10:44:12][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02880 number with 5 decimals of accuracy
[10:44:16][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.28759 kWh with 5 decimals of accuracy
[10:44:16][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.39159 kW with 4 decimals of accuracy
[10:44:21][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 391.59320 W with 1 decimals of accuracy
[10:44:22][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02819 number with 5 decimals of accuracy
[10:44:25][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.28880 kWh with 5 decimals of accuracy
[10:44:25][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.38738 kW with 4 decimals of accuracy
[10:44:31][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 387.37872 W with 1 decimals of accuracy
[10:44:32][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02789 number with 5 decimals of accuracy
[10:44:34][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.29000 kWh with 5 decimals of accuracy
[10:44:34][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.38942 kW with 4 decimals of accuracy
[10:44:41][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 389.41638 W with 1 decimals of accuracy
[10:44:42][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02804 number with 5 decimals of accuracy
[10:44:44][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.29121 kWh with 5 decimals of accuracy
[10:44:44][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.38266 kW with 4 decimals of accuracy
[10:44:46][D][sensor:094]: 'Elec Meter WiFi Signal': Sending state -70.00000 dBm with 0 decimals of accuracy
[10:44:51][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 382.65982 W with 1 decimals of accuracy
[10:44:52][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02755 number with 5 decimals of accuracy
[10:44:53][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.29241 kWh with 5 decimals of accuracy
[10:44:53][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.38190 kW with 4 decimals of accuracy
[10:45:01][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 381.89865 W with 1 decimals of accuracy
[10:45:02][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02750 number with 5 decimals of accuracy
[10:45:03][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.29362 kWh with 5 decimals of accuracy
[10:45:03][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.38516 kW with 4 decimals of accuracy
[10:45:09][D][sensor:094]: 'Elec Meter Uptime': Sending state 517353.62500 s with 0 decimals of accuracy
[10:45:11][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 385.16281 W with 1 decimals of accuracy
[10:45:12][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.29482 kWh with 5 decimals of accuracy
[10:45:12][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.38052 kW with 4 decimals of accuracy
[10:45:12][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02740 number with 5 decimals of accuracy
[10:45:21][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 380.52484 W with 1 decimals of accuracy
[10:45:22][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.29603 kWh with 5 decimals of accuracy
[10:45:22][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.37361 kW with 4 decimals of accuracy
[10:45:22][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02690 number with 5 decimals of accuracy
[10:45:31][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 373.61258 W with 1 decimals of accuracy
[10:45:31][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.29723 kWh with 5 decimals of accuracy
[10:45:31][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.37176 kW with 4 decimals of accuracy
[10:45:32][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02677 number with 5 decimals of accuracy
[10:45:41][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.29844 kWh with 5 decimals of accuracy
[10:45:41][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.39030 kW with 4 decimals of accuracy
[10:45:41][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 390.29684 W with 1 decimals of accuracy
[10:45:42][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02810 number with 5 decimals of accuracy
[10:45:46][D][sensor:094]: 'Elec Meter WiFi Signal': Sending state -69.00000 dBm with 0 decimals of accuracy
[10:45:50][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.29964 kWh with 5 decimals of accuracy
[10:45:50][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.39248 kW with 4 decimals of accuracy
[10:45:51][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 392.48315 W with 1 decimals of accuracy
[10:45:52][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02826 number with 5 decimals of accuracy
[10:45:59][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.30085 kWh with 5 decimals of accuracy
[10:45:59][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.38807 kW with 4 decimals of accuracy
[10:46:01][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 388.07489 W with 1 decimals of accuracy
[10:46:02][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02794 number with 5 decimals of accuracy
[10:46:08][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.30206 kWh with 5 decimals of accuracy
[10:46:08][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.38201 kW with 4 decimals of accuracy
[10:46:09][D][sensor:094]: 'Elec Meter Uptime': Sending state 517413.62500 s with 0 decimals of accuracy
[10:46:11][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 382.01114 W with 1 decimals of accuracy
[10:46:12][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02750 number with 5 decimals of accuracy
[10:46:18][D][sensor:094]: 'elec_meter_total_kWh': Sending state 82.30326 kWh with 5 decimals of accuracy
[10:46:18][D][sensor:094]: 'Electricity Usage Now in kW': Sending state 0.38837 kW with 4 decimals of accuracy
[10:46:21][D][sensor:094]: 'Electricity Usage Now in Watts': Sending state 388.36609 W with 1 decimals of accuracy
[10:46:22][D][sensor:094]: 'Electricity Cost Now': Sending state 0.02796 number with 5 decimals of accuracy

Yes, this update time will change according to the frequency of pulses received… it is the timing between the pulses which correspond to the electricity usage. If hardly using anything, such at night when in bed, then the pulses could be quite widely spaced out, and a reading would only occur from pulse to pulse.

If I understand you correctly, you want the total readout in HA to match the current meter reading? If this is the case, please see this small thread about using the calibrate function - Set value current energy?

Since last commenting on this thread I have changed the YAML code within my meter to include a cost ‘now’ (sort of an instant monetary readout) which varies according to the consumption at that point in time. This is then shown on a gauge display in the dashboard - see screen grab below. (Never got around to calculating the accuracy of this instant cost though - code noted lower down for this)

image

The YAML I’m using at present is noted below, if you should require it.

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

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "4Z3zoJKjDPyix32yRGOATSvqdvtS2u1t0WxeOI0wT4s="

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

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    # Set this to the IP of the ESP
    static_ip: 192.168.0.192
    # Set this to the IP address of the router. Often ends with .1
    gateway: 192.168.0.2
    # The subnet of the network. 255.255.255.0 works for most home networks.
    subnet: 255.255.255.0

  # 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: 5
      unit_of_measurement: 'kWh'
      device_class: "energy"
      name: 'elec_meter_total_kWh'
      filters:
        - multiply: 0.001205714286  # (1/1000 pulses per kWh) and an adjustment figure to gain greater accuracy between this sensor and true meter values (multiplies by 1.12057.... with long-term reading values)

  # 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: 10s

# Instantaneous Cost
#Code after Lambda takes the power in Watts value and multiplies by Elec Co. kWh charge (Noted as of £0.25920/kWh on the monthly bill) and converts to cost per Watt/Second = 0.000072 figure. Using a 10s refresh interval
  - platform: template
    name: "Electricity Cost Now"
    accuracy_decimals: 5
    unit_of_measurement: 'number'
    state_class: measurement
    device_class: monetary
    lambda: 'return (id(elec_meter_now_kW).state * 1000) * 0.000072;' 
    update_interval: 10s

  # 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 hope the above is useful and should you need any additional clarification, by all means, shout up, always happy to help (with my limited knowledge! :roll_eyes: )

Cheers,

Mike.

Mike Thanks for the quick reply.
I used your updated code and made some small edits.
Screenshot 2024-03-29 at 21-07-16 Monarch – Home Assistant

1 Like

If I understand you correctly, you want the total readout in HA to match the current meter reading? If this is the case, please see this small thread about using the calibrate function - Set value current energy?
I did find the answer on the EPSHome website under “Pulse Meter Sensor”
https://esphome.io/components/sensor/pulse_meter.html
image

I now just need to understand how to Schedule the service code below to run on 1st of each month to zero out the values.

service: esphome.vector_power_meter_set_total
data:
  new_total: 0

I have used the same service to match the Meter Reading on the first line of the card.

I should also note I had to use another GPIO pin to count the same pulses (GPIO26 & 27) as I needed 2 pulse sensors. Diodes are very helpful to bridge these two pins.

Tony

1 Like

Nice one Tony. Looks like you have got it up and running very nicely.

Sorry, I can’t help you with the code to reset your values on the first of the month - way beyond my knowledge! :flushed:

Two pulse sensors too? Are you using more than one electrical phase?

Cheers,

Mike.

Hi Mike
After some investigation today I have arrived that I need to use 3 GPIO pins.
GPIO27 = Meter Reading (it is updated as required to match the meter board reading)
GPIO26 = Monthly Reading (It is reset on the 1st of each Month)
GPIO25 = Daily Reading (It is reset in 1st minute of each day)

It all comes from the same sensor wire coming from the LM393 but I split it 3 ways using Diodes.
It all works very well now.

Tony

1 Like

Ahh… I see, each pin input would log to a different variable I presume. Another way to do the same would be to use just one input and utilise the “Meter Helpers” which can be set to record daily, yesterday, weekly, and monthly (and more) totals. It is how I did my dashboard, but used them both for gas and elec’.

Cheers,

Mike.

Mike
OK, you have my attention again.
How did you use the Helpers to record daily & monthly?
I have looked at the helpers but it only shows 7 days Sun-Sat
and without any consideration to 1st day or last day of month.
Like you, I plan to move onto the Gas next. Just have to find a sensor that can read it.

Tony

Morning Tony,

I had to scratch my head bit here, as I’d forgotten what I did and how I did it!

I set up a number of Helpers to create daily, weekly and monthly metering, these are listed here. They are under Settings > Helpers

There is also some yaml to accompany the cost part of these, which goes under the ‘configuration.yaml’ section - (I use Studio Code Server plugin to see & edit this sort of stuff). The numbers used (0.25920) on this yaml is the cost per kWh in £’s charged by the elec Co. BTW, these sort of YAML entries tend to be known as ‘template sensors’. In these, I’ve got Meter Helpers working with these template sensors. The first one for Daily Cost uses a Meter Helper called Elec Daily Consumption - this total is multiplied by the cost per kWh, and then spewed out using the unique_id of daily_electric_cost - it is pretty much replicated for the other cost examples on here - Meter Helpers to log the time interval kWh totals, and then use cost from Template Sensors - the Template Sensors also end up as Helpers too (it seems)

template:
  - sensor:
      - name: "Daily Electric Cost" # Use a Meter Helper for display in Dashboard
        unique_id: daily_electric_cost
        state: "{{ (states('sensor.electricity_daily_consumption')|float * 0.25920)|round(2) }}"
        availability: "{{ states('sensor.electricity_daily_consumption')|is_number }}" 
        unit_of_measurement: "£"
        device_class: monetary
        state_class: total

      - name: "Weekly Electric Cost" # Create a Meter Helper "Weekly Electric Cost" for display in Dashboard
        unique_id: weekly_electric_cost
        state: "{{ (states('sensor.electricity_weekly_consumption')|float * 0.25920)|round(2) }}"
        availability: "{{ states('sensor.electricity_weekly_consumption')|is_number }}" 
        unit_of_measurement: "£"
        device_class: monetary
        state_class: total

      - name: "Monthly Electric Cost" # Create a Meter Helper "Monthly Electric Cost" for display in Dashboard
        unique_id: monthly_electric_cost
        state: "{{ (states('sensor.electricity_monthly_consumption')|float * 0.25920)|round(2) }}"
        availability: "{{ states('sensor.electricity_monthly_consumption')|is_number }}" 
        unit_of_measurement: "£"
        device_class: monetary
        state_class: total

Using a combination of the YAML and Meter Helpers I can pull stuff such as this into my Dashboard
image
The Meter Helpers are the key as they allow you to take the main kWh log and manipulate it into daily, weekly, and other totals.

This may be useful - Utility meter helper kwh
and have a read of this too - Utility Meter - Home Assistant

Gas Meter
Is your gas meter a ‘dumb’ one at all? If it is, then this may help you with yours, it is the main thread for me automating my own dumb gas meter.

It’s a long read, so either grab a brew or a cold one and settle in for weeks of head-scratching and frustration. I solved it in the end, and it works like an absolute beaut’ now. More accurate than the elec’ meter too!! :+1:t2:

I hope the above helps,

Cheers,

Mike