Binary Sensor MQTT with JSON

I have a couple of sensors (not binary sensors) that get JSON data via MQTT successfully so I know I’m missing something simple…

Here’s the config:

binary_sensor:
  - platform: mqtt  
    state_topic: "feldman/dryersensor"
    name: "Laundry Dryer"
    value_template: '{{ value_json.sensor }}'  

JSON is:

{"sensor":"on","uptime_ms":16928697,"heartbeat":"yes"}

The error I’m getting in the Home Assistant logs is:

No matching payload found for entity: Laundry Dryer with state_topic: feldman/dryersensor

6:02 PM components/mqtt/binary_sensor.py (WARNING) - message first occurred at 6:02 PM and shows up 3 times

Error parsing value: 'value_json' is undefined (value: {"sensor":"on","uptime_ms":16798699,"heartbeat":"yes"}, template: {{ value_json.sensor }})

6:02 PM helpers/template.py (ERROR)

I’m running HA 0.101.3 in a docker container and everything else works great!

Thanks!

Have you connected to your mqtt server with something like MQTT Explorer and see what is being posted to this topic?

  - platform: mqtt
    state_topic: "feldman/dryersensor"
    name: "Laundry Dryer"
    value_template: >- 
      {% if value_json.sensor == 'on' %}
        {{'ON'}}
      {% else %}
        {{'OFF'}}
      {% endif %}
1 Like

Here is the JSON that’s being posted to the topic confirmed via the MQTT.fx tool (https://mqttfx.jensd.de) :

{"sensor":"on","uptime_ms":16928697,"heartbeat":"yes"}

Thanks @francisp. I hadn’t thought of doing if/then.

I tried your suggestion but I still get:

Error parsing value: 'value_json' is undefined (value: {"sensor":"on","uptime_ms":20688697,"heartbeat":"yes"}, template: {% if value_json.sensor == 'on' %}
  {{'ON'}}
{% else %}
  {{'OFF'}}
{% endif %})

Strange. I looked at my own config, I get these MQTT messages :
2019-11-10 20:12:41 -> rflink/NewKaku-03a0b800 - {‘SWITCH’: ‘2’, ‘CMD’: ‘OFF’, ‘SWITCH2’: ‘OFF’}

and have this binary sensor :

  - platform: mqtt
    name: sensorTVboven
    state_topic: 'rflink/NewKaku-03a0b800'
    value_template: >- 
      {% if value_json.SWITCH == "2" and value_json.CMD == "ON" %}   
        {{'ON'}}
      {% elif value_json.SWITCH == "2" and value_json.CMD == "OFF" %}
        {{'OFF'}}
      {% else %}
        {{states('binary_sensor.sensorTVboven') | upper}}
      {% endif %}

The only difference I see is that your MQTT uses double quotes, mine single.

If the value of sensor is exclusively on or off then this template will convert the values into uppercase which is what a binary_sensor requires. It does the same thing as francisp’s example, just in one line.

    value_template: '{{ value_json.sensor | upper}}'  

HOWEVER, this won’t eliminate the error message because it claims value_json is undefined. In other words, it is unable to interpret the received payload as a JSON string. That’s puzzling because the payload you’ve presented appears to be a valid JSON string and Home Assistant should be able to convert it to a JSON object. Only thing I can think of is that there may be an unprintable character terminating the string.

As an experiment, try this template:

    value_template: "{{ 'ON' if 'on' in value else 'OFF'}}"

Oooh… Good call @123 !

First off, your test value_template worked:

value_template: "{{ 'ON' if 'on' in value else 'OFF'}}"

But, as you alluded to, looking really closely at the MQTT payload revealed that, due to an off-by-one error in my custom Arduino code, it included the C-string’s null terminator (00) in the message.

This didn’t show up in the text or the pretty views in the MQTT.fx tool. Nor did Home Assistant show it. The way I found it was in hex view in the tool:
39%20PM

Once I updated my Arduino code to remove the trailing character, both the value templates from @123 and @francisp worked. Woo hoo!

Thanks all for the great and very quick help.

P.S. It’s a bit odd to me that “ON” vs. “on” is case-sensitive, but whatever.

1 Like