MQTT binary_sensor always unknown (solved, kind of)

Hello there,

Trying to create an automatically discovered binary_sensor here.
In the (partial) arduino code it is

#define smoke_topic "homeassistant/binary_sensor/smoke_1/state" 
char state[] = "online";

client.publish(smoke_topic, String(state).c_str(), false);

And it is working, because here is the log of mosquitto:

1676020599: New connection from 192.168.0.103:60568 on port 1883.
1676020599: New client connected from 192.168.0.103:60568 as ESP8266Client (p2, c1, k15, u'mqtt_user').
1676020599: No will message specified.
1676020599: Sending CONNACK to ESP8266Client (0, 0)
1676020599: Received PUBLISH from ESP8266Client (d0, q0, r0, m0, 'homeassistant/binary_sensor/smoke_1/state', ... (6 bytes))
1676020599: Sending PUBLISH to 8xBErttyplVYvDpTgp72CB (d0, q0, r0, m0, 'homeassistant/binary_sensor/smoke_1/state', ... (6 bytes))
1676020599: Sending PUBLISH to mqttjs_18b8f4b1 (d0, q0, r0, m0, 'homeassistant/binary_sensor/smoke_1/state', ... (6 bytes))

6 bytes, online, that is a match
But in HA, I do not have anything automatically created.
Therefore I tried a manual configuration, like so:
In configuration.yaml

mqtt: !include mqtt.yaml`

In mqtt.yaml

# ----------------------------------------------------------------------------------------------------------------------------------
# Everything MQTT
# ----------------------------------------------------------------------------------------------------------------------------------
binary_sensor:
  - name: smoke_rdc
    state_topic: "homeassistant/binary_sensor/smoke_1/state"
    expire_after: 15
    icon: mdi:fire
    payload_available: online
    device_class: smoke

But whatever I publish on the topic, the state remains unavailable

Can someone please guide me?


EDIT: auto discover achieved but state is still unknown


One step at a time.
I change my sketch so now, at startup, it is publishing a config topic.

{"name": "smoke_1", "device_class": "smoke", "unique_id": "smoke_1",  "state_topic": "homeassistant/binary_sensor/smoke_1/state"}

Therefore, the binary sensor is created automatically in HA, and I removed my manual config, great.

But, the same issue persists with the state of the sensor.
Looking in the documentation (because, yes, I do read doc and search forum)
It is not mandatory to provide more information than the state_topic.

Whatever I send, as plain text is not changing my sensor’s value.
Tried so far: true, false, on, off, online, offline, detected, clear

I saw a lot of topic in the forum about template because of json and so on but I’m not sending any kind of json.

Additional question since I managed to do an auto discovered sensor. How do I make the state (if it is working one day) to expire?

Hi,
I suspect you have not defined the availability topics for the sensor, so HASS is defaulting to an incorrect set of values. You code needs to define and publish a retained “Last Will and Testament” topic (I use Python can can’t help with Arduino - Tasmota and ESPhome are enough for my projects this far).

Here’s a test code example (using command line Mosquitto tools) for a binary switch - different, but the MQTT availability LWT will be the same:

Worst case - fire up MQTT Explorer, flash an ESP8266 with Tasmota, and see how that works in practice.

Oh, and search the forum - custom MQTT development seems to be coming up more regularly now.

If this helps, :heart: this post!

Thank you

as I managed to do the auto discovery, I don’t know what to send as message to the state topic.

I tried various text without success.
According to the documentation it should be straight forward but no, I’m struggling with the state message.
I’m not sending any complex message but maybe I should …
Is it supposed to be some json like { "state": "detected"}?

And I’m now also facing the issue to make this state temporary.

Context: a D1mini is connected to my smoke detector, when the alarm goes off, it will be powered on while smoke is detected and send a state update instantly and then every 10 seconds. Therefore, I’d like the sensor in HA to go back to clear or unavailble if no update of the sensor has occurred in the last 15 seconds.

I know how to do an automation with last_updated but would like to avoid it.

EDIT: This is my source of information

Hints:

  • Read my linked post.

  • Install MQTT Explorer and explore what topics working MQTT devices publish (Tasmota with SetOption19 = 1 is a good example to study).

  • Don’t use JSON unless you see other devices doing the same - KISS!

  • Your goal is to just work like other devices so there’s no need for any server-side setup - it should just work
    (My custom device has multiple actuators, multiple sensors of different types and needs no HASS setup, even for icons and units - all are automatically created. I’d treat server-side setup as a bug.)

  • Creating a connection to a broker should allow the setting of a Last Will and Testament / LWT (e.g. payload="Offline", qos=1, retain=True) to automatically set LWT to Offline after a set timeout.

  • Then when connected to the broker, publish a LWT topic like tele/your device name/LWT payload Online with qos=1 & retain=True

  • Publish state to something like tele/your device name/STATE 1

  • Set LWT to `Offline yourself on disconnect (although the timeout should do this itself).

  • Crash the client - the broker should set Offline itself.

Did not managed to make it work and came back to the manual config with some templates.
Anyway it is now solved, not clean but working.