Use a condition if the state of an attribute is empty

I’m using an automation to trigger image_processing.scan when my camera (frigate) sees a car in the driveway. that triggers my plate recognizer to find and read a license plate. The challenge is getting the timing correct so the vehicle is close enough to the camera but not too far that the plate is out of view.

My solution is an automation that will check for a plate 2 times. I want to use a condition in the automation that continues checking the plate if the attribute vehicles: is blank. I can’t find out how to do it.

Here’s my automation

- id: '1651295257276'
  alias: License Plate Trigger
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.driveway_south_car_motion
    to: 'on'
  condition: []
  action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - service: image_processing.scan
    data: {}
    target:
      entity_id: image_processing.platerecognizer_driveway_west
  - condition: state
    entity_id: image_processing.platerecognizer_driveway_west
    attribute: vehicles
    state:
  - service: image_processing.scan
    data: {}
    target:
      entity_id: image_processing.platerecognizer_driveway_west
  mode: single

This is what the attribute looks like when it has found a plate

And when it hasn’t.

When it finds a plate the attribute vehicles: has several lines of info below it. Including the plate it found.

Does anyone know how I can get the automation condition to work?

Can you create a template condition like:

"{{ state_attr('image_processing.platerecognizer_driveway_west', 'vehicles').0.plate is defined }}"

I tried that template in the developer tools template checker and I got:

> UndefinedError: list object has no element 0

If I change the template to:

"

> {{ state_attr('image_processing.platerecognizer_driveway_west', 'vehicles').plate is not defined }}"

I get “true” as a result in the template checker but it doesn’t work as a condition in the automation.

- id: '1651295257276'
  alias: License Plate Trigger
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.driveway_south_car_motion
    to: 'on'
  condition: []
  action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - service: image_processing.scan
    data: {}
    target:
      entity_id: image_processing.platerecognizer_driveway_west
  - condition: template
    value_template: '"{{ state_attr(''image_processing.platerecognizer_driveway_west'',
      ''vehicles'').plate is not defined }}"'
  - service: image_processing.scan
    data: {}
    target:
      entity_id: image_processing.platerecognizer_driveway_west
  mode: single

In the automation above the first image_processing.scan fires but not the second

Nevermind. I found a better way to do it. The state of image_processing.platerecognizer_driveway_west changes to ‘1’ when a plate is found. So I just set the condition to check that state.

Nice!

Someone w/ more HA underpinning’s knowledge than I would have to expound as to why checking if something is defined throw’s an ‘Undefined… error’ instead of just ‘false’

Glad you got it working.

1 Like