Home Assistant Google Drive Backup - how to change "never delete" status of a backup - video surveillance system proj

I want to share my proj about a video surveillance system and the usage of the addon created by @sabeechen called Home Assistant Google Drive Backup

This proj includes:

So I set all my cameras via their own webacces, to make snapshots and videos via FTP on a folder under HA (media->cams->snaps and media->cams->motioncams) when a motion will be detected.

In HA, i added a “_motsens” suffix to all the entity_ids of all motion cams’ binary sensors .
This because i can have outdoor and indoor cams, for instance, and i want only outdoor cams follow some rules, so i added a suffix in help.

I also enabled the expose_extra_server feature of the Home Assistant Google Drive Backup addons in order to be able to access to the addon via 1627 http port.

Then I created some templates:

  • binary_sensor.cam_motion_sensors_detected
    (in my templates.yaml included via configuration.yaml)
- binary_sensor:
    - name: "Cam motion sensors detected"
      device_class: motion
      state: >
        {%- for s in states.binary_sensor
        if ('motsens' in s.entity_id and s.state == 'on') %}
        {{s.state}}{% if not loop.last %}, {% endif %}
        {%- endfor %}
      attributes:
        last_cam_motion_detected: >
          {%- for s in states.binary_sensor
          if ('motsens' in s.entity_id and s.state == 'on') %}
          {{s.entity_id}}{% if not loop.last %}, {% endif %}
          {%- endfor %}

this binarysensor contains all binaries motion sensor of your cams that contains “motsens” suffix in their entity_id, so if 1 of your cam binary_sens will be triggered, this main binary_sens will change its state to on too.

  • sensor.gdrive_last_bkup_slug
    (in my sensors.yaml included via configuration.yaml)
- platform: template
  sensors:
    gdrive_last_bkup_slug:
      friendly_name: Gdrive last backup slug
      value_template: >
        {%- for s in state_attr('sensor.backup_state', 'backups')
          if (state_attr('sensor.backup_state', 'last_backup') in s.date) %}
          {{s.slug}}{% if not loop.last %}, {% endif %}
        {%- endfor %}
      attribute_templates:
        name: >
          {%- for s in state_attr('sensor.backup_state', 'backups')
          if (states.sensor.gdrive_last_bkup_slug.state in s.slug) %}
            {{s.name}}{% if not loop.last %}, {% endif %}
          {%- endfor %}
        date: >
          {%- for s in state_attr('sensor.backup_state', 'backups')
          if (states.sensor.gdrive_last_bkup_slug.state in s.slug) %}
            {{s.date}}{% if not loop.last %}, {% endif %}
          {%- endfor %}
        state: >
          {%- for s in state_attr('sensor.backup_state', 'backups')
          if (states.sensor.gdrive_last_bkup_slug.state in s.slug) %}
            {{s.state}}{% if not loop.last %}, {% endif %}
          {%- endfor %}

this sensor will give some info about the last backup made, expecially its slug nr. that will be used later for some automations.

a rest command called gdrive_retain_command
(in my restcommands.yaml included via configuration.yaml)

gdrive_retain_command:
  url: https://homeassistant.local:1627/retain
  method: POST
  headers:
    accept: "application/json, text/javascript, */*; q=0.01"
    user-agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36"
  payload: '{"slug":"{{ states.sensor.gdrive_last_bkup_slug.state }}","sources":{"HomeAssistant":true,"GoogleDrive":true}}'
  content_type: "application/json"
  verify_ssl: false

this restfull cmd will be called as a service in automations in order to make permanent/never delete partial backups of media->cams folder (the folder that contains cams snapshots and videos).

  • a smtp notify template under configuration.yaml
notify:
  - name: "gmailsmtp_MYGMAILSMTP"
    platform: smtp
    server: "smtp.gmail.com"
    port: 587
    timeout: 15
    sender: !secret smtp_user
    encryption: starttls
    username: !secret smtp_user
    password: !secret smtp_psw
    recipient:
      - !secret smtp_recipient
    sender_name: "video surveillance system proj"

this service will be called to send snapshots via email by automations

I also created some input entities via HA helpers feature:

  • switch.cams_ftp
    that will toggle the ftp service of all my cam devices

  • switch.cams_recording
    that will toggle the video rec service of all my cam devices

  • input_text.last_cam_motion_detected
    that will be updated with the name of the last cam motion detected used for email etc…

  • input_text.delay_1st_snaps_cams_motion_detected
    a time value to set the backup delay

  • input_boolean.alarm_toggle
    a very useful button to toggle alarm on/off for some tests

So i created some automations:

alias: Cams recording & FTP - allarm toggle
description: ''
trigger:
  - platform: state
    entity_id:
      - input_boolean.alarm_toggle
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.alarm_toggle
            state: 'on'
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.alarm_toggle
          - service: alarm_control_panel.alarm_arm_home
            data:
              code: 'xxxxx'
            target:
              entity_id: alarm_control_panel.allarme_casa
          - service: switch.turn_on
            data: {}
            target:
              entity_id:
                - switch.cams_ftp
                - switch.cams_recording
      - conditions:
          - condition: state
            entity_id: input_boolean.alarm_toggle
            state: 'off'
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.alarm_toggle
          - service: alarm_control_panel.alarm_disarm
            data:
              code: 'xxxxx'
            target:
              entity_id: alarm_control_panel.allarme_casa
          - service: switch.turn_off
            data: {}
            target:
              entity_id:
                - switch.cams_ftp
                - switch.cams_recording
    default: []
mode: single

this automation will enable/disable ftp camera devices features when i will use the lovelace button or when i use the alarm control panel. this to prevent that cams will rec or snaps if the alarm is off.

alias: Quick notify camera motion sensor
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.cam_motion_sensors_detected
condition:
  - condition: state
    entity_id: binary_sensor.cam_motion_sensors_detected
    state: 'on'
  - condition: not
    conditions:
      - condition: state
        entity_id: alarm_control_panel.allarme_casa
        state: disarmed
action:
  - service: camera.snapshot
    data:
      filename: >-
        /media/cams/snaps/snap_{{state_attr(states('input_text.last_cam_motion_detected'),
        'friendly_name')}}.jpg
    target:
      device_id: '{{device_id(states(''input_text.last_cam_motion_detected''))}}'
  - service: notify.persistent_notification
    data:
      message: >-
        Motion detected by
        {{state_attr(states('input_text.last_cam_motion_detected'),
        'friendly_name')}} alle {{ states.sensor.cust_time.state }} del {{
        states.sensor.cust_date.state }}
  - service: notify.gmailsmtp_MYGMAILSMTP
    data_template:
      message: >-
        Motion detected by
        {{state_attr(states('input_text.last_cam_motion_detected'),
        'friendly_name')}} alle {{ states.sensor.cust_time.state }} del {{
        states.sensor.cust_date.state }}
      title: Motion detected
      target: [email protected]
      data:
        images:
          - >-
            /media/cams/snaps/snap_{{state_attr(states('input_text.last_cam_motion_detected'),'friendly_name')}}.jpg
mode: single

this automation will make some cam snaps and will send them to an [email protected] account and make some permanent notifications on HA

alias: Update input_text.last_cam_motion_sensors_detected
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.cam_motion_sensors_detected
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition:
  - condition: state
    entity_id: binary_sensor.cam_motion_sensors_detected
    state: 'on'
action:
  - service: input_text.set_value
    data_template:
      value: >-
        {{ state_attr('binary_sensor.cam_motion_sensors_detected',
        'last_cam_motion_detected') }}
    target:
      entity_id: input_text.last_cam_motion_detected
mode: single

this will update the input_text.last_cam_motion_detected value used for send emails and notifications

alias: Backup cams motion sensors
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.cam_motion_sensors_detected
condition:
  - condition: not
    conditions:
      - condition: state
        entity_id: alarm_control_panel.allarme_casa
        state: disarmed
  - condition: state
    entity_id: binary_sensor.cam_motion_sensors_detected
    state: 'on'
action:
  - delay: '{{states(''input_text.delay_1st_snaps_cams_motion_detected'')}}'
  - service: hassio.backup_partial
    data:
      folders: media
      name: >-
        cams_motion_detected_{{states.sensor.cust_time_4file.state}}_{{states.sensor.cust_date_4file.state}}
mode: single

this will make a partial backup of the media->cams folder after a delay sets by input_text.delay_1st_snaps_cams_motion_detected value

alias: Retain gdrive backup cams motion detected
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.gdrive_last_bkup_slug
    attribute: state
    to: Backed Up
condition:
  - condition: template
    value_template: >-
      {{ 'cams_motion_detected' in state_attr('sensor.gdrive_last_bkup_slug',
      'name') }}
action:
  - service: rest_command.gdrive_retain_command
    data: {}
mode: single

this will call the restfull cmd that will change to “never delete” the cams_motion_detected backup created earlier

alias: Toggle alarm switch by alarm CP
description: ''
trigger:
  - platform: state
    entity_id:
      - alarm_control_panel.allarme_casa
condition: []
action:
  - choose:
      - conditions:
          - condition: not
            conditions:
              - condition: state
                entity_id: alarm_control_panel.allarme_casa
                state: disarmed
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.alarm_toggle
      - conditions:
          - condition: state
            entity_id: alarm_control_panel.allarme_casa
            state: disarmed
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.alarm_toggle
    default: []
mode: single

this will toggle the switch button alarm if you will toggle alarm status via alarm control panel

I hope this will help someone. Cheers!

1 Like