I just thought I’d share this automation I have been working on as it took me a while to figure out. Might be useful to someone…
Back Story
A while ago I setup a door sensor on my front door. This is useful for a lot of automation tasks (such as lighting when we get home) but I also setup an automation so that we would get a notification (via Telegram) if the door was opened whilst neither of us were at home (Locative Tracking).
The issue I found was that very occasionally the automation would misfire due to the door opening before the location tracking had had a chance to catch up. This wasn’t so often as to make the automation useless but it did mean that the notifications were essentially ignored as we would both just assume it was just a misfire (not brilliant if there was a genuine intruder).
To fix this I purchased and setup a Raspberry Pi Zero W to act as an IP Camera. I used the official case and camera and it’s running MotionEyeOS. The idea being that when the door was opened (with no one at home) it could take some snaps of whoever came in and send them to us to verify.
Problem
I found with MotionEyeOS I could get a much better frame rate using the Fast Network Camera option. This unfortunately disables a lot of the OS’s great features such as motion tracking but for my use case this wasn’t a problem. What was a problem was that it also disables the URL to grab still images and leaves only the MJPEG stream up. If I wanted to send photos via Telegram then I needed a solution.
After a lot of Googling and playing around I eventually came across this solution:
automation:
- alias: frontdoor_snapshots
trigger:
platform: mqtt
topic: automation
payload: "frontdoor"
condition:
condition: and
conditions:
- condition: state
entity_id: device_tracker.b57a733cd0fc44957a733cd0fc4493894
state: 'not_home'
- condition: state
entity_id: device_tracker.97097be2b976ce28e8e442cd0fc449449
state: 'not_home'
action:
- service: shell_command.take_snapshots
- service: shell_command.rename_snapshots
- service: notify.telegram
data:
message: "Frontdoor Opened!"
- service: notify.telegram
data:
message: "Frontdoor Opened"
data:
photo:
- file: /home/pi/snapshots/snapshot1.jpg
caption: "Snapshot 1"
- file: /home/pi/snapshots/snapshot2.jpg
caption: "Snapshot 2"
- file: /home/pi/snapshots/snapshot3.jpg
caption: "Snapshot 3"
As you can see, the trigger is an MQTT message which is sent by the door sensor. The condition is that nether of us are at home. The actions are as follows:
The first fires a Shell Command which uses VLC to take 3 snapshots from the MJPEG stream:
shell_command:
take_snapshots: 'rm -f /home/pi/snapshots/* | vlc -q -I dummy http://192.168.0.11:8081 --video-filter=scene --vout=dummy --scene-format=jpg --scene-ratio=1 --scene-prefix=snapshot --scene-path=/home/pi/snapshots --run-time 5 --scene-ratio=40 vlc://quit'
The important bits in this command are:
rm -f /home/pi/snapshots/*: This clears the snapshots directory of all files (all old snapshots).
http://192.168.0.11:8081: The URL to my MJPEG stream
–scene-path=/home/pi/snapshots: The location where the snapshots should be saved.
–run-time 5: Only run for 5 seconds.
–scene-ratio=40: Only take a snapshot every 40 frames.
It took some trial and error but I eventually found that 5 seconds covered the person from walking in to walking past the camera (as they enter the house) and the “every 40 frames” meant that 3 snapshots were taken over the course of the 5 seconds.
The next issue I had was that the filenames of the snapshots were difficult to determine as they included the frame number (which could be slightly different each time it was run). A simple solution to this was to simply rename every file in the snapshots directory using a known format (snapshot<n>.jpg):
shell_command:
rename_snapshots: ls /home/pi/snapshots | cat -n | while read n f; do mv "/home/pi/snapshots/$f" "/home/pi/snapshots/snapshot$n.jpg"; done
Using this I can specify the 3 filenames up front in the automation:
...
photo:
- file: /home/pi/snapshots/snapshot1.jpg
caption: "Snapshot 1"
- file: /home/pi/snapshots/snapshot2.jpg
caption: "Snapshot 2"
- file: /home/pi/snapshots/snapshot3.jpg
caption: "Snapshot 3"
...
The result is as follows (Please Note: I have pixelated the photos for privacy, the actual quality is very good ).