MQTT Binary Sensor configuration issue, no matching payload

I’m having an issue to get a binary mqtt sensor to trigger properly. What am I doing wrong?

configuration.yaml:

binary_sensor:
  - platform: mqtt
    name: "Alarm Tablet Sound Detected"
    state_topic: fully/event/onMotion/75e73c62-b38fbd7d
    value_template: '{{value_json.type}}'
    payload_on: acoustic
    off_delay: 5
    device_class: sound

mqtt raw packet:

fully/event/onMotion/75e73c62-b38fbd7d {“type”:“acoustic”,“deviceId”:“75e73c62-b38fbd7d”,“event”:“onMotion”}

error in home-assistant.log:

2020-02-20 08:40:15 WARNING (MainThread) [homeassistant.components.mqtt.binary_sensor] No matching payload found for entity: Alarm Tablet Motion with state topic: fully/event/onMotion/75e73c62-b38fbd7d. Payload: acoustic, with value template Template("{{value_json.type}}")

I think value_template and payload_on/off are interchangeable. At least, that’s how I interpret this:

value_template

(string)(Optional)

Defines a template to extract a value from the payload. Available variables: entity_id . Remove this option when ‘payload_on’ and ‘payload_off’ are sufficient to match your payloads.

Try having value_template return true or false.

 binary_sensor:
  - platform: mqtt
    name: "Alarm Tablet Sound Detected"
    state_topic: fully/event/onMotion/75e73c62-b38fbd7d
    value_template: '{{value_json.type == "acoustic"}}'
    off_delay: 5
    device_class: sound

But I don’t think this is really the issue as I’m pretty sure your way should work.

Ok, I figured this out. It’s a bit tricky of a scenario. I’m trying to break out the “onMotion” of the MQTT topic so that it splits the sensor between acoustic and visual. Turns out it was actually working the entire time, but the error messages were unintended consequences.

The raw packets look like this for both types of events

fully/event/onMotion/75e73c62-b38fbd7d {"type":"acoustic","deviceId":"75e73c62-b38fbd7d","event":"onMotion"}

fully/event/onMotion/75e73c62-b38fbd7d {"type":"visual","deviceId":"75e73c62-b38fbd7d","event":"onMotion"}

configuration.yaml is like this:

binary_sensor:
  - platform: mqtt
    name: "Alarm Tablet Motion"
    state_topic: fully/event/onMotion/75e73c62-b38fbd7d
    value_template: '{{value_json.type}}'
    payload_on: visual
    off_delay: 5
    device_class: motion

  - platform: mqtt
    name: "Alarm Tablet Sound Detected"
    state_topic: fully/event/onMotion/75e73c62-b38fbd7d
    value_template: '{{value_json.type}}'
    payload_on: acoustic
    off_delay: 5
    device_class: sound

The problem is, when the acoustic trigger sets off, you get an error on the “Alarm Tablet Motion” since the acoustic doesn’t match a payload for the “Alarm Tablet Motion” binary sensor.

2020-02-20 10:11:19 WARNING (MainThread) [homeassistant.components.mqtt.binary_sensor] No matching payload found for entity: Alarm Tablet Motion with state topic: fully/event/onMotion/75e73c62-b38fbd7d. Payload: acoustic, with value template Template("{{value_json.type}}")

The reverse happens when the visual trigger is set off since “Alarm Tablet Sound Detected” fails to match a payload.

2020-02-20 10:11:35 WARNING (MainThread) [homeassistant.components.mqtt.binary_sensor] No matching payload found for entity: Alarm Tablet Sound Detected with state topic: fully/event/onMotion/75e73c62-b38fbd7d. Payload: visual, with value template Template("{{value_json.type}}")

Not sure there is a way to suppress these or a best practice to handle this. I could remove all WARNING messages for mqtt.binary_sensor but then I may miss something that I actually care about.

Yes, in the following post I explain two ways to handle a single MQTT topic that contains multiplexed data (i.e. data intended for more than one entity).

Although it mentions Sonoff RF Bridge, the principle remains the same regardless of the source.

Using the first approach described in the linked post, your binary_sensors would be defined like this:

binary_sensor:
  - platform: mqtt
    name: "Alarm Tablet Motion"
    state_topic: fully/event/onMotion/75e73c62-b38fbd7d
    value_template: >
      {% if value_json.type == 'visual' %}
        {{'ON'}}
      {% else %}
        {{states('binary_sensor.alarm_tablet_motion') | upper}}
      {% endif %}
    off_delay: 5
    device_class: motion

  - platform: mqtt
    name: "Alarm Tablet Sound Detected"
    state_topic: fully/event/onMotion/75e73c62-b38fbd7d
    value_template: >
      {% if value_json.type == 'acoustic' %}
        {{'ON'}}
      {% else %}
        {{states('binary_sensor.alarm_tablet_sound_detected') | upper}}
      {% endif %}
    off_delay: 5
    device_class: sound

Works like a charm. Thank you @123!

1 Like