Ho to make and AND expression

Hi,
I wish to set a binary_sensor as a combination of 2 attributes in AND:

if (binarySensor.state && imageProcessing.field.subfield > 40) {
  
   return "yes"
}

This:
image

and the state of another binary_sensor:

binary_sensor:
  - platform: template
    sensors:
     vinzhere:
      friendly_name: Vinz Here
      device_class: motion
      value_template: >-
        {% if is_state('binary_sensor.fricam_person_motion', 'on') %}
         {{ state_attr('image_processing.facebox_faccia', 'matched_faces.Vinz') | float > 40 }}
        {% else %}
         no
        {% endif %}

I cannot get how to reference “matched_faces” object that contain “Vinz” key with a numeric value.

faces:
  - name: Vinz
    image_id: sette
    confidence: 78.53
    matched: true
    bounding_box:
      top: 92
      left: 44
      width: 107
      height: 107
    entity_id: image_processing.facebox_faccia
total_faces: 1
matched_faces:
  Vinz: 78.53
total_matched_faces: 1
hostname: 0942f4949d6b
friendly_name: facebox_faccia
device_class: face

NB: This template syntax is confusing many people. I’m a software engineer with 20 year of experience and just spent 3 hours without getting a working simple stupid “if” condition :confused:

Please try this template.

{{ state_attr('image_processing.facebox_faccia', 'matched_faces')['Vinz'] }}
1 Like

Binary sensors can only be on(true)/off(false), not yes/no.
You don’t need if/else here, just a single expression that evaluates to true or false is enough.

Try this:

binary_sensor:
  - platform: template
    sensors:
     vinzhere:
      friendly_name: Vinz Here
      device_class: motion
      value_template: >
        {{ is_state('binary_sensor.fricam_person_motion', 'on') and state_attr('image_processing.facebox_faccia', 'matched_faces')['Vinz'] | float > 40 }}

This binary_sensor will be on when motion is detected and the image processing value is above 40, otherwise it will be off. In tje frontend you will see some other states depending on your language.

1 Like

Ended on this string:

{{ (is_state('binary_sensor.fricam_person_motion', 'on')) and (state_attr('image_processing.facebox_faccia', 'matched_faces')['Vinz'] != None) and (state_attr('image_processing.facebox_faccia', 'matched_faces')['Vinz'] | float > 40) }}

But, when sensor is “off” it says me “Unavailable” in gray and not “Away”.

I think that the problem is that [‘Vinz’] can be present or not (it’s Facebox image_processing).

1 Like

No, it’s not working.

I get this when “Vinz:” key doesnt exists:

UndefinedError: 'dict object' has no attribute 'Vinz'

Any suggestion?

Try

state_attr('image_processing.facebox_faccia', 'matched_faces')['Vinz'] is defined
1 Like