MQTT binary sensors ignoring off_delay

Hi,

I have a number of MQTT binary sensors with an off delay set to 10 seconds. This is being ignored though. I have another sensor with multiple or conditions set to 90 seconds which works with the correct off delay, but it is slow to trigger on even though one of the conditions is met.

  - binary_sensor:
      name: Front MQTT Dog & Score
      off_delay: 10
      state_topic: "frigate/events"
      value_template: "{{ 'ON' if value_json['after']['label'] == 'dog' and value_json['after']['camera'] == 'front' and value_json['after']['score'] | float >= 0.7  else 'OFF' }}"

image

I have this binary sensor with 4 or conditions set with a 90 second delay which works as expected, but it’s only triggering on the first condition:

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

image

I think the off_delay: should be in a time format

off_delay: 90
to
off_delay: '00:01:30'

Your template is wrong with the additional if statements. It can be reduced to this, returning logical true / false rather than the ON / OFF strings:

{{ value_json['after']['label'] == 'dog'
   and value_json['after']['camera'] in ('dahua', 'front', 'rear', 'reolink')
   and value_json['after']['score'] | float >= 0.7 }}

Always test your templates in Developer Tools / Template. Yours:

Mine:

That is very helpful Thank you!

I did want to test in the Developer Tools, but I didn’t know how to.

I figured out I need to use only payload_on (no payoad_off), then the off-delay works as expected.

  - binary_sensor:
      name: Dog Detected
      off_delay: 90
      payload_on: "True"
      state_topic: "frigate/events"
      value_template: >
        {{ value_json['after']['label'] == 'dog'
            and value_json['after']['camera'] in ('dahua', 'front', 'rear', 'reolink')
            and value_json['after']['score'] | float >= 0.7 }}

image

1 Like