Using ESPhome to build a water flow rate meter

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:

The YF-B10 doesnā€™t have a constant pulse rate for the pulses per liters, so I have to find another approach then esphome

Why? Sensor Component ā€” ESPHome

Assuming that itā€™s not faulty and you donā€™t have any cavitation, with it being a turbine meter, each rotation of the turbine will generate a pulse from the hall effect sensor which will equate to a set
displacement of fluid. It has to be constant, or more correctly, consistent.

ESPhome will allow you to count the pulses correctly over time for rate, or as a totaliser.

If youā€™re not getting the readings or accuracy you expect then Iā€™d look at the possibility of cavitation or a faulty meter.

Another thought. Do you have it installed at the end of the line or mid line?

@Troon Iā€™m now using these code: https://github.com/mattbnz/smarthouse/blob/a6bf5d16d5499ff3652c152af391c12f94d64ee1/WaterFlowMeter/src/main.ino

@ashscott Itā€™s at the beginning just behind the filters, Iā€™m unable to determine the constant pulse per liter.

    // per datasheet; pulse characteristic (6*Q-8) Q=L/MinĀ±5%
    // aka pulses=6*L_per_min-8;
    // solved for L_per_min = 1/6*pulses + 4/3
    // except, if we didn't see any pulses, we don't add the constant factor
    // because it's not plausible that we're actually consistently consuming
    // 1.3L/min of water, which is what that would imply.

That data sheet spec (here) makes very little practical sense. The code youā€™re using does its best to produce an output aligned to it, but as the author points out, itā€™s not possible to measure less than 1.3L/min according to the spec. Iā€™d be inclined to take some empirical measurements at different flow rates and plug them into the polynomial filter I linked above.

To be fair, the sensor data also shows 1ā€“30L/min as its working range (and the label on the sensor says 2ā€“50L/min!)

However, if itā€™s working accurately enough for you, great! :slightly_smiling_face:

I have tested with 2 - 5 L tests and it works for my needs with that code. If I find the time Iā€™ll try a polynomial filter like you suggest

@DeeBeeKay great tinkering and useful outcome.