What to post in order to create a binary sensor via MQTT autodiscovery?

I am trying to create a binary_sensor programmatically, by sending an autodiscovery payload (this is Node.js but the problem is the content of the payload and not the specific language)

sendMqttToHass(
      `homeassistant/binary_sensor/monitoring_${this.unique_id}/config`,
      JSON.stringify({
        icon: "mdi:web",
        unique_id: this.unique_id,
        name: this.api.name,
        state_topic: `homeassistant/binary_sensor/monitoring_${this.unique_id}/state`,
        monitoring_type: "http",
      })
    )

${this.unique_id} is generated as a SHA256 hash.

The message arrives in MQTT and is next to similar messages from other sources:

This binary sensor does not appear in HA. Is there something extra/special I need to send in the payload?

Your JSON is way off

This is an example of a binary sensor:

'{"name": "sensorTVboven", "device_class": "plug", "state_topic": "sensor/tvboven", "unique_id": "sensortvboven", "device": {"identifiers": ["sensorTVboven"],"name": "sensorTVboven", "model": "Action Switches", "manufacturer": "Action"}}'

See

I am not sure I understand how (or why) it is way off. The documentation that you mentioned (and that I used) list all elements as optional except state_topic - so why isn’t my definition transformed into a binary sensor?

Is there a mandatory entry I missed? Ore one that breaks the sensor?

The code I provided is TypeScript, what you are checking against a JSON validator is an object (dict in Python) that is serialized before being sent out (the JSON.stringify function).

You can see in the MQTT screenshot that what is sent is a proper JSON.

Please paste in the actual MQTT topic contents. We can only see a portion of the data in that screenshot.

Is there a reason for having such a long ID? Whilst I can’t find anything on limits, that’d be my first port of call.

As Troon said, post the actual JSON. You can copy it from MQTT Explorer.

{
    "icon": "mdi:web",
    "unique_id": "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
    "name": "hello",
    "state_topic": "homeassistant/binary_sensor/monitoring_cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e/state",
    "monitoring_type": "http"
}

I also tried with a short (5 chars or so) ID, I also thought that this could be the issue. But the result is the same.

OK, so I issued this using exactly the JSON you pasted above:

mosquitto_pub -h 192.168.1.7 -t 'homeassistant/binary_sensor/hello/config' -m '{"icon": "mdi:web","unique_id": "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e","name": "hello","state_topic": "homeassistant/binary_sensor/monitoring_cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e/state","monitoring_type": "http"}'

and this immediately appeared:

I’m not sure what your issue is: make sure the name (here, hello) is the same in the config topic and the JSON definition.

It’s not the name length either. Once I’d deleted the hello sensor (same unique_id!) and re-issued the command with the stupid long name in the config topic and the name field, that worked too: