Skip to content

Instantly share code, notes, and snippets.

@alanb128
Last active May 17, 2018 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanb128/979c6b182116f05aa916dc2046f0af92 to your computer and use it in GitHub Desktop.
Save alanb128/979c6b182116f05aa916dc2046f0af92 to your computer and use it in GitHub Desktop.
Basic Raspberry Pi water meter pulse counting and MQTT push to Home Assistant
import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
global pcount # variable for counted pulses
pcount = 0
count1 = 0
client = mqtt.Client()
client.connect(192.168.1.100, 1883, 60) #IP address of your Home Assistant (HA)
client.loop_start()
def reportstatus(channel):
# will execute on every rising pulse
global pcount
pcount += 1
GPIO.add_event_detect(3, GPIO.RISING, callback=reportstatus, bouncetime=120)
while True:
# loop updates flow in HA every 3 seconds
count1 = pcount
count1 = count1 * 9.6 #convert pulses to ounces
pcount = 0
sleep(3)
# push the flow data to HA using MQTT
client.publish(topicname, round(float(count1)/3,0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment