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!
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’}
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'}}"
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:
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.