Template If/Then and generated content

Hi all,
I have a automation that runs a script when my camera detects motion and that sends it off through the Google Ai Generative integration and the results come back as generated content. That all works fine but I now ask in the prompt for it to respond with “Camera has detected motion however no obvious motion observed comparing snapshots” which works when trees or something set off the motion. Again this all works fine…

Now what I am trying to do though is stop the script sending the notification to one device and not the other if the response contains the above with a if/then/else in my script.

However my yaml seems to ignore the if part and still fires off the notification no matter what. Below is what I have, any ideas on why it is ignoring the “if statement?” I am guessing it’s not getting the generated content or the stop part isn’t right but I am no programmer! I am learning as I go, any tips pointing me in the right direction on what I am doing wrong? Thank you in advance! :slight_smile:

if:
  - condition: template
    value_template: >
      "{{ generated_content['text'] }}" == "{{ 'Camera has detected motion however no obvious motion observed comparing snapshots' }}" 


then:
  - stop: ""
else:
  - service: notify.mobile_app_adams_iphone_15_pro_max
    metadata: {}
    data:
      title: Driveway 1 Motion Detected
      message: "\"{{generated_content['text'] }}\""
      data:
        image: /local/snapshots/driveway1_snapshot2.jpg

You’ve gone a little crazy with the quotes… to the point you are just building a string.

The comparison operator == needs to be inside the expression {{ }}.

if:
  - condition: template
    value_template: >
      {{ generated_content['text'] == 'Camera has detected motion however no obvious motion observed comparing snapshots' }}
then:
  - stop: ""
else:
  - service: notify.mobile_app_adams_iphone_15_pro_max
    metadata: {}
    data:
      title: Driveway 1 Motion Detected
      message: "{{ generated_content['text'] }}"
      data:
        image: /local/snapshots/driveway1_snapshot2.jpg
1 Like

LEGEND! Thank you!! I think I was more than a little confused on how expressions work! I gotta read some more! However while I don’t get a error running the script now, it still fires off the notification! Am I using the stop correctly? This is what I have now:

if:
  - condition: template
    value_template: >
        {{ generated_content['text'] == 'Camera has detected motion however no obvious motion observed comparing snapshots' }} 
then:
  - stop: ""
else:
  - service: notify.mobile_app_adams_iphone_15_pro_max
    metadata: {}
    data:
      title: Driveway 1a Motion Detected
      message: "{{generated_content['text'] }}"
      data:
        image: /local/snapshots/driveway1_snapshot2.jpg

The == operator is checking for an exact match… spelling, capitalization, punctuation, etc needs to match completely.

The automation’s debug trace should provide some insight into why the condition failed.

1 Like

Again thank you! Using the debug trace / changed variables I could see the google response contains a space at the start and that’s why it wasn’t a exact match! All working now! Champion! :slight_smile:

1 Like

I learned something here.

I had no idea that “stop:” was a thing.

:+1:

1 Like