Binary sensor for DSC sensor via MQTT and JSON

I’m trying to setup my home-assistant install to read the states of my DSC 433 MHz sensors (motion, doors etc) and I’m stuck. I’ve gotten the rtl_433 software to figure out which sensor is which and I’ve got it to post JSON output to my MQTT server, however they all post to the same topic however in the JSON message I need to extract the ESN/serial number of the DSC sensor and what is it doing, ie, motion detected or door not closed.

I’d like to figure out a binary sensor that can read this JSON output and change the state of the sensor (which the device class should be door) when the door is opened or closed.

Here is an example of one of my sensors, you’ll see the unique ESN for it and that the closed state changes:

# front door open
RTL_433/Raw {"time" : "2018-06-02 20:42:31", "model" : "DSC Contact", "id" : 2640695, "closed" : 0, "event" : 1, "tamper" : 0, "battery_low" : 0, "xactivity" : 1, "xtamper1" : 0, "xtamper2" : 0, "exception" : 0, "esn" : "284b37", "status" : 161, "status_hex" : "a1", "mic" : "CRC"}

# front door closed
RTL_433/Raw {"time" : "2018-06-02 20:42:33", "model" : "DSC Contact", "id" : 2640695, "closed" : 1, "event" : 1, "tamper" : 0, "battery_low" : 0, "xactivity" : 1, "xtamper1" : 0, "xtamper2" : 0, "exception" : 0, "esn" : "284b37", "status" : 163, "status_hex" : "a3", "mic" : "CRC"}

To create this, I am running this command on another server with a RTL-SDR dongle:

/usr/local/bin/rtl_433 -q -F json -R 23 -p 68 | /usr/bin/mosquitto_pub -h 192.168.0.13 -i RTL_433 -l -t RTL_433/JSON

I’m just at a loss how to write the sensor for this. All the reading I’ve dong points to having unique topics for specific sensors. I could probably always script this when it is polling the sensors and change the topic, but I’d rather just do it in homeassistant as it would be cleaner to manage everything there in the config file.

I’ve read this topic but I’m still a little lost…

Trying this with no luck so far…

# DSC Sensors
- platform: mqtt
  name: 'Under Stairs Door'
  state_topic: "RTL_433/Raw"
  sensor_class: door
  value_template: "{% if value_json.esn == '286337' and value_json.closed == '0' %}on{% elif value_json.esn == '286337' and value_json.closed == '1' %}off
{% endif %}"

Thanks!

Since the value is in quotes, it is a string and so has to match the string value.

That was my bad for the forum post, I just copied and pasted an example of another sensor that wasn’t the same as I was testing so I’m not sure if my syntax is correct or not.

I still haven’t been able to get this to work… Any ideas anyone? Is my value_template correct?