Guide for CCTV Snapshot on motion, send to Google Generative AI & get notification with description & snapshot

Brilliant thank you. My first serious attempts at playing around with these prompts. I’ll give it a go. Cheers!

1 Like

I’m sending alerts with images via Pushover as I don’t have the companion app installed.

I’ve added a bit to the end for my own entertainment in the Message field, but shows you can prepend or add anything to the response from Google.

action: notify.pushover
metadata: {}
data:
  title: "{{ motion_name }} Detected"
  target: Samsung-S20
  message: >-
    {{ generated_content['text'] }} This message was generated with Google
    Generative AI.
  data:
    attachment: "{{ snapshot_access_file_path }}"
    sound: cashregister
1 Like

thanks for the script.
i get the HA notification on my iphone.
when i long press the notification, i can read the text but a blank image appears.
the image file is not displayed
although the text matches the image stored.
any idea ?
this is the code
metadata: {}
data:
title: Driveway 1 Motion Detected
message: “{{generated_content[‘text’] }}”
data:
image: /config/www/picture/doorbell/visitor2.jpg
action: notify.mobile_app_dkiphone

whilst it is the same directory it is called using “/local” not “/www” in the notification part, try using this as the image path at the bottom of your script:

image: /local/picture/doorbell/visitor2.jpg

thank you…that worked

1 Like

Just for notice… :smiley:
I ended up with using TELEGRAM instead.
Compainon app is somewhat very restricted with notification history and Androids only allow a couple of lines in text…

Telegram however allows all text and a nice pic.
Sorry for the rooster… :smiley:

Aaa crap. Telegram images won’t show up on my watch.
Damn it. :grimacing:

I’m testing this and it is very promising. However I have an issue for a long time by sending picture notifications to iPhone and for this it is quite essential :slight_smile: Is this even possible receiving a picture with a notification on iPhone? I know Apple devices quite sensitive what too allow and what is very dangerous :slight_smile:

Hi, I have been trying to send via telegram. I get the message, but not the photo. Can you help:

type or paste code here

      - ./www/snapshots/driveway_snapshot1.jpg
        - ./www/snapshots/driveway_snapshot2.jpg
        - ./www/snapshots/driveway_snapshot3.jpg
    response_variable: generated_content
    action: google_generative_ai_conversation.generate_content
  - if:
      - condition: template
        value_template: "{{ 'No Obvious Motion Detected.' in generated_content.text }}"
    then:
      - stop: ""
    else:
      - metadata: {}
        data:
          title: Driveway camera Motion Detected
          message: "{{generated_content['text'] }}"
          data:
            image: /local/snapshots/driveway_snapshot2.jpg
        action: notify.telegram_notifier2
mode: single
description: ""

Are you sure your images are saved ?

action: camera.snapshot
metadata: {}
data:
  filename: www/img/frigate/ai_parkering_1.jpg
target:
  entity_id:
    - camera.frigate_parkering

And then the telegram

action: telegram_bot.send_photo
data:
  file: /config/www/img/frigate/ai_parkering_2.jpg
  caption: "{{ generated_content.text }}"
  title: PARKERING AI

Important that you whitelist your snapshots dir in your configuration.yaml .
Example:

homeassistant:
  allowlist_external_dirs:
    - /config/
    - /config/snapshots/
    - /config/www/img/
    - /

The image path looks suspect, compare where you save the image:

        - ./www/snapshots/driveway_snapshot1.jpg
        - ./www/snapshots/driveway_snapshot2.jpg
        - ./www/snapshots/driveway_snapshot3.jpg

Versus the reference in the Telegram Notification

image: /local/snapshots/driveway_snapshot2.jpg

I suspect the Telegram path need to match the Snapshot Path

This is brilliant. Thanks so much for posting.

I’ve gone from just hearing the doorbell ring, to having home assistant send me a snapshot of who rang the doorbell, to having AI analyse motion at the front door, all in the space of a week.

This really does get the very best out of the technology available.

Aside from that, this morning, I’ve been helping a guy in South Africa get this up and running too. I love how this community connects people from all over the world.

2 Likes

I get the image and text via the companion app, but not telegram.

Here is my complete script:

alias: AI Driveway camera - Snapshot, AI & Notification (Duplicate)
sequence:
  - metadata: {}
    data:
      filename: ./www/snapshots/driveway_snapshot1.jpg
    target:
      device_id: f60ae99c7139f23e79cd9a74e4b875d7
    enabled: true
    action: camera.snapshot
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 500
    enabled: true
  - metadata: {}
    data:
      filename: ./www/snapshots/driveway_snapshot2.jpg
    target:
      device_id: f60ae99c7139f23e79cd9a74e4b875d7
    enabled: true
    action: camera.snapshot
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 500
    enabled: true
  - metadata: {}
    data:
      filename: ./www/snapshots/driveway_snapshot3.jpg
    target:
      device_id: f60ae99c7139f23e79cd9a74e4b875d7
    enabled: true
    action: camera.snapshot
  - metadata: {}
    data:
      prompt: >-
        Motion has been detected, compare and very briefly describe what you see
        in the following sequence of images from my driveway camera number 1.
        What do you think caused the motion alarm? If a person or car is
        present, describe them in detail. Do not describe stationary objects or
        buildings. If you see no obvious causes of motion, reply with "No
        Obvious Motion Detected." Your message needs to be short enough to fit
        in a phone notification. Ignore animals and rain.
      image_filename:
        - ./www/snapshots/driveway_snapshot1.jpg
        - ./www/snapshots/driveway_snapshot2.jpg
        - ./www/snapshots/driveway_snapshot3.jpg
    response_variable: generated_content
    action: google_generative_ai_conversation.generate_content
  - if:
      - condition: template
        value_template: "{{ 'No Obvious Motion Detected.' in generated_content.text }}"
    then:
      - stop: ""
    else:
      - metadata: {}
        data:
          title: Driveway camera Motion Detected
          message: "{{generated_content['text'] }}"
          data:
            image: ./www/snapshots/driveway_snapshot2.jpg
            file: ./www/snapshots/driveway_snapshot2.jpg
        action: notify.telegram_notifier4
      - action: telegram_bot.send_photo
        metadata: {}
        data:
          authentication: digest
          file: ./www/snapshots/driveway_snapshot2.jpg
          target: notify.telegram_notifier4
mode: single
description: ""

I’m not familiar with telegram, sorry.

I would imagine if it works via the companion app but not telegram, then the problem is with telegram :thinking: :man_shrugging:

A novice here, giving this a go.
I’ve got issues with the script saving the snapshot.
I got the error:
“Failed to perform the action script/garage_cam_snapshot_ai_notification. Cannot write filename: ./www/snapshots/garagecam_snapshot1.jpg, no access to path; allowlist_external_dirs may need to be adjusted in configuration.yaml

I went into the configuration file and added the lines:

allowlist_external_dirs:
- www/snapshots/

I created the folder as per instructions so it is there, have double checked spelling!

Here is the start of my code:

alias: Garage Cam - Snapshot, AI & Notification
description: AI work on snapshots
sequence:
  - action: camera.snapshot
    metadata: {}
    data:
      filename: "filename: ./www/snapshots/garagecam_snapshot1.jpg"
    target:
      device_id: 4ec962ba7d4f71f8b342558bd4332922
       enabled: true

Not a clue why I couldn’t copy and paste the code into yaml but it disappeared everytime i saved it or switched to visual interface, so was obviously doing something wrong. I used the visual interface to pick the entities and got the code above. That bit isn’t too important right now though :grinning: one problem at a time!

Edit1: Fixed indentation omission in the yaml file update

allowlist_external_dirs:
  - www/snapshots/

Edit 2: I have now corrected to:

homeassistant:
  allowlist_external_dirs:
    - /config/www/snapshots

Edit 3: Spotted my mistake, I had copied the word filename twice in the script. So I can take snap shots…not to get the rest working!

“Message malformed: Service notify.mobile cdc49305af8725b720f979ee0c23196a does not match format . for dictionary value @ data[‘sequence’][6][‘else’][0][‘action’]”

anyone getting that error?

HA 11.0

1 Like

Nice work.
But this here looks like a nice neet Blueprint works great.

This is absolutely crazy, thank you for sharing this script/automation!!

1 Like

I think you need to use actions now instead of service.

else:
      - action: notify.mobile_app_952505fcee7aafb7d5a574051c2a852e

That’s how mine is. I was getting that error before. I haven’t got mine to work yet though overall…but i think it’s because the photos don’t seem to show anything different. I think maybe the blink app recording video automatically could be stopping me taking photos in time.

Hi Everyone, I have just set this up for my 4 cameras (4 individual scripts and a custom automation). However, I am having a slight issue with the notification. It is displaying a cached image which doesn’t exist anymore and not the recent snapshot. The AI does describe the latest snapshot which I can see in the snapshots folder.

Any ideas on how to fix this?