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

Love it! Thanks!

Interestingly, it seems to think the sequence is opposite of what it is – a vehicle pulling into my driveway is seen as “backing out”.

Could fixing this just be a matter of reversing the image sequence as sent to Google?

Possibly. I noticed mine does this as well on one camera when I enter the drive. I will have a play and report back. :slight_smile:

Google AI is not available in Europe… is this could work with ChatGPT? Or any other AI solution?

Works a treat! Initially got a 400 error about billing but once I added a payment account it worked straight away. It seems UK and EEA areas don’t get gemini for ‘free’ and it has to have a linked payment account. Unsure whether it’ll cost or not as the documentation is unclear.

hey, how do you deal with the occassional 500 errors from Google?

For example, in my case, I just want to Google to tell us what type of bird it is from the photo thumbnail, but if it fails (because of a 500 error), to just say there is motion detected

With a 500 error, it stops the whole automation so I tried this if-else, but it seems to always default to else, even if Google responded correctly:

  - service: tts.speak
    data:
      cache: true
      media_player_entity_id: media_player.fire_tablet
      message: >-        
        {% if 'text' in generated_response %}
            Motion has been detected at your bird feeder. {{ generated_response['text'] }}
        {% else %}
            Motion has been detected at your bird feeder. No AI avialable.
        {% endif %}
    target:
      entity_id: tts.piper

The photo part for this is a little different than text, because with text you can work with the action_done attribute, but I don’t think the photo generation has this?

thanks

Hi @Aussie_Adam , I just want to thank you for this. I’ve been trying to achieve similar for days and then came across this and it helped no end. Kudos sir!

1 Like

This is phenominal, thank you for the write-up! My only issue is I’m seeing an old/cached snapshot in the notification, even though the snapshot is up to date in the folder. Has this happened to anyone else? :slight_smile:

2 Likes

Sorry for the slow reply, but I haven’t come across this issue on my end with Iphone or Ipad. Did you end up getting it sorted or are you still seeing cached images?

Thank you so much for this! I do have one quick question / issue I’m running in to. Even when using the updated script, I’m still receiving notifications indicating “Camera has detected motion however no obvious motion observed comparing snapshots.”

Any thoughts on why it may be triggering? When I check the traces, it’s evaluating as false.

Not sure without seeing your YAML however just double check that you don’t have an extra space or something in the text in the prompt or in the if statement, especially at the end after the full stop. It needs to match exactly or it will still trigger and when I wrote, one space did my head in for ages so just check! If still no joy, post up your YAML on a GitHub Gist or even post below and I’ll try spot what is going on :slight_smile:

Maybe it is something I’m just missing after looking at it over and over. I tried changing the text as well. Apprecaite any guidance on anything I might have missed! I’m posting from the call for the Generative AI on - the actual camera calls for the snapshots works great!

Once again, thanks for posting this!

  - action: google_generative_ai_conversation.generate_content
    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. 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. 
      image_filename:
        - ./www/snapshots/driveway1_snapshot1.jpg
        - ./www/snapshots/driveway1_snapshot2.jpg
        - ./www/snapshots/driveway1_snapshot3.jpg
    response_variable: generated_content
  - if:
      - condition: template
        value_template: |
          {{ generated_content['text'] == 'No Obvious Motion Detected.'}} 
    then:
      - stop: 
    else:
      - metadata: {}
        data:
          title: Driveway Motion Detected
          message: "{{generated_content['text'] }}"
          data:
            image: /local/snapshots/driveway1_snapshot2.jpg
        action: notify.mobile_app_danielws_iphone_2
mode: single

Ok my first reply was wrong, but I fixed it and it runs fine on my end now. Let me know if it works for you now:

  - action: google_generative_ai_conversation.generate_content
    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. 
      image_filename:
        - ./www/snapshots/driveway1_snapshot2.jpg
        - ./www/snapshots/driveway1_snapshot1.jpg
    response_variable: generated_content
  - if:
      - condition: template
        value_template: >-
          {{ 'No Obvious Motion Detected.' in generated_content.text }}
    then:
      - stop: " No motion detected"
    else:
      - metadata: {}
        data:
          title: Driveway Motion Detected
          message: "{{generated_content['text'] }}"
          data:
            image: /local/snapshots/driveway1_snapshot2.jpg
        action: notify.mobile_app_danielws_iphone_2
1 Like

Just tested it - worked great! Thank you so much!

1 Like

Thank You for sharing :slight_smile:

I’m getting notification with old snapshot and not the correct one, there’s any way to refresh before sending the picture from the snapshot folder?

2 Likes

Solution:

image: >-
https://HA-URL/local/snapshots/intercom_snapshot2.jpg?{{as_timestamp(now())}}
2 Likes

Sweet thanks for posting that, I had been meaning to update to include timestamps but never got around to it. Glad you got it sorted. :slight_smile:

@Aussie_Adam

Im getting the alerts and the AI generated text, but no image preview.

Does the image only show if motion changes detected, rather than when “no obvious motion observed”


Edit it seems like I get no images even if it detect and describes and event.

Thank you very much! I have this setup with 4 cams and it works perfectly.

1 Like

This is working nicely with a few Amcrest cams (I’m using the HACS Dahua integration because I need intrusion detection and advanced event (“human”) detection which the Amcrest integration doesn’t offer). One thing I had to add was “continue_on_error: true” to each snapshot generation portion of the scripts because I had a situation where one of the snapshots timed out, and the whole thing failed.

1 Like