Water usage from LJ18A3 sensor drifting

I assembled a water meter following this guide:

Using this sensor: LJ18A3-8-Z/BX-5V
https://www.aliexpress.com/item/32826218456.html

On a ESP32WROVER from AliExpress.

The sensor reads pulses and if I stand there looking at it, it seems like the sensor is reading the pulses fine (red LED pn the sensor lights up when it detects the metal on the needle).
Unfortunately the measures are drifting considerably at a rate of roughly 3.000 liters per day.

Seems the drift happens during use of water, there’s no water usage registered during the night which would indicate ghost pulses while nothing was happening.

I would like someone smarter than me to please tell me what could I be possibly doing wrong and how could I troubleshoot.

My code:

substitutions:
  name: esphome-web-21dc30
  friendly_name: ESP32 Tester

esphome:
  name: ${name}
  friendly_name: ${friendly_name}
  min_version: 2024.6.0
  name_add_mac_suffix: false
  project:
    name: esphome.web
    version: dev

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:

# Allow Over-The-Air updates
ota:
- platform: esphome

# Allow provisioning Wi-Fi via serial
improv_serial:

wifi:
  # Set up a wifi access point
  ssid: *****
  password: ***** 

# In combination with the `ap` this allows the user
# to provision wifi credentials to the device via WiFi AP.
captive_portal:

dashboard_import:
  package_import_url: github://esphome/example-configs/esphome-web/esp32.yaml@main
  import_full_config: true

# Sets up Bluetooth LE (Only on ESP32) to allow the user
# to provision wifi credentials to the device.
esp32_improv:
  authorizer: none

# To have a "next url" for improv serial
web_server:



sensor:
  - platform: pulse_counter
    pin: 
      number: GPIO2
      allow_other_uses: true
    update_interval: 6s
    name: "water pulse"
    id: water_pulse
    

  - platform: pulse_meter
    pin: 
      number: GPIO2
      allow_other_uses: true
    name: "Water Pulse Meter"
    unit_of_measurement: "liter/min"
    icon: "mdi:water"
    total:
      name: "Water Total"
      unit_of_measurement: "liter"

  - platform: pulse_meter
    pin: 
      number: GPIO2
      allow_other_uses: true
    name: "Water Pulse Meter"
    unit_of_measurement: "liter/min"
    icon: "mdi:water"
    total:
      name: "Water Meter Total"
      unit_of_measurement: "m³"
      id: water_meter_total
      accuracy_decimals: 3
      device_class: water
      state_class: total_increasing
      filters:
        - offset: 252735
        - multiply: 0.001

  - platform: template
    name: "Water Usage Liter"
    id: water_flow_rate
    accuracy_decimals: 1
    unit_of_measurement: "l/min"
    icon: "mdi:water"
    lambda: return (id(water_pulse).state * 10);
    update_interval: 6s

my instalation:

Looking at the log (below) I see there’s a peak in the measurement once a faucet is open, then it seems to stabilize.
I’m increasing the sensor update from 6s to 60s to see what happens.

log:

accuracy
[18:43:00][D][sensor:094]: 'Water Usage Liter': Sending state 0.00000 l/min with 1 decimals of accuracy
[18:43:01][D][pulse_counter:181]: 'water pulse': Retrieved counter: 0.00 pulses/min
[18:43:01][D][sensor:094]: 'water pulse': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[18:43:01][D][sensor:094]: 'Water Meter Total': Sending state 300.73401 m³ with 3 decimals of accuracy
[18:43:01][D][sensor:094]: 'Water Pulse Meter': Sending state 1.69735 liter/min with 2 decimals of accuracy
[18:43:03][D][sensor:094]: 'Water Meter Total': Sending state 300.73502 m³ with 3 decimals of accuracy
[18:43:03][D][sensor:094]: 'Water Pulse Meter': Sending state 35.39725 liter/min with 2 decimals of accuracy
[18:43:06][D][sensor:094]: 'Water Usage Liter': Sending state 0.00000 l/min with 1 decimals of accuracy
[18:43:07][D][sensor:094]: 'Water Meter Total': Sending state 300.73602 m³ with 3 decimals of accuracy
[18:43:07][D][sensor:094]: 'Water Pulse Meter': Sending state 15.09443 liter/min with 2 decimals of accuracy
[18:43:07][D][pulse_counter:181]: 'water pulse': Retrieved counter: 10.00 pulses/min
[18:43:07][D][sensor:094]: 'water pulse': Sending state 10.00000 pulses/min with 2 decimals of accuracy
[18:43:09][D][sensor:094]: 'Water Meter Total': Sending state 300.73700 m³ with 3 decimals of accuracy
[18:43:09][D][sensor:094]: 'Water Pulse Meter': Sending state 34.28638 liter/min with 2 decimals of accuracy
[18:43:12][D][sensor:094]: 'Water Usage Liter': Sending state 100.00000 l/min with 1 decimals of accuracy
[18:43:13][D][pulse_counter:181]: 'water pulse': Retrieved counter: 10.00 pulses/min
[18:43:13][D][sensor:094]: 'water pulse': Sending state 10.00000 pulses/min with 2 decimals of accuracy
[18:43:18][D][sensor:094]: 'Water Usage Liter': Sending state 100.00000 l/min with 1 decimals of accuracy
[18:43:19][D][pulse_counter:181]: 'water pulse': Retrieved counter: 0.00 pulses/min
[18:43:19][D][sensor:094]: 'water pulse': Sending state 0.00000 pulses/min with 2 decimals of accuracy
[18:43:24][D][sensor:094]: 'Water Usage Liter': Sending state 0.00000 l/min with 1 decimals of accuracy

After some trial and error I managed to get the meter to read correctly by using a pulse_counter sensor:

sensor:
  - platform: pulse_counter
    pin: 
      number: GPIO2
    update_interval: 60s #60s is the default value
    name: "Water Pulse Meter_" 
    unit_of_measurement: "liters"
    icon: "mdi:water"
    total:
      name: "Water Pulse Total_"
      unit_of_measurement: "liters"
      id: water_meter_total
      device_class: water
      state_class: total_increasing
      filters:
#the value below should be the value on the water meter at the time of flashing. It will reset to the number below if you flash again.
        - offset: 262739 
1 Like