I modified the code to include the if-else block and restarted the HA just to make sure there are no glitches with the reload and still seeing the same state.
From the Developer tools, I also called the service for MQTT: Export to dump the payload for the topic just in case the data was not making its way to this template.
The mqtt:Export service dumped a file “mqtt.dump.txt” in the config folder which shows that correct data is being received by HA.
And you locked/unlocked the door to trigger a new event, just to be sure? Maybe also check your QoS on that topic and whether state is retained.
Lastly, the template can be reduced to this (binary sensors not only accept on/off, but also true/false):
{{ value_json.lockState == "locked" }}
The sensor is on if the template evaluates as True , yes , on , enable or a positive number. Any other value will render it as off . The actual appearance in the frontend (Open /Closed , Detected /Clear etc) depends on the sensor’s device_class value.
Yes, I have few tasmota devices and a zigbee bridge that is using the MQTT broker. There are couple of light automations using these devices which are working fine.
Make sure to send the payload again after reloading the configuration of your MQTT binary sensor.
Otherwise, it will still be “unknown” if the payload is not sent with the retain flag.
Thanks for confirming! Now some good news, I created another sensor for battery level that has information in the same payload and it is working correctly. That eliminates few doubts and seems like MQTT is working correctly.
Another question - is there any way to create a text sensor from MQTT playload? I just want to see the raw value that binary sensor might be getting from the MQTT broker.