Water usage sensor

I’ve spent many hours trying to figure out how to get proper water measurements using examples that others have posted, including the one you linked to. I’m still not certain how it all works, but here’s my code for my sensor that outputs one pulse for every 0.0748 gallons (13.36898395721925 pulses per gallon). I’ve had this running for a few weeks now and it’s very accurate.

The Energy Dashboard uses sensor.water_total as it’s source, which is the total_increasing value.
The sensor.water_rate is the current rate of water flow, but if you watch that value change as you run a faucet it varies wildly and I’m not sure it provides any value when using it directly.

To get the hourly, daily, monthly, etc. usage I created a single Reimann Sum entity in sensors.yaml which uses the sensor.water_rate as it’s source. The Reimann Sum seems to smooth out the fluctuating values from the sensor.water_rate:

  - platform: integration
    source: sensor.water_rate
    name: Water RSI
    method: left

Once the sensor.water_rsi entity created I used that as the source for each of the hourly, daily, weekly, etc sensors. I didn’t use the yaml method for creating these sensors as the Brinkman article shows, but instead used Helpers (Settings - Devices - Helpers - Utility Meter) to create them, with each using the same sensor.water_rsi as a source and selecting the different meter reset cycles.

sensor:
  - platform: pulse_counter
    name: "Water Rate"
    id: water_rate
    pin: 
      number: GPIO5
      mode:
        input: true
        pullup: true
    update_interval: 10s
    count_mode:
      rising_edge: DISABLE
      falling_edge: INCREMENT
    unit_of_measurement: "gal/hr"
    accuracy_decimals: 3
    filters:
     - debounce: 1.0s
     - lambda: return (x / 13.36898395721925) * 60;  
    total:
      name: "Water Total"
      unit_of_measurement: "gal"
      id: water_total
      accuracy_decimals: 3
      device_class: water
      state_class: total_increasing
      filters:
        - multiply: 0.0748

Hi @mightybosstone ,

Thank you for your code snippet, it’s helped a lot. Maybe Im just tired and need to look at this again, however, why are you multiplying the pulses per gal by 60? Is your system reading pulses per minute?

I ask as Im converting to L/s and m3, respectively, and would thus change things to /60 instead of *60.

Any thoughts?
Thanks

Most examples show, and I believe the pulse counter default, is to report liters per minute, but I wanted gallons per hour. My meter documentation provided the flow rate in pulses per gallon, so the * 60 converts it from per minute to per hour. @DeeBeeKay has an example here that shows this.

Once you think you have it working do a test by filling a liter or gallon bottle with water and see if it closely matches what’s being reported. I first started out with a different meter that I used only for an outside yard irrigation valve where I didn’t know what the pulse per liter/gallon was. I kept testing with a 1 gallon jug until I got it reporting accurately. Amazingly, it resulted in using a value of 2775 before it was accurate as tested against actual measurements.

sensor:
  - platform: pulse_counter
    name: "Water Meter Flow Rate"
    id: water_meter_flow_rate
    pin: GPIO5
    update_interval: 5s
    filters:
     - lambda: return (x / 2775) * 60.0;
    unit_of_measurement: "gal/hr"
    accuracy_decimals: 1

Awesome, thank you very much for the insight!

Thanks for the code. I could switch the code but since this setup is referenced in the Home Assistant docs I find it strange it’s not working. Usually when blogs are mentioned in the docs they work flawless. Peter Brinkman (the author) does not have any comment options on his blog so twitter is the only option to contact him. I’ll reach out and ask him to have a look. Perhaps things have changed in HA which makes his code need some work. If that doesn’t work out I’ll have a try with this setup!

@EvilUnicorn134 I tried to follow the same blog post and got the similar result as you - the water usage cards for various time intervals shows 0. Did you get your setup working?

No, unfortunately I did not. I reached out on Twitter but haven’t heard back. The sensor is fine so I guess it can be used with other integrations. I’ll give it a bit more time but if I get no response I’m going to try that. If nothing changes the reference to the blog should be removed from HA documentation.

@mightybosstone Thank you for sharing your code. I’m using an EKM water meter that has the same pulse per 0.0748 gallons calibration.

I’m still struggling to understand the ESP code. In the Pieter Brinkman example, there are two pulse_meter sensors and a pulse_counter sensor. The code you shared just as one pulse_counter sensor. Is that all that I would need or are there additional water meter sensors in your code that weren’t included in your snippet?

I just replaced the pulse_counter and pulse_meter sensors from the Brinkman example with the @mightybosstone pulse_counter sensor and without doing anything else, my util_water_usage (hourly, daily and monthly) sensors in HA now show water usage for the first time. I have not made any other changes to my set up yet. Thanks @mightybosstone

So you didn’t use any of the yaml from Step 5 of Brinkman, and just added the two pulse_counter sensors from above to your esphome?

@tc23 I replaced the code from Brinkman’s step 5 with the code below. Perhaps I didn’t implement Brinkman’s code correctly (I was trying to convert the pulses/minute to ft3/hr and gallons/hr). But with the Brinkman code, the utility meter mentioned in his step 6 that tracks hourly, daily and monthly usage always showed 0. I don’t think I was ever able to get step 7 to work.

Also, I’m still struggling to understand why there are 2 pulse_meter sensors and a pulse_counter sensor in the Brinkman code. Can anyone help me understand the purpose for having those three sensors in the code?

The code from @mightybosstone with 1 pulse_counter sensor makes sense to me.

Anyway, here is the code that I’m currently using:

  - platform: pulse_counter
    name: "Water Rate"
    id: water_rate
    pin: 
      number: GPIO4
      mode:
        input: true
        pullup: true
    update_interval: 10s
    count_mode:
      rising_edge: DISABLE
      falling_edge: INCREMENT
    unit_of_measurement: "gal/hr"
    accuracy_decimals: 3
    filters:
     - debounce: 1.0s
     - lambda: return (x / 13.36898395721925) * 60;  
    total:
      name: "Water Total"
      unit_of_measurement: "gal"
      id: water_total
      accuracy_decimals: 3
      device_class: water
      state_class: total_increasing
      filters:
        - multiply: 0.0748 

Here is a screen shot of my Lovelace graphs that show non-zero water usage (with Brinkman, these graphs always showed 0). I still need to compare the readings in HA with the readings on the face of the actual water meter. Given that my water meter is under the house in the crawl space, I have not been quick to check the actual meter.

I filled a one gallon milk jug to confirm the accuracy, and seems to be spot-on. But I still think there’s a problem with excessively high usage showing up occasionally.

The ESP code produces 2 sensors: One for the flow rate and one for the water total. The water total seems to be accurate over time. But the flow rate sometimes reports short bursts (for a few seconds) of huge flow rates that are simply not possible for a shower or faucet. On the chart below there’s a burst showing almost 20,000 gallons per hour!

I have no idea what’s causing this. I figured that since the Energy Dashboard is using just the total there’s no need for concern - perhaps those high flow bursts are so short that they don’t change the overall usage reported.

However…
I also occasionally see a large amount of water usage showing in the Energy Dashboard, and those times match with the time of the excessive flow rate and the gallons used directly from the ESP water total sensor. So while this seems to work well most of the time, there’s definitely something wrong that’s occasionally causing high pulses and over-reporting water usage. Yesterday I changed the power supply to an iPhone charger plugged into a surge protected UPS and it’s still happening, so I don’t think it’s a power issue. My guess at this point is that it’s either an issue with the internal pull-up or the debounce. I’m going to try increasing that debounce value to see if it has any effect.

Thanks for sharing. I suspect that I’m having the opposite problem. The water usage HA is showing seems to be too low. Yesterday did a quick test. I took a picture of the face of my physical water meter and noted what HA indicated as my daily water usage up to that point. About an hour later I took another picture of the face of the physical meter. That meter indicated that I used about 5 ft3 of water which is about 37.4 gallons. HA showed that my daily water usage over that time only increased by about 1.1 gallons.

I wonder if the debounce filter setting is causing me to miss valid pulses from my meter. Why would we need to use the debounce filter?

Debounce is to remove any additional noise around the pulse. Some relays do not produce a clean pulse. They “bounce”. And therefore to avoid an additional pulse being accidently recorded. It is usual to set the timing so that it would only ever pickup short pulses. So what is the fastest pulse per second you would ever get when all the taps are open? Check and adjust.

Thank you for helping me understand. I believe my meter is measuring the water coming into the house from our well. I’ve been told that the well produces at 18 gallons per minute. The specs of the meter state it produces a pulse every 1/10th of a cubic foot of water. At 18 gallons per minute, that translates to about 24 pulses per minute or 1 pulse every 2.5 seconds.

I need to run a number of tests as something seems to be wrong.

Where do you find the pulse info? I have an invensys 5/8 meter and can’t find any relevant info on it

There should be a sheet that lists your meter’s specifications. You might be be able to find it by searching the internet for your meter’s name, model number and the word “specifications”.

1 Like

It looks like I found the source of my HA water meter readings being too low. Unlike @mightybosstone who seems to have a meter with 10x the resolution of mine, my meter sends a pulse every 0.748 gallons, not ever 0.0748 gallons. I need to adjust the conversion factor in the lambda function of the code for my water meter.

Hmm, I’m not getting any pulses. Two questions, did you keep the same wiring diagram as the original blog post? And also, I have a plexiglass cap on my meter which stops me from getting right up against it. Is this similar for everyone?

I managed to fix this. It’s because the same pin is defined twice for two sensors. Only the latter sensor definition will work. platform: copy is the solution, but a template sensor should also work. See two sensors with same input not work with latest firmware · Issue #3364 · esphome/issues · GitHub.

I also cleaned up the code a bit, I can now see both usage in litres and meter reading in cubic meters:

sensor:
  - platform: pulse_counter
    pin: GPIO14
    id: water_flow_rate
    name: "Water Flow Rate"
    update_interval : 60s
    accuracy_decimals: 1
    unit_of_measurement: "l/min"
    icon: "mdi:water"

  - platform: pulse_meter
    pin: GPIO14
    internal: true
    id: water_pulse_meter
    total:
      id: water_usage_total
      icon: "mdi:water"
      name: "Water Usage Total"
      unit_of_measurement: "litres"
      accuracy_decimals: 0
      device_class: water
      state_class: total_increasing

  - platform: copy
    source_id: water_usage_total
    id: water_meter_total
    name: "Water Meter Total"
    icon: "mdi:water"
    unit_of_measurement: "m³"
    accuracy_decimals: 3
    device_class: water
    state_class: total_increasing
    filters:
      - multiply: 0.001
2 Likes