Using ESPhome to build a water flow rate meter

That’s exactly what I did, see the quoted yaml config. Works great!

1 Like

Thanks, it really inspires me try as well

1 Like

Hi DeeBeeKay, How did you setup it up with d1 mini?

Hi @saboaua, are you asking to see my complete yaml file, or do you want pictures of the circuit?

Yes, if you can share some pic of the project and yaml file. I’m looking to implement something similar and found your topic.

Luckily I have the board in front of me (it’s going back in the enclosure later this morning) so I was able to snap a picture.

As you can see, not a lot exciting going on. On the right, AC power coming in. I put it through a large MOV, and then a PCB-mount 220V-5V adapter. Then a small MOV (because I am paranoid), and a 4700uF capacitor. All of that is just to clean the power supply and protect the D1 from spikes.

Then I just have the D1 Mini mounted on some female pin headers, and on the other side of the board, I soldered some traces. On the left side, the three connectors are for the flowmeter, and the trigger and echo pins of the ultrasonic I’m using to measure how much water is in the reservoir.

The resistor you see on the left joins to the flowmeter’s data line before it hits the D1’s input pin. It’s a 10kohm resistor, coming from 5V+. I am not an electronics genius, I saw this in a YouTube tutorial for working with this flowmeter, and they didn’t explain it, but if I have to guess what it does, I think it effectively boosts the signal that the flowmeter is putting out.

Here is the full yaml file:

esphome:
  name: d1m001_reservoir
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: "xxxxxxxx"
  password: "xxxxxxxxxxxxx"

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

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"  
      
  - platform: ultrasonic
    name: "Reservoir level"  
    trigger_pin: D3
    echo_pin: D4
    update_interval: 60s
    filters:
    - lambda: return 200000 - ((x / 2.1) * 200000.0);
    unit_of_measurement: "L"

As you can see, I am using filters to format the data into usable form. ESPHome reports the flowmeter’s speed in revolutions per minute, and I know that the flowmeter turns 27 revolutions per litre. Therefore the formula is (x/27)*60, which gives the speed in Litres per hour.

Then for ultrasonic, I really want to do the formula better, but, basically, I know the capacity of the reservoir is 200,000L, and it’s 2,1m deep. The ultrasonic is returning a measurement in metres, so I divide by 2.1 to get percentage, and multiply by 200,000. This effectively shows how much water would still be needed to fill the reservoir. To get the actual amount, therefore, I just subtract the result from 200,000. It works, but I wish I could make that calculation a little more elegant.

Anyway I hope that helps you.

3 Likes
200000.0 * (1.0 - (x/2.1))

But that is really the same number of calculations

1 Like

or better

200000.0 - (95238.0 * x)

which is just simplifying 200000/2.1 = 95328

1 Like

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