Using ESPhome to build a water flow rate meter

True, but, yours is much more beautiful and clean.

Much better! But oddly, not as beautiful as the previous one :rofl:

Thank you for your contribution, I feel much better about it now.

Thanks for sharing your project. I also noticed some setup has the resistor, but as you mentioned, there’s not allot details on the setup.

Will try it out!

Perhaps you are after a circuit diagram?

I’m using this flow meter which includes a resistor on the unit which, I believe, makes it unnecessary to put one on the board.

This is more of a presumption on my part as my electronics knowledge is far from great.

The unit is plastic. I haven’t been able to find a brass unit that uses npt fittings (npt is the US plumbing standard for threaded fittings). The rest of the world seems to use the British “g” standard and brass “g” to npt adapters, unfortunately, cost more than flow meters.

So, a small update on this. I finally got it deployed in the wild; it’s about 60 metres from the house in a box, and working.

However.

The numbers its kicking back seem to be excessively high. Like, when I measure by catching the water in a 20L jug and timing how long that takes, it seems to be very, very wrong. Like, my shitty jug measurement gives me about 900L/hr. The flow meter says 3,800L/hr.

Is there some sort of calibration thingy that I ought to be doing?

Annnnnd scratch that. Settled down, and now my ghetto jug measurements are within error bars of the digital. No idea what that was all about, but, it’s now working as expected. Yay!

1 Like

@DeeBeeKay connected my today and it’s working, thanks. Did you tried to have it counting the liters used per day?

I used this Uxcell G1/2" Thread Hall Effect Liquid Water Flow Sensor Switch Flowmeter for this setup

Hi @saboaua, that would be hard for me because I don’t have a flow meter on the outlet - so I can’t just count it.

I suppose it could be calculated, but it would be hard to do in ESPHome itself. I would prefer Node-RED or something similar.

If you take a reading of the reservoir level at the beginning of the day (x), and then add how much you pumped in during the day (y), then measure the reservoir actual at the end of the day (z), then (x+y)-z should give you the total litres you went through. I think. I am not clever.

But I can’t even do that because I have an old diesel pump down by the spring that doesn’t have a flow meter, so it would throw off my calculation.

I was trying to find a way to have it done thru esphome itself. So have to look and try with Node-RED or something similar. As my setup will include a NO valve to shut off the water, if it notice a waterleak for x amount of time.

You work has got me started on the project, thanks.

1 Like

@saboaua That’s very interesting! Controlling for leaks is a great idea.

ESPHome can have lambdas, so, it should actually be possible to do it if you can write the code in C++. My coding ability is very limited. I see the difficulty more as being that you might practically want more advanced responses to the actual problem than just shutting off the water.

I’ll give you an example of what I mean. I’m using ESPHome to actually control the borehole pump itself, using a relay to switch a 26A contactor which in turn switches on the heavy load of the pump. The ESPHome device that controls the pump and the one that monitors the flow are half a kilometer apart, so it’s not practical to have them work off the same board, but they are on the same network.

Node-RED controls the schedule. It turns the pump on, and then it monitors the flow of water. If the flow rate begins to drop suddenly, it means that the water level in the borehole has dropped below the pump intake, and we are now sucking (some) air. That’s bad for the pump (and also sad because we have so little water), so Node-RED shuts the pump down immediately. But then it sends me an email to let me know there’s a problem. My next goal is to have Node-Red tweak the pump run and rest times in response to these problems, so that it can run the pump a little less next time, or let it rest a little longer.

I think you need something similar. Checking for leak condition is a great idea! but you also need a response to that situation that makes you aware there’s an issue - like with a push notification, or a flashing light, or whatever. Otherwise, you will only know there is a leak when you notice that your automatic system (which you designed so you can ignore it) has stopped pumping. Just a thought.

Please continue to post here as you develop your system. I’m glad I got you started, but you also gave me a great idea about detecting leaks. If we keep sharing our ideas, our systems will be amazing.

Ok, I have implemented the valve to my water line coming to the house. The only issue i have now, is to show it correctly in home assistant and getting it working correctly with ESPHOME.

I got only the toggle option working, but as i turn it on the toggle will switch to on and then off. But the valve is still close.

Any ideas? i tried the switch.turn.on option and the toggle in home assistant will do the same, but the relay will stay on. But as i click the toggle to turn off the relay, nothing happends to the relay and stays on.

switch:

  • platform: gpio
    pin: GPIO05 #D1
    id: relay
    restore_mode: ALWAYS_OFF

  • platform: template
    name: Water Valve Control
    icon: mdi:valve-closed
    turn_on_action:

    • switch.toggle: relay
      turn_off_action:
    • switch.toggle: relay

You’re over complicating it.
Remove the template switch and put the name and icon to the GPIO one

Note that esphome does not support per state icon

I did it before, but it didn’t remember the state. As this morning i change it back to the simple switch and it remembers the state.

switch:

  • platform: gpio
    pin: GPIO05 #D1
    id: relay
    name: “Water Valve Control”
    icon: “mdi:valve-closed”
    restore_mode: ALWAYS_OFF

Thanks.

1 Like

It took me a while to figure out the calculations. So I will write here for others to use. Hopefully I am not wrong. First thing is to check your water flow sensor for the Flow Pulse. In DeeBeeKay’s case it was 0.45. I use a a YF-21 model so I have 0.7 from this datasheet.
This is a constant set in the datasheet of the sensor that tells us the relation between flowrate (Q) and frequency aka pulses per second the sensor is sending (Hz). Check this formula written in the DeeBeeKay’s datasheet (Flow Pulse: F(Hz)=(0.45xQ) +/-3% Q=L/min). That also means that F(Hz) / 0.45 = Q (L/m). So if we have 10 Hz (pulses per second) we get 10 / 0.45 = 22.22 (L/m). Or is it L/s?

This is the only thing bothering me right now. That F(Hz) is measured in seconds as in pulses per second and Q is said to be in minutes as in Litres per minute. So how is it?

This? Option A

filters:
    - lambda: return (x / 0.45) * 60.0 * 60.0;
    unit_of_measurement: "L/hr"

(Pulses per sec divided by 0.45 to get L/sec, multiplied by 60 seconds to get L/minute, and multiplied again by 60 minutes to get L/hour. I let you see all the calculations so you understand what is going on. For example 0.45 * 60 = 27 :wink: )

Or this? Option B

filters:
    - lambda: return (x / 0.45) * 60.0;
    unit_of_measurement: "L/hr"

(Pulses per sec divided by 0.45 to get L/min !!!, multiplied by 60 minutes to get L/hour.)

BTW, in ESPHome the pulse counter sensor defaults to measuring its values using a unit of measurement of “pulses/min”.

So DeeBeeKay, are you sure your calculations are now off by 60? :slight_smile:

Let me know if and what I did wrong and if you are getting good results from your setup.
Thank you!
ygreq

1 Like

I am a little bit … no, I am a lot confused by what you have written. You appear to be trying to re-do the work that ESPHome is already doing.

ESPHome measures the rate of pulses by itself and reports this as Pulses Per Minute. You don’t have to write any code for that. ESPHome just does it for you.

Converting that data into Litres per Minute or per hour depends on how many times your specific sensor will spin per litre. For my sensor, I know that 27 pulses = 1L of water. So I divide PPM by 27, and that gives me litres per minute. But I want to know the rate per hour, because that is more useful to me, and because there are 60 minutes in an hour, I multiply L per min by 60.

I can do this because the datasheet for my sensor explicitly states that 27 pulses = 1L. The datasheet for your sensor does not explicitly state how many pulses equate to 1L of water. However, it seems that your sensor is also called YF-S201 (based on the file name of the PDF you linked to). Some googling reveals that YF-S201 is quite a small sensor, and turns 450 times for 1L.

So for you, the filter looks like this:

    filters:
    - lambda: return (x / 450.0) * 60.0;

That’s it. You don’t need to do anything else.

2 Likes

Thank you for your reply, DeeBeeKay! But can you explain this formula? It’s for your sensor. The previous message is based on this

And based on that formula, it makes sense for your sensor. Because ESPHome gives you F(Hz) as PPS and you know the constant (0.45). And if you multiply the constant to 60 sec, it actually gives you 27 like you already know and have used in your calculations ( 1L water= 27Pulse).

I am curious where you found info for my sensor as I couldn’t find how many pulses for 1 litre. Can you share the link? Because otherwise, the same formula will get me somewhere else completely for my sensor. The formula for my sensor is F(Hz)=(0.7xQ) +/-5% Q=L/min. That would mean:

filters:
     - lambda: return (x / 42.0) * 60.0;

(meaning 0.7 * 60sec = 42)

Thank you!

1 Like

And btw, I didn’t write code for that. Indeed I made you really confused. :smiley:

Life, the Universe and Everything!

1 Like

Hi @DeeBeeKay,

Thank you, this is a great tread!
I use the YF-B10 and I have got a problem with the datasheet I found.
http://www.sunwoald.com/en/productshow.asp?ArticleID=R8S7WRQTY5
It does not give the number of pulses for one liter, it gives only the formula between F and Q.
F=(6*Q-8)
What surprises me is the 8. It means that when there is no water flow, there is still a pulse!?
I did the experimentation by typing this inton ESPhome:

  - platform: pulse_counter
    name: "Flow rate 1"
    pin: D5
    update_interval: 60s
    filters:
    - lambda: return (( x + 480 ) / 360) * 60; #Flow pulse: F=(6*Q-8)±3% with Q=L/min
    unit_of_measurement: "L/h" 
    id: flow_rate_1

So of course, when putting no water in the sensor, I can read 80 L/h.
Any clue?

1 Like

Surely the formula is

((x+8)/6)*60

Which can be reduced further to

((x+8)*10