Binary Sensor with multiple or conditions

Hi,

I’m trying to create a binary sensor with multiple or conditions. I don’t get any errors when reloading the config, but something isn’t right because the sensor status is stuck on unknown. Each condition works as an individual sensor.

  - binary_sensor:
      name: Person Detected
      off_delay: 90
      state_topic: "frigate/events"
      value_template: |-
        {{ if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'dahua' and value_json['after']['score'] | float >= 0.7)
           or if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'front' and value_json['after']['score'] | float >= 0.7)
           or if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'rear' and value_json['after']['score'] | float >= 0.7)
           or if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'reolink' and value_json['after']['score'] | float >= 0.7) }}

Your template resolves to true or false, the default payload_on and payload_off options are ON and OFF respectively.

Either change your template to produce ON or OFF or change the payload_on/off options to true and false respectively.

https://www.home-assistant.io/integrations/binary_sensor.mqtt/#payload_off

I thought it must be something to do with the on/off state. How can I add that to the template?

Either:

  - binary_sensor:
      name: Person Detected
      off_delay: 90
      state_topic: "frigate/events"
      payload_on: true
      payload_off: false
      value_template: >
        {{ if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'dahua' and value_json['after']['score'] | float >= 0.7)
           or if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'front' and value_json['after']['score'] | float >= 0.7)
           or if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'rear' and value_json['after']['score'] | float >= 0.7)
           or if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'reolink' and value_json['after']['score'] | float >= 0.7) }}

Or:

  - binary_sensor:
      name: Person Detected
      off_delay: 90
      state_topic: "frigate/events"
      value_template: >
        {{ 'ON' if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'dahua' and value_json['after']['score'] | float >= 0.7)
           or if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'front' and value_json['after']['score'] | float >= 0.7)
           or if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'rear' and value_json['after']['score'] | float >= 0.7)
           or if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'reolink' and value_json['after']['score'] | float >= 0.7) else 'OFF' }}

I’ve changed to the on/off setup, but the status is still unknown

  - binary_sensor:
      name: Person Detected
      off_delay: 90
      state_topic: "frigate/events"
      value_template: >
        {{ 'ON' if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'dahua' and value_json['after']['score'] | float >= 0.7)
            or if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'front' and value_json['after']['score'] | float >= 0.7)
            or if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'rear' and value_json['after']['score'] | float >= 0.7)
            or if (value_json['after']['label'] == 'person' and value_json['after']['camera'] == 'reolink' and value_json['after']['score'] | float >= 0.7) else 'OFF' }}

image

It will report unknown until a payload is published to frigate/events.

Use an MQTT client, like MQTT Explorer, to confirm that a payload was published to frigate/events.

Apologies, you’re right. A sensor with a single payload seems to report the off status straight away which is what threw me.

The sensor is only activating when the first condition is met.

Any ideas?