Last Watch is a standalone application for creating if-then automations based on AI object detection.
Last Watch comes with a few automations out of the box which allow you to do simple things like send a Telegram photo message if a person is detected on camera. The list of integrations continues to grow (MQTT is now available!), but the automation options are still very basic.
What if you want more advanced options, like turning on a sensor when a car is stationary, and a different sensor when a car is in motion? While you could create different Detection Profiles for each scenario, this can become very tedious with dozens of profiles to manage in LW. Instead, why not offload the automation logic to a real automation platform like Home Assistant or Node-RED?
As of version 0.8.0, Last Watch allows you to do just that. Using the MQTT automation in LW, you can publish entire detection events to be used in any platform you want. Node-RED is perfect in this case for managing automations and allows you to build new and interesting profiles that are not possible natively in Last Watch. In this post I will demonstrate how to set this up and begin building automations.
In this demo I am going to send events from Last Watch to Node-RED using MQTT. First, I need to define an Automation config in Last Watch for my MQTT topic.
Here I entered the IP of my Home Assistant server and the default MQTT port. The Topic and Client ID can be set to anything you want. Username and Password must be set unless you enabled Anonymous authentication in the MQTT add-on. Finally, make sure to not tick Custom Payload. We want Last Watch to automatically generate the event payload.
Now we can save the config and create a new Detection Profile to trigger the messages.
Since I am handling all of my automation in Node-RED, I'll create a profile to capture every type of object with a very low level of confidence.
Now I can save my profile and subscribe it to my MQTT automation. Now messages should start flowing to the broker.
Next, we will set up an MQTT listener in Node-RED.
I'll start by connecting an Mqtt-in node to a debug node and setting the topic to match the string configured in Last Watch. We also need a json node in the middle to de-serialize the payload into a usable object. Detection Events should begin recording in the debug window.
The payload has 3 properties. Two contain metadata about the event and the detection profile, and the third is an array of predictions. This array tells us what kinds of objects were detected, how confident the AI was, and whether or not the object was masked or filtered.
At this point we can begin building automations based on this message structure. I will use a split node to turn the prediction array into a stream of messages, and handle each prediction separately.
A switch node is used to split out different object classes so I have different paths for people, cars, trucks, etc. I am also using switch nodes to check if objects are masked or filtered (the smart filter flags objects which have not moved).
Now I can hook each path into whatever automation I want. I can trigger scripts or turn on sensors in Home Assistant, send push notifications, control smart home devices, etc. I could also add additional logic and filtering based on things like AI confidence, object size/position, proximity of objects or total number of objects. Some of this is possible natively in Last Watch, but it is much easier to manage and debug using the visual editor in Node-RED.