Can't wrap my head around configuring MQTT

Hello,

I am still quite the newbie to HA and I need some help with configuring and using MQTT.
I have a home-build (Hardware and Software) MQTT-Client that regularly publishes status updates to a MQTT server (no need to be able to “control” anything on the client from HA).

The MQTT-server is running and does receive the published status messages, this I have confirmed using three different tools (mosquitto_sub, HA-MQTT-Explorer and HA itself (Listen to topic)).

Here is the output from the three Tools (the “time” does not match as the Info is collected at different times):

$ mosquitto_sub -h -p 1883 -u -P -t ‘basement/wateralarm/state’ --pretty
{“sensor”:“Wateralarm”,“time”:1763654945,“status”:“OK”}
{“sensor”:“Wateralarm”,“time”:1763655547,“status”:“OK”}
{“sensor”:“Wateralarm”,“time”:1763656148,“status”:“OK”}

And in HA -Listen to topic:

Message 0 received on basement/wateralarm/state at 17:29:
{
“sensor”: “Wateralarm”,
“time”: 1763656148,
“status”: “OK”
}

And in HA-MQTT-Explorer:

basement
wateralarm
state = {“sensor”:“Wateralarm”,“time”:1763656750,“status”:“OK”}

Even if I receive data - the status of the device shows up as “Unknown”.
The status can contain three different status-codes:
OK
WARNING
CRITICAL

I have searched the net and found some YouTube-Videos, but most of them are either really old or when I try the configuration steps I end up with some errors. :frowning:

Any suggestions where I could have gone wrong?
If you need more information, please feel free to ask.

Regards,
Dan

How did you get the ‘device’ to show up in HA ?
Auto-discovery ? Defined a sensor in .yaml ? ‘Add MQTT device’ in the MQTT integration screen ?

Hello Dan Johansson,

What Francis said is very important.

If you want MQTT Discovery you need to publish in the homeasistant/ topic.
If you want to publish in your own topic, you need to create MQTT YAML to interact with them. Both are documented here:

That was done using “Add MQTT device” in the MQTT integration screen.

Try to set up a binary sensor as an MQTT device.

Then select Binary sensor. The Payload on and off can be customized. Or you can use a value template.

sensor with

What does this even mean? What is “home-build”? What are the “regular status updates”?

Is this Zigbee2MQTT or something else?

Be more specific about your configuration. To me, the things you posted make no sense with Home Assistant, like this:

$ mosquitto_sub -h -p 1883 -u -P -t ‘basement/wateralarm/state’ --pretty
{“sensor”:“Wateralarm”,“time”:1763654945,“status”:“OK”}
{“sensor”:“Wateralarm”,“time”:1763655547,“status”:“OK”}
{“sensor”:“Wateralarm”,“time”:1763656148,“status”:“OK”}

Why is the topic basement? What is the field sensor? Where are these things coming from? And how do you expect Home Assistant to understand them and turn them into Home Assistant things??

You need to be way more clear about what you’re doing.

It seems you set up a sensor instead of a binary_sensor. As the state is OK I guess that is the ON state. Also you can set the device class to moisture.

If I’m correct, 3 states is a sensor, not a binary_sensor

Yes you are correctly assuming that it is a “3 state sensor”.
What I mean with “home-build” is that this is a sensor that I have build myself - hardware (Arduino) and software.
No, the device does not use Zigbee2MQTT but directly sends its status data via WiFi to the MQTT server (which works).
Once every 10 minutes the device will “post” a status message like this

{“sensor”:“Wateralarm”,“time”:1763757241,“status”:“OK”}

This is to be able to see that the device is up & running (keep-alive message).
If something “happens” the status immediately changes to WARNING and if not reacted to within a certain time the status changes to CRITICAL.

I have used exactly the same Hardware and Software in an other “Home Automation Project” and there it worked fine (do not ask why I changed).

From my point of view, a binary_sensor will not work in this scenario as it is only On/Off and not three-state.

I am going to try again to set it up using a “yaml entry”, but last time I tried that I got some error message (cant remember exactly what) in the log.

I’ll get back with the outcome of that test tomorrow.


Dan

I managed to add the MQTT-Device and it shows up as “OK” instead of “Unknown”.

So far so good…

Now I will try to make an automation that triggers by a state change.
But that is another theme and will try to do it myself and before I post here again.

Thanks for your support and help

Dan