Hey!
Lots of interesting projects in the past thanks to cats
( @teachingbirds (Smart Litter Box (or Smart Cats)))
Got a spare Raspberry and PiNoir camera module ( https://www.raspberrypi.org/products/pi-noir-camera-v2/ ) and decided to track when and how often my cat leaves and returns to his favorite sleeping spot.
All credits for the initial setup go to https://einar.slaskete.net/2018/08/16/using-a-raspberry-pi-as-a-surveillance-camera-in-home-assistant/ (I don’t know if the author is also registered here but a big thank you to Einar!)
My changes:
Added another ffmpeg shell command to make the video compatible with telegram standards
Shutdown/Restart for the pi via ssh
some (rather useful) sensors for remote CPU temperature and data usage
The SSH Part:
I won’t repeat how to add ssh support to the pi and that you have to change the default password for the pi user (basically add a file with no content named “ssh” to the boot partion and you are done)
I use a shell_command to shutdown or reboot the Raspberry via ssh (authenticated with a private key generated only for this purpose).
Generate a ssh key and add it the to the authorized_users file in /home/pi/.ssh/
I’ve restricted the usage for this key to some commands with a bash helper script (log below)
Shell Log (ssh to the pi):
pi@raspberrypi:~ $ pwd
/home/pi
pi@raspberrypi:~ $ mkdir .ssh && cd .ssh
pi@raspberrypi:~ $ ssh-keygen -C "HomeAssistantRemote-$(whoami)@$(uname -n)-$(date -I)" -t ed25519 -f HA_Remote_Key
Generating public/private ed25519 key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in HA_Remote_Key.
Your public key has been saved in HA_Remote_Key.pub.
The key fingerprint is:
....
..
.
pi@raspberrypi:~/.ssh $ cat allowed-commands.sh
#!/bin/sh
#
# You can have only one forced command in ~/.ssh/authorized_keys. Use this
# wrapper to allow several commands.
case "$SSH_ORIGINAL_COMMAND" in
"sudo systemctl restart raspicam.service")
sudo systemctl restart raspicam.service
;;
"sudo shutdown -r now")
sudo shutdown -r now
;;
"sudo shutdown -h now")
sudo shutdown -h now
;;
"sudo cat /sys/class/thermal/thermal_zone0/temp")
sudo cat /sys/class/thermal/thermal_zone0/temp
;;
"uptime")
uptime
;;
*)
echo "Access denied"
exit 1
;;
esac
pi@raspberrypi:~/.ssh $ echo "command=\"/home/pi/.ssh/allowed-commands.sh\"" `cat HA_Remote_Key.pub` >> authorized_keys
Save “HA_Remote_Key” on your HomeAssistant Server (or any other machine of your choice with a working ssh client) and test if everything works with the command:
ssh pi@-IPv4-of-your-Raspberry -i /path/to/your/HA_Remote_Key 'sudo cat /sys/class/thermal/thermal_zone0/temp'
and
ssh pi@-IPv4-of-your-Raspberry -i /path/to/your/HA_Remote_Key 'whoami'
First command should return sth like “49926” and the seconed one “Access denied”
Home Assistant will use “shell_command” to execute the commands via ssh. (Full link to my package at the end of the post)
Example:
shell_command:
ssh_shutdown_rpi3: "ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no [email protected] -i /home/homeassistant/.keys/ssh2rpi3 'sudo shutdown -h now'"
the “-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no” isn’t really satisfying but in my case it was the only solution (maybe because of the VENV?)
The final automation:
- id: record_camera_on_motion
alias: record_camera_on_motion
trigger:
- platform: template
value_template: "{{ is_state('binary_sensor.esp8266_02_pir', 'on') }}"
condition:
- condition: template
value_template: "{{ not is_state('input_boolean.chrishome', 'on') }}"
action:
- service: switch.turn_on
data:
entity_id: switch.11011_c
- service: shell_command.record_raspicam_minute
- delay: 00:00:30
- service: switch.turn_off
data:
entity_id: switch.11011_c
- service: shell_command.record_raspicam_telegram
- service: notify.telegram_christoph
data_template:
message: >
{{ now().strftime('%d.%m.%Y %H:%M:%S') }} Motion was detected!
- service: notify.telegram_christoph
data:
title: MotionCameraELW
message: "Cat Cam"
data:
video:
- file: /home/homeassistant/raspicam/raspicam_telegram.mp4
caption: "Cat Cam"
- delay: 00:01:00
- service: shell_command.archive_raspicam_video
As soon as a the PIR on the shelf detects motion (and I am not home…) the following happens:
Turn on a Lamp (will be replaced with some IR LEDs (as soon as I’ll find them )
Use ffmpeg to dump the stream and save it as mkv
Turn off the Lamp
Convert the mkv stream dump to H264/MPEG4 (Telegram needs this format to autoplay the video)
Send the file with telegram
Archive the file
The commands for ffmpeg etc.:
shell_command:
#Image Processing
record_raspicam_minute: ffmpeg -y -i tcp://192.168.0.26:9000 -an -c:v copy -t 00:00:30 /home/homeassistant/raspicam/raspicam.mkv
# H.264/MPEG4 to autoplay video in telegram
record_raspicam_telegram: ffmpeg -y -i /home/homeassistant/raspicam/raspicam.mkv -an -c:v libx264 -crf 26 -vf scale=640:-1 -t 00:00:20 /home/homeassistant/raspicam/raspicam_telegram.mp4
archive_raspicam_video: cp /home/homeassistant/raspicam/raspicam.mkv /home/homeassistant/raspicam/raspicam-{{ now().strftime("%Y-%m-%d_%H-%M-%S") }}.mkv
Link to the Files:
Package
Lovelace Card
Let me know what you think! =)
Telegram Screenshot:
Lovelace Card (without a cat )
Sample Videos: