Home Assistant Automation to store periodically captured cam PICTURES in Oracle Cloud object storage


Purpose

The idea of this application is to get images captured each minute from your cam devices and store them in an object storage bucket in Oracle cloud so that you can have a robust and secure place to keep then in case you needed.

CREATE A BUCKET IN OCI

Create a normal bucket. If you will, create lifecycle policies in order to save storage costs, for instance:

getTING cam images every minute in hassio

Consult the documentation of your cam to know how to get an static image of them.

Grab the IP of each cam.

In configuration.yaml create shell commands invoking wget as follows:

shell_command:
  get_keleta_image:  "wget http://192.168.1.97/tmpfs/auto.jpg  --header 'Authorization: Basic YW...4u' -q -O /config/img/keleta.jpg"
  get_placeta_image:  "wget http://192.168.1.91/tmpfs/auto.jpg  --header 'Authorization: Basic YW...4u' -q -O /config/img/placeta.jpg"
  get_hall_image:  "wget http://192.168.1.43/tmpfs/auto.jpg  --header 'Authorization: Basic YW...4u' -q -O /config/img/hall.jpg"

Create an automation that calls the different shell commands as a service every minute, for example:

alias: cameras_to_oci
description: ""
trigger:
  - platform: time_pattern
    minutes: /1
condition: []
action:
  - service: shell_command.get_placeta_image
    data: {}
  - service: shell_command.get_hall_image
    data: {}
  - service: shell_command.get_keleta_image
    data: {}
mode: single

UPLOADING CAM PICTURES TO OCI

We are using a program coded in golang to upload the images. Get the code example from here and put in a file called uploader.go. Edit the code and change the name of the images accordingly.

Prior to compile the code do the following in hassio shell:

apk get go 
export GOPATH=/root/go
export PATH=${GOPATH}/bin:/usr/local/go/bin:$PATH
export GOBIN=$GOROOT/bin
mkdir -p ${GOPATH}/src ${GOPATH}/bin
export GO111MODULE=on
go mod init main
go install github.com/oracle/oci-go-sdk/common@latest
go install 	github.com/oracle/oci-go-sdk/example/helpers@latest
go install github.com/oracle/oci-go-sdk/objectstorage@latest
#

Configure oci-cli outside and copy the content of the .oci directory to /root directory in hassio:

NOTE: As an alternative you can use the following approach to avoid puting the key files on your hassio

Test your program:

go run uploader.go

Now, compile the program:

go build uploader.go

Hassio uses busybox interpreter to execute linux commands. If you try to execute a command that is not provided by Busybox you get error 127 in the logs (path not found) because the executable you are invoking doesn’t exist.

For that reason we are gonna utilise the crond daemon to execute our compiled program every minute.

Create a crontab entry by issuing the following (editor behaves like vi):

crontab -e

Add a line at the end, put the name of your compiled program scheduled to be run every minute and exit saving changes (wq!):

Now, execute the following to launch crond daemon:

crond

How it works

The shell commands explained above is executed every minute on second 0 by hassio storing the images in /root/config/img directory

The compiled go program is executed every minute on second 0, but the first line of the code has a sleep that waits 10 seconds (if you have more cameras maybe you need more time, who knows… This sleep is in order to create a delay forr the images to be stored locally. If the delay is too short you will se your images incomplete,

The structure of the folder with the uploaded images is year/month/day/hour:

That’s all, hope it helps!

One Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.