Command Line vs MQTT for binary sensor

Hi!

So, I have a Raspberry Pi Zero motion sensor/camera setup in a room and am trying to decide the best option for setting up the motion sensor portion to interface with Home Assistant. I have the motion sensor working/confirmed working with a Python script on the Pi that spits out “MOTION DETECTED!!” when motion is detected. I figure I could do something like:
if motion:
echo 1 > /tmp/motion
else:
echo 0 > /tmp/motion

And NFS mount my /tmp directory on my Home Assistant server. Then use a Command Line Binary Sensor to check the output of motion to determine if there was motion or not. Seems easier than setting up an MQTT client, but I’m not sure which option is more efficient/responsive? Any thoughts?

Binary Sensor Command Line Component

Thanks!
-Chad

Hi @ChadCurvin, i think this will be to slow for motion detection.
The command line sensor gets updated on an intervall, and so there can be few seconds.
IMO publish a payload to MQTT and let HA trigger on that is the best you can do.

1 Like

I agree with @VDRainer, in this case MQTT is the way to go. There are plenty of examples of using python and MQTT on the forum, like this

1 Like

and MQTT will be useful for plenty of other things so it’s worth the (easy) setup IMO.

1 Like

Thanks for the feedback! I ended up going down the MQTT route. I already had the broker setup (I have a SmartThings Hub) so that link above helped a ton!

Just for future sake, below is what’s working for me. Thanks again!

-Chad

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import sys
import paho.mqtt.publish as publish

DELAY = 3

# Setup the mode and pin assignment, physical pin 26
GPIO.setmode(GPIO.BCM)
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)

def INACTIVE():
        print "Motion Inactive"
        publish.single("mb/eastWall/motion", "inactive", retain=True, hostname="homeserver")

def MOTION(PIR_PIN):
        print "Motion Detected!"
        publish.single("mb/eastWall/motion", "active", retain=True, hostname="homeserver")
        # A timer to delay the reset and call INACTIVE()
        COUNTER = 10
        while COUNTER >= 0:
                time.sleep(1)
                print COUNTER
                if COUNTER < 1:
                        print "Calling Inactive"
                        INACTIVE()
                COUNTER -= 1 

print "PIR Module Test (CTRL+C to exit)"
time.sleep(DELAY)
print "Ready"

try:
        GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)
        while 1:
                time.sleep(1000)
                                
except KeyboardInterrupt:
        print " Quit"
        GPIO.cleanup()

Update to make it work slightly better. The timer will now reset if it “resenses” motion before the timer expires.

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import sys
import paho.mqtt.publish as publish

DELAY = 3

# Setup the mode and pin assignment, physical pin 26
GPIO.setmode(GPIO.BCM)
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)

def INACTIVE():
        print "Motion Inactive"
        publish.single("mb/eastWall/motion", "inactive", retain=True, hostname="homeserver")

def MOTION(PIR_PIN):
        print "Motion Detected!"
        publish.single("mb/eastWall/motion", "active", retain=True, hostname="homeserver")

        # A timer to delay the reset and call INACTIVE()
        COUNTER = 8
        while COUNTER >= 0:
                time.sleep(1)
                print COUNTER
                # If motion is detected again while timer is counting down, reset timer
                if GPIO.input(PIR_PIN):
                        COUNTER = 9

                # Once timer reaches zero, call inactive
                if COUNTER < 1:
                        print "Calling Inactive"
                        INACTIVE()
                COUNTER -= 1

print "PIR Module Test (CTRL+C to exit)"
time.sleep(DELAY)
print "Ready"

try:
        GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)

        while 1:
                time.sleep(100)

except KeyboardInterrupt:
        print " Quit"
        GPIO.cleanup()
2 Likes