Raspberry Pi Home Assistant Hassbian Camera Motion Mqtt

Usb Camera as a Motion sensor on Raspberry pi 3 Hassbian using Mosquitto MQTT broker/client 

Using "motion"(software) the images and Videos from the Usb webcam are recorded and streamed on the (same)Rpi3 which is also running Hassbian. 


I have edited the motion.conf file to run commands when motion is detected and when the motion event ends so that it publishes Mqtt messages to a Topic using Mosquitto MQTT broker and in the Homeassistant configuration.yaml file i have made an mqtt sensor that shows motion detected TRUE or FALSE .


It works and using Automations  it can be used to send mqtt messages to some other mqtt switches connected to the Hassbian to turn lights on or off or do something else.


#######################################

# In motion.conf file edit the following lines (in addition to the settings that are necessary for 
# your camera to work 
#######################################

# Command to be executed when an event starts. (default:none)


on_event_start mosquitto_pub -u newuser -P 12345 -t /house/system/motion -m "TRUE"


# Command to be executed when an event ends after a period of no motion


on_event_end mosquitto_pub -u newuser -P 12345 -t /house/system/motion -m "FALSE"


# Command to be executed when a motion frame is detected 


on_motion_detected mosquitto_pub -u newuser -P 12345 -t /house/system/motion -m "TRUE"


# the commands should also contain an ip number and a port number for connecting with 

# mosquitto broker if it is running on a different device within a network

###########


#########################

In Configuration.YAML file
#########################


camera:



####


  - platform: mjpeg    ## camera is on same device i.e. localhost

    mjpeg_url: http://localhost:8081
    name: Usb Cam

####

  - platform: mjpeg        ## camera is a different device
    mjpeg_url: http://192.168.1.7:8081
    name: RCam1
    username: user
    password: qwerty12345
    authentication: basic

######################

# MOSQUITTO MQTT 
######################


mqtt:

  broker: localhost
  port: 1883
  client_id: home-assistant-1
  keepalive: 60
  username: newuser
  password: 12345
  protocol: 3.1 

###############

# SENSORS 
###############
sensor 1:
  - platform: mqtt
    name: "Motion Detect"
    state_topic: "/house/system/motion"
  #  payload_on: "TRUE"
  # payload_off: "FALSE"
    qos: 0
    expire_after: 60
####################
sensor 6:
  - platform: mqtt
    name: "RCam1 Motion"
    state_topic: "/house/rcam1/motion"
 #   payload_on: "TRUE"
 #   payload_off: "FALSE"
    qos: 0
    expire_after: 60
####################

############################################


##################

In groups.YAML file
##################
default_view:
  view: true
  icon: mdi:home
  entities:
  #- group.time
  #- group.sun
  #- group.weathertoday
  - group.usbcam
  #- group.systeminfo
  name: default_view

###

usbcam:
  name: Usb Cam
  view: false
  entities:
  - camera.usb_cam
  - sensor.motion_detect

####

testview:
  name: TestView
  view: true
  entities:
  - group.rcam1

##########

rcam1:
  name: RCam1
  view: false
  entities: 
  - sensor.rcam1_motion
  - camera.rcam1




################ HOW TO ##################



First, make the camera work and configure motion by editing motion.conf file.Keep the resolution and frame rate at a minimum, for example : height x width 640 x 480 and 10 fps.

You need to make motion and your camera work with each other first so you can view the camera feed using the ip address of your Pi and a port at which motion is streaming the video from your camera.


Then add camera component and all the related settings in configuration.yaml file to show the camera on the homeassistant page. 

In the example above I have shown two ways to do it. 


First is for a USB cam attached directly to the Pi which is Running Homeassistant.


Second is for a different Pi with Raspbian Jessie,a CSI camera, and running motion and mosquitto and sends video and mqtt messages to the first Pi which is running Homeassistant/Hassbian.


Then use sensor component and make a "Sensor" by adding text in the Configuration.yaml file make sure the text has proper "spaces" because it will not work if the text is not formatted according to the YAML syntax. In the example above I have made Two sensors for motion detection from two camera feeds.


First is from the Usb cam and motion running on the same Pi where Hassbian is running.Second is for a different Pi with a CSI camera in the same Wifi network.They should have different mqtt "Topics"and the Payloads can be either "1" and "0" or "true" and "false".


Then make a "card" to show on the HomeAssistant page that can show camera feed and  motion detect = True / False  by adding text to the groups.yaml file. there are two ways to do it. 


First, is by adding it as a new tab by making two groups. First group with "view" set to "true", like in the example above the "Test View" in groups.yaml file . Then make a second group and set its view to "false"and add camera and sensor entity names to the second group (with view false),like in the example above the RCam1 group.Then add the second group's name to the first group (with view set  to true). Like I did above in the TestView entities section.If you do it like I did a new "TAB" will appear in the HomeAssistant webpage with the name Test View and it will show both the rcam1 video feed  and it's motion detection sensor using mqtt.


Second is by making a group with view set to false and add usb camera and motion sensor entity names to it then adding that group name to the default view entities section will show the video feed from usb cam and its motion sensor on the main home tab on the homeassistant page. 


Then test the Commands for Publishing a Message to the mqtt topics from command line and at the same time watch the changes on the motion detect cards on the homeassistant page. 

If the command is correct with correct topic,username,password and payload (TRUE/FALSE) and if on a different device with correct ip and port in the command too. (ip and port where the mosquitto broker is running) then you should be able to manually publish to the sensor topic and the motion detect status will change to TRUE or FALSE and in case no new message after expiry time is up it will change to UNKNOWN.  


Then add those tested/working commands for mosquitto_pub to the motion.conf file to automate the process of publishing messages to the mqtt topic as a result of motion events.


Please Keep it in mind that if this is being done on the same Raspberry Pi running Home Assistant,Motion and Mosquitto the Mosquitto commands that are executed on motion detection do not contain an ip and a port number for the mosquitto broker but will be necessary for communicating with the broker running on a different device/pi in a network.


Which means if you have two raspberry Pis FIRST with HomeAssistant and Mosquitto Broker and the SECOND with usb/csi Camera , Motion and Mosquitto Client then the SECOND Rpi with camera and motion has to know the Ip and port for the FIRST Rpi so it can communicate with it and send a message upon motion detection. 


If you are planing to use a second Pi as a camera then MotionEyeOS will not work as mqtt or Mosquitto cannot be installed on it at least not easily you will have to build it from source and add all the necessary packages to the image .Use Raspbian with MotionEye and Mosquitto I have used it and it works.



If anything is still not clear then Comment on my Youtube Channel and Please SUBSCRIBE to the CHANNEL.