Using ESPhome to build a water flow rate meter

thanks , do i need any other device to buy ?

I have a tip for anyone doing this.

Most of my flow meters are either YF-DN40 or YF-DN50. Where I live, these are crazy expensive. But I discovered something today.

They all work on the same principle - either a propeller with a magnet in one or more blades, or a propeller made of ferrous material. And however the internals look, they all use the same hall sensor, on the same board. If you pop open one of these devices, you will find a circular board with the code WT8-Y printed on it.

Why is that useful? Well, I’ve had these deployed in the wild for a year now, and a few weeks ago I had my first failure, on a big DN50 which measures the rate water is coming from a dam about a kilometre down the mountain. Replacing the sensor would be a big job, so I tried everything else first - making sure the power supply was good, replacing the sensor cables, etc etc. Then I opened the sensor and saw that the gasket had failed and rain had penetrated inside and wet the board.

Which is also when I noticed that the board is standard. Before I deployed across the farm, I did experiments on my workbench with a small version of the sensor, and I remembered what it looked like inside. I scratched in a drawer and found the sensor, and it was a match. Swapping out the board and putting in a new gasket was child’s play.

So what I’m saying is, if you deploy big like I did, and one of your flow rate sensors fails, buy the smallest, cheapest flow meter you can find and cannibalise the board. ESPHome does not care, and why should it? It’s the exact same part, the only difference between the big and small sensors is the number of turns that equals a litre, and that’s a setting you do in ESPHome anyway.

Anyway, that’s all. Hope this tip saves you some money. It’s a lot easier swapping a circuit board than replacing an inline water flow sensor.

9 Likes

Morning, guys

So I’m trying to calculate my daily usage using utility meter in Home Assistant, would I need to create 2 sensors to display L/hr and L/min or is there a better way to get this working?

Water Flow Monitor

sensor:

  • platform: pulse_counter
    pin: GPIO5
    name: “Pulse Counter”
    update_interval: 5s
    filters:
    • lambda: return (x / 450.0) * 60.0;
      unit_of_measurement: “L/hr”

Daily Water Flow Monitor

  • platform: pulse_counter
    pin: GPIO5
    name: “Daily Water Counter”
    update_interval: 1s
    filters:
    • lambda: return x / 450.0;
      unit_of_measurement: “L/min”

I can’t see why you couldn’t use both in one yaml. Have you tried to yet? Seems a pretty good way to get the data in different formats. I mean the only other thing I can think of is bringing Node-RED into it, which seems overkill for this.

Which utility meter are you using?

You could also use a Template sensor if the dual one gives you gyp.

1 Like

@pmiles156, I dug into this myself, and found that you do the config on the Home Assistant side. On the ESPHome side, you only need the sensor that returns in L/Hr.

Here’s what you need to do.

First, edit your ESPHome node YAML so that you have just the sensor that returns L/Hr.

Then, go to HA and edit your config.yaml file. You must add a Riemann sum integral sensor, see here: https://www.home-assistant.io/integrations/integration/

Mine looks like this:

sensor:
  - platform: integration
    source: sensor.flow_rate_southgate
    name: RSI Southgate

Now, I should mention that the default time unit is per hour, that’s why you could leave the ESPHome sensor returning per hour. If you have ESPHome returning something else, you’ll need to add the relevant unit_time argument here.

Next, you must add a Utility Meter in your config.yaml. Mine looks like this:

utility_meter:
  southgate_flow_hourly:
    source: sensor.rsi_southgate
    cycle: hourly
  southgate_flow_daily:
    source: sensor.rsi_southgate
    cycle: daily
  southgate_flow_weekly:
    source: sensor.rsi_southgate
    cycle: weekly
  southgate_flow_monthly:
    source: sensor.rsi_southgate
    cycle: monthly

So you see I have added hourly, daily, weekly, and monthly. This is likely overkill for you, and probably for me too, but I am super stoked to get it working. Note that I am using the RSI sensor we created in the previous step as the source, not the ESPHome sensor.

And that should be that.

6 Likes

This is excellent. It’s been a puzzle to me for a while.

On the ESPHome side, are you using:

  - platform: gpio
    pin: 
      number: GPIO34
      mode: INPUT_PULLUP
    name: "Water meter sensor"
    id: water_meter_sensor

or:

  - platform: pulse_counter
    pin: GPIO34
    name: "Water meter"
    id: water_meter_pulse_counter
    accuracy_decimals: 0
    update_interval: 10s
    unit_of_measurement: 'litres'

as the input to HA sensor.flow_rate_southgate?

If the latter, what do you have your update_interval set at? Does it make a difference?

1 Like

Hi @ashscott, my flow meter YAML looks like this:

sensor:
  - platform: pulse_counter
    name: "Flow rate"
    pin: GPIO5
    update_interval: 60s
    filters:
    - lambda: return (x / 27.0) * 60.0;
    unit_of_measurement: "L/hr" 

Or at least, my original did. I am currently updating at 20s intervals, only because I also use this data to detect air in the pipe (ie borehole empty). It does not make any difference to HA or to the RSI sensor.

I have to point out, your config is missing a very important part: the number of pulses that equals 1L. You can find this on the spec sheet of your sensor, but it is very unlikely to be 1. In my example, my sensor pulses 27 times for 1L of water (I am using a YF-DN40 water meter).

Thank you for that.

My sensor is, indeed, 1 pulse per litre, which keeps things simple.
image

Ye gods and little boarlets! That’s amazing!

1 Like

How have you configured sensor.flow_rate_southgate?

I’m getting loads of errors.

I can’t follow on one thing. I installed a YF-B10(https://www.aliexpress.com/item/4000133724195.html?spm=a2g0s.9042311.0.0.68674c4dNzqD31) using the following config:

  - platform: pulse_counter
    name: "Flow rate"
    pin: 26
    update_interval: 10s
    filters:
      - lambda: return (x+8)*10; #Flow pulse: F=(6*Q-8)±3% with Q=L/min
    unit_of_measurement: "L/hr" 
  - platform: pulse_counter
    name: "Flow rate Minute"
    pin: 26
    update_interval: 10s
    filters:
      - lambda: return (x+8)/6; #Flow pulse: F=(6*Q-8)±3% with Q=L/min
    unit_of_measurement: "L/min" 

In the log when there is no water flow I get:

[D][pulse_counter:159]: 'Flow rate Minute': Retrieved counter: 0.00 pulses/mine
[D][sensor:092]: 'Flow rate Minute': Sending state 1.33333 L/min with 2 decimals of accuracye
[D][pulse_counter:159]: 'Flow rate': Retrieved counter: 0.00 pulses/mine
[D][sensor:092]: 'Flow rate': Sending state 80.00000 L/hr with 2 decimals of accuracye

If no pulse are retrieved I would expect to send a state of 0 instead of 1.33 L/min or 80 L/hr.

I also noticed that the flow rate is incorrect when letting it run, When I do a test it does 22 seconds for a liter.

Can some one help me on this?

1 Like

@phdelodder Here’s the problem. ESPHome does most of the heavy lifting, and already returns the flow rate in pulses per minute, without you having to use a formula at all. Your problem is that you are trying to apply the wrong formula to that output.

I mean, in fairness, it’s the right formula, if you were coding this raw in Arduino IDE. But all you really need to know is how many pulses = 1L. For example, my lambda looks like this:

  - lambda: return (x / 27.0) * 60.0;

It looks like that because the spec sheet for my sensor says that that it’s 27 pulses per litre, and I then turn that into litres per hour by multiplying by 60.

You need to google what the pulse rate is for your sensor. And it will be a gross, frustrating google because I just tried it and I could only find the formula you’re using, not a practical number.

Otherwise, failing that, you’re going to need to write an arduino sketch to count pulses, and pour a litre jug through the sensor a few times, then average your result. Many people feel that this approach is best for accuracy, but I would still start by googling.

Hi @ashscott, what sort of errors are you getting. My sensor configuration looks like this:

  - platform: pulse_counter
    name: "Flow rate - Southgate"
    id: flow_southgate
    pin: D7
    update_interval: 20s
    filters:
    - lambda: return (x / 27.0) * 60.0;
    unit_of_measurement: "L/hr"

That’s literally it.

If you tell us more about your errors, or better yet, post the log output, we’ll be better able to help. Bear in mind that when you use Utility Meter, the output will always be wrong until the second cycle. So if you configure it hourly, it will only be accurate from hour 2. If you configure daily, it will only be accurate from the second day.

1 Like

Thanks for the explanation. I have done some digging and found that according to this thread on tasmota (https://github.com/arendst/Tasmota/issues/3390): it does 4500 pulses per 10L, so for 1 L it would be 450 pulses.

I’ll also do some test by tapping a 1L and see how many pulses I get. The meter is already installed.

I have tested it and it seems to correct/not correct it depends on the rate the water is flowing I have been spot on with 1L for 450 pulses. If I do other tests I also have 850 pulses for 0.5L.

Thank you.

That works. However, what I’m after is a totaliser over hour/day/week/month and HA utility_meter seems to be giving me flow rates over those periods.

Is that correct and is that how you are using it?

Hi @ashscott, Utility Meter is a totaliser, that’s why it changes as new values come in.

I have them in the front end as an Entity card, next to each other: Hourly, Daily, Weekly, Monthly. If you click on one, it pops up the UM card, where you can see what the previous total was.

Ok, Sorry I haven’t been back in touch. So Thank you so much for your time on this. I have updated my Home Assistant and I believe it is set up correctly but will need to do some more testing.

I will report back in a couple of days with my findings, is there a way to adjusting the output? instead of saying 222.659 L/hr just to say 222?

Thanks again for all your help

Whats a UM card and how have you setup the previous totals?

Right slept on it UM is Utility Meter :blush: