Is it possible to use text as part of an automation with “If Then”?

I have an automation that takes a snapshot from the camera and Google’s Gemini AI analyzes the snapshot and sends back what it sees as text.

I want to add IF-THEN as part of my automation, for example, “If TEXT contains “empty driveway and grass” then don’t notify.”

How does it send back the information?

If it is in a sensor state you can do this with a template condition:

action:
  - if:
      - condition: template
        value_template: "{{ 'empty driveway and grass' not in states('sensor.your_image_descrition_sensor_here') }}"
    then:
      - service: notify...etc

These are the relevant parts of the automation. No, it isn’t a sensor value.

  - alias: Google Generative AI Analyze
    service: google_generative_ai_conversation.generate_content
    metadata: {}
    data:
      image_filename: /config/www/snapshots/driveway.jpg
      prompt: >-
        Explain briefly what you see in the picture, focus only on the driveway.
        Try to describe people in particular. Your message should be short
        enough to fit in a phone notification. Do not describe fixed objects.
        Ignore animals, trash can, or lawn.
    response_variable: answer

and to send the text as notification to my phone, I have:

  - service: notify.mobile_app_iphone
    metadata: {}
    data:
      data:
        image: /api/camera_proxy/camera.driveway_fluent
        actions:
          - action: URL
            title: View
            url: fb1675493782511558://
      title: Driveway
      message: "{{ answer.text }}"
    enabled: true

Post the complete yaml file. And indicate where you want the “if”.

How to help us help you - or How to ask a good question.

Thanks for sharing. First time asking a question here.

Here is the YAML. My previous comment was only basically missing the top part. I want it to not send me a notification if “answer” includes “empty driveway and grass.”

alias: Snapshot Driveway
trigger:
  - platform: state
    entity_id:
      - binary_sensor.driveway_motion
    to: "on"
condition: []
action:
  - alias: Take Snapshot
    service: camera.snapshot
    metadata: {}
    data:
      filename: /config/www/snapshots/driveway.jpg
    target:
      entity_id: camera.driveway_fluent
  - alias: Google Generative AI Analyze
    service: google_generative_ai_conversation.generate_content
    metadata: {}
    data:
      image_filename: /config/www/snapshots/driveway.jpg
      prompt: >-
        Explain briefly what you see in the picture, focus only on the driveway.
        Try to describe people in particular. Your message should be short
        enough to fit in a phone notification. Do not describe fixed objects.
        Ignore animals, trash can, or lawn.
    response_variable: answer
  - service: notify.mobile_app_asads_iphone
    metadata: {}
    data:
      data:
        image: /api/camera_proxy/camera.driveway_fluent
        actions:
          - action: URL
            title: View
            url: fb1675493782511558://
      title: Driveway
      message: "{{ answer.text }}"
    enabled: true
  - if:
      - condition: template
        value_template: "{{ 'empty driveway and grass' not in answer.text }}"
    then:
      - service: notify...etc

I just want to be sure you have successfully received a notification with a image?

Thank you, this worked wonderfully!

Yes, I’m able to receive the image and description in my notifications.

This is unrelated to this post, but the only issue remaining now is that home assistant takes the snapshot a few seconds late, so sometimes the notification is an empty driveway. However, the snapshots from the camera itself (that are emailed to me—enabled emails to test), are at the perfect time.

If only there was a way to get snapshots from Reolink and import them to home assistant.

1 Like